Hey do we have docs for `!terraform.output` and `!...
# atmos
m
Hey do we have docs for
!terraform.output
and
!terraform.state
interpolations? I will like to do simple add a string at the end of the output like for secrets. so !terraform.output/!terraform.state will get the secret ARN:
Copy code
arn:aws:secretsmanager:REGION:AWS:secret:SUPER_SECRET_NAME
And I will like to append this string to get a specific secret:
Copy code
:username::
so at the end the yaml will look like this:
Copy code
username: 'arn:aws:secretsmanager:REGION:AWS:secret:SUPER_SECRET_NAME:username::'
Is there a way to do this? I have try many ways but I continue to use
atmos.Component
bc it works better for me
a
currently, you can do the following with
!terraform.output
and
!terraform.state
YAML functions. (this will be in the docs in the next release)
Copy code
## Using YQ Expressions to modify values returned from the remote state

Since the `output` parameter of the `!terraform.state` function is a [YQ](https://mikefarah.gitbook.io/yq) expression,
you can use [YQ pipes](https://mikefarah.gitbook.io/yq/operators/pipe) and
[YQ operators](https://mikefarah.gitbook.io/yq/operators) (including [YQ string concatenation functions](https://mikefarah.gitbook.io/yq/operators/add#string-concatenation)
and [YQ arithmetic functions](https://mikefarah.gitbook.io/yq/operators/add))
to modify the values returned from the remote state.

For example, suppose you have an `aurora-postgres` Atmos component which has the output `master_hostname`.

To read the output without modification, you can use the following expressions:

```yaml
postgres_url: !terraform.state aurora-postgres master_hostname
Copy code
yaml
postgres_url: !terraform.state aurora-postgres .master_hostname
To prepend and append strings to the output, you can use YQ pipes and the
add
function (
+
operator):
Copy code
yaml
postgres_url: !terraform.state aurora-postgres ".master_hostname | ""jdbc:postgresql://"" + . + "":5432/events"""
:::note The double quotes are required around the whole YQ expression, and around the strings inside the YQ expression, hence the double-double quotes
""
to escape it. ::: After the
!terraform.state
function is executed, the
postgres_url
variable will have the final value similar to:
Copy code
yaml
postgres_url: "jdbc:postgresql://aurora-postgres-cluster-writer.prod.plat.mydomain.net:5432/events"
For more details, review the following docs: - [YQ pipe](https://mikefarah.gitbook.io/yq/operators/pipe) - [YQ operators](https://mikefarah.gitbook.io/yq/operators) - [YQ string concatenation](https://mikefarah.gitbook.io/yq/operators/add#string-concatenation) - [YQ
add
function](https://mikefarah.gitbook.io/yq/operators/add)```
since the
output
parameter is a YQ expression, you can use YQ pipes, string functions and other functions to transform a value returned from the remote state (this is not the prettiest syntax, but it works. We'll think about how to simplify it and add "native" Atmos support to transform the values returned from remote state)
@Miguel Zablah
m
Oh thanks! I was missing the
"
wrap the includes the output key! perfect! Let me try it now and yeah adding this to the docs will be just perfect I was looking for this the other day there hehehe 🙂
@Andriy Knysh (Cloud Posse) I have just tested it and it works but it gives me a map of strings the key is the correct interpolation but the value is null of it so it looks like this:
Copy code
```yaml
postgres_url: !terraform.state aurora-postgres ".master_hostname | . + "":5432/events"""
but the value is:
Copy code
yaml
postgres_url: {
"jdbc:postgresql://aurora-postgres-cluster-writer.prod.plat.mydomain.net:5432/events": null
}
is this expected?```
I see the issue it looks it might be bc I'm using it for secrets and the
::
is the issue is just adding one
:
and the other it looks like creates the map of strings
a
let me test it, i’ll ping you when done
m
thanks, I'm also testing so I will also ping you if I find a solution
a
if you add
:
at the end, it generates an invalid YAML (
:
signifies a start of a map). this is currently not supported by Atmos and i'm not sure yet how to handle it. we'll have to review it in more details @Miguel Zablah do yu need to have
:
at the end? (w/o it, the expression works OK)
m
Yeah it's for setting the secret on ECS
Copy code
arn:aws:secretsmanager:region:aws_account_id:secret:secret-name:json-key:version-stage:version-id
But maybe I can modify my tf to allow only 'json-key' as the last part of the secret instead of' expecting empty or value for 'version-stage' and 'version-id'
Otherwise I will just go back to using 'Atmos.Compose' bc I also could not find a solution for it using YQ
a
it’s the last colon that breaks YAML, colons inside are ok
m
I guess I can get arround it by adding the missing
::
if we use one of
json-key
,
version-stage
or
version-id
but it kinda feels wrong hahaha
I will use
atmos.Compose
while I think about this change maybe is worth it and it might be accomplish with a regex but idk it feels wrong hahaha I wish there was a way to do this with YQ and
!terraform.state
a
YQ and
!terraform.state
- return the "correct" (composed) value, but if there is
:
at the end, the YAML parser treats it as the start of a map.
we'll have to review this and implement transformations as a native Atmos feature (not using the complex YQ expressions)
m
yeah maybe having atmos do the transformations will be a waay better solution I thinks this can be very usful
s
facing the same issue with ipv6 address that have
:
in the end, via
!terraform.state
it translates to a map... will use atmos.Component meanwhile