I am trying to use gomplate functions to select a ...
# atmos
d
I am trying to use gomplate functions to select a single value out of a list and am having no luck. Are general gomplate functions supported? I'm trying variations on this:
Copy code
components:
  terraform:
    foo:
      vars:
        subnets: !template '{{ toJson ((atmos.Component "subnets" .stack).outputs.private_subnet_ids | coll.Index 0) }}'
all
coll.*
functions I attempt to use yield a Go error message like "can't evaluate field Index in type interface {}" I'm using this as a source for gomplate functions: https://docs.gomplate.ca/functions/ Are these unsupported? And if so, is there advice on slicing or selecting from output lists?
a
Atmos supports Gomplate (uses the
gomplate
binary in the template engine). You should enable it in
atmos.yaml
(which I guess you did)
Copy code
templates:
  settings:
    # Enable `Go` templates in Atmos stack manifests
    enabled: true
    sprig:
      # Enable Sprig functions in `Go` templates in Atmos stack manifests
      enabled: true
    # https://docs.gomplate.ca
    # https://docs.gomplate.ca/functions
    gomplate:
      # Enable Gomplate functions and data sources in `Go` templates in Atmos stack manifests
      enabled: true
we did not use the
coll.Index
function before, maybe it's not supported by the embedded gomplate binary, or might be some other issues - please create an issue in the Atmos repo, we'll take a look at it
πŸ‘ 1
---
for now, test this: Use the built-in Go template
index
function (or Sprig helpers) instead, and don’t pipe the collection into `index`β€”`index` expects the collection as the first argument:
Copy code
components:
  terraform:
    foo:
      vars:
        subnets: !template '{{ toJson (index ((atmos.Component "subnets" .stack).outputs.private_subnet_ids) 0) }}'
πŸ‘€ 1
or try Sprig’s
first
(https://masterminds.github.io/sprig/lists.html)
Copy code
!template '{{ ((atmos.Component "subnets" .stack).outputs.private_subnet_ids | first) | toJson }}'
d
This has worked! Thank you, @Andriy Knysh (Cloud Posse)
πŸ‘ 1
e
Atmos supports Gomplate (uses the
gomplate
binary in the template engine).
We don't use the binary, right? we use the SDK
@Andriy Knysh (Cloud Posse) could this be rewritten to a simple
!terraform.state
with yq?
πŸ‘€ 1
a
We don't use the binary, right? we use the SDK
yes, we are using the SDK
Copy code
github.com/hairyhenderson/gomplate/v4 v4.3.3
Copy code
components:
  terraform:
    foo:
      vars:
        subnets: !terraform.state subnets ".private_subnet_ids | .[0]"
!terraform.state
with YQ
@Duffy Gillman use that ^
it's much simpler than the templates
πŸ™‡ 1
e
(and faster)
d
thanks, guys
w
is there a simple way to get outputs using the CLI with the
!terraform.state
function? eg.
Copy code
atmos terraform state subnets private_subnet_ids
a
!terraform.state
reads the outputs of a component in a stack. thich yu can do using
atmos terraform output <component> -s <stack>
or, if you are asking if it's possible to see the results of
!terraform.state
inside the YAML manifest, execute
atmos describe component <component> -s <stack>
- if the component has
!terraform.state
in the YAML config, it will be evaluated and the final value shown
w
thanks for replying, I was more asking if atmos could return the outputs from state data without having to init the component first
a
oh, you are thinking about a new command
atmos terraform state
πŸ™‚ (we can implement it)
πŸ™Œ 1
e
probably
atmos state
since it's not specific to terraform per say
I think first version would be
atmos terraform state get|set|delete|init
r
To add to this, this is the same issue we were reporting previously, it does look like parts of gomplate are turned off, https://sweetops.slack.com/archives/C031919U8A0/p1758849197918349 I understand that the code in atmos doesn't do that but something is wrong and its not working the way the documentation is describing for atmos
a
@Gabriela Campana (Cloud Posse) please create a task "Review
gomplate
template functions in Atmos" - some
gomplate
functions look like they are not configured. And it looks like some namespaced functions are not working @Red_M thanks for reporting that
βœ… 1
g
πŸ‘ 1