Solution skill
How to retrieve a secret in terraform from aws secret manager
Complete solution
Walks through retrieve a secret in terraform from aws secret manager, step by step. Use it when you need to do exactly this. Not a troubleshooting guide, so skip it if you are here because of an error.
TL;DR
By default, awssecretsmanagersecretversion retrieves information based on the AWSCURRENT label (a.k.a. the latest version): `` data "aws\secretsmanager\secret" "secrets" { arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret - my\secrety\name-123456" } data "aws\secretsmanager\secret\version" "current" { secret\id [your value] } ` And use data.aws\secretsmanager\secret\version.current.secret\_string to get the secret.
Steps
- [pabloxio (top answer)] Here is an example. By default, awssecretsmanagersecret_version retrieves information based on the AWSCURRENT label (a.k.a. the latest version):
2. And use data.aws_secretsmanager_secret_version.current.secret_string to get the secret. If you want to retrieve a specific value inside that secret like DATABASE_URL you can use the built-in function jsondecode:
``` jsondecode(data.aws_secretsmanager_secret_version.current.secret_string)["DATABASE_URL"] ```
``` data "aws_secretsmanager_secret" "by-arn" { arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret - example-123456" } ```
3. How do I then retrieve the value? Meaning what is the "get-value" equivalent in terraform for an EC2 isntance?
## When to use
You are seeing this: [pabloxio (top answer)] Here is an example. Use this skill when you run into "How to retrieve a secret in terraform from aws secret manager".
## When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
## Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
## Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Source: Stack Overflow #62328683 (top answer, 82 votes, 4 answers): I have a secret stored in secrets manager to which I have access to the arn. I want to retrieve the value from this arn and use it in terraform how can I achieve this? I found this from terraform website `` data "aws_secretsmanager_secret" "by-arn" { arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret - example-123456" } `` How do I then retrieve the value? Meaning what is the "get-value" equivalent in terraform for an EC2 isntance?
Verified resolution: [pabloxio (top answer)] Here is an example. By default, awssecretsmanagersecretversion retrieves information based on the AWSCURRENT label (a.k.a. the latest version): ``` data "awssecretsmanagersecret" "secrets" { arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret - mysecretyname-123456" } data "awssecretsmanagersecretversion" "current" { secretid [your value] } ``` And use data.awssecretsmanagersecretversion.current.secretstring to get the secret. If you want to retrieve a specific value inside that secret like DATABASEURL you can use the built-in function jsondecode: `` jsondecode(data.aws_secretsmanager_secret_version.current.secret_string)["DATABASE_URL"]
Source URL: https://stackoverflow.com/questions/62328683
By @issueatlas · Version 3 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_rPEFJullD2vFjOE_MiWzHQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">How to retrieve a secret in terraform from aws secret manager <span>— Vectle</span></a>