-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AWS Secrets Manager - retrieve json secret as a map #4789
Comments
This seems for me is a bug to set wrong schema type for output
Which should be defined to https://www.terraform.io/docs/extend/schemas/schema-types.html#typemap The original output is (
Similar codes in
|
Secrets Manager allows two modes of operation with respect to non-binary secrets:
The Secrets Manager API only returns a The existing The second case of a JSON mapping being returned in its "raw" string format is understandably harder to work with given the current native abilities of Terraform core (as of 0.11.7) and its configuration language (HCL) though, since we do not provide an easy built-in function (e.g. Thanks to some of the stricter typing that will be available with the enhancements with Terraform's configuration language coming in the next major version of Terraform (preview blog post available here), the implementation of a # Potential Terraform 0.12 configuration - details may change during implementation
data "aws_secretsmanager_secret_version" "map_example" {
secret_id = data.aws_secretsmanager_secret.map_example.id
}
output "map_value" {
value = jsondecode(data.aws_secretsmanager_secret_version.map_example.secret_string)["example"]
} Further tracking of the new While I cannot give personal experience in this recommendation, it is probably worth mentioning that there is a community Given the above, I believe we should wait until Terraform 0.12 is released with |
Thanks, it is clear explanation. I understood the secret string can be three types, string, map and binary, then PR #5087 will not be suitable. I will close it. This issue should be closed as well to not misleading the reader who need spend time on it. I will try the plugin terraform-provider-jsondecode you mentioned, if it doesn't work as expect, I will continuous to use mine, until v0.12 get released. Because our project use secret map only and need this feature now. |
Here's a solution for Terraform v0.11 without any dependencies:
Or throw it into a module and return the whole map as output:
|
Hi Everyone 👋 We recently released our first beta of Terraform 0.12, which includes the By example, if I create a secret with a JSON string: $ aws --region us-east-2 secretsmanager create-secret --name bflad-testing --secret-string '{"key1": "value1", "key2": "value2"}'
{
"ARN": "arn:aws:secretsmanager:us-east-2:123456789012:secret:bflad-testing-a8Wfqj",
"Name": "bflad-testing",
"VersionId": "ae6f71c3-e99f-4d35-b4f1-7d2037df0976"
} It can be parsed with Terraform 0.12 and a Terraform 0.12-compatible version of the Terraform AWS Provider (I used a development snapshot of the provider from the blog post in this case) via this example configuration: terraform {
required_version = "0.12.0"
}
provider "aws" {
region = "us-east-2"
}
data "aws_secretsmanager_secret" "example" {
name = "bflad-testing"
}
data "aws_secretsmanager_secret_version" "example" {
secret_id = data.aws_secretsmanager_secret.example.id
}
output "secret_key1_value" {
value = jsondecode(data.aws_secretsmanager_secret_version.example.secret_string)["key1"]
}
output "secret_key2_value" {
value = jsondecode(data.aws_secretsmanager_secret_version.example.secret_string)["key2"]
} Its $ terraform0.12-beta1 apply
data.aws_secretsmanager_secret.example: Refreshing state...
data.aws_secretsmanager_secret_version.example: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
secret_key1_value = value1
secret_key2_value = value2 I will keep this issue open to add a |
Hi again 👋 Coinciding with the timing of the second beta release of Terraform 0.12, version 2.7.0 of the Terraform AWS Provider was released today and is compatible with Given the below configuration: terraform {
required_version = "0.12.0"
}
provider "aws" {
region = "us-east-2"
version = "2.7.0"
}
data "aws_secretsmanager_secret" "example" {
name = "bflad-testing"
}
data "aws_secretsmanager_secret_version" "example" {
secret_id = data.aws_secretsmanager_secret.example.id
}
output "secret_key1_value" {
value = jsondecode(data.aws_secretsmanager_secret_version.example.secret_string)["key1"]
}
output "secret_key2_value" {
value = jsondecode(data.aws_secretsmanager_secret_version.example.secret_string)["key2"]
} And running with Terraform 0.12.0-beta2: $ aws --region us-east-2 secretsmanager create-secret --name bflad-testing --secret-string '{"key1": "value1", "key2": "value2"}'
{
"ARN": "arn:aws:secretsmanager:us-east-2:--OMITTED--:secret:bflad-testing-nENYtI",
"Name": "bflad-testing",
"VersionId": "1e22208f-10ab-4ed6-bade-7b043abac110"
}
$ terraform0.12-beta2 init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws (terraform-providers/aws)" (2.7.0)...
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform0.12-beta2 apply
data.aws_secretsmanager_secret.example: Refreshing state...
data.aws_secretsmanager_secret_version.example: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
secret_key1_value = value1
secret_key2_value = value2 Please create new GitHub issues for any additional feature requests or bug reports with this functionality. Enjoy! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Community Note
Description
AWS Secrets Manager can store secrets as a plain string or json with flat string to string mapping.
It would be nice to have native support for retrieving secrets stored as json. Currently Terraform returns only string output with no easy way to convert it to map. Ideally, there could be something like
secret_map
attribute onaws_secretsmanager_secret_version
data source, returning secret as a map.Terraform probably should attempt to parse the secret string only when
secret_map
attribute is accessed.The motivation is to conserve the number of secrets (AWS cost), Terraform objects, API calls, etc.
New or Affected Resource(s)
Potential Terraform Configuration
References
The text was updated successfully, but these errors were encountered: