If I am using HCL template, what method should I be using to access secrets from AWS Secrets Manager? I’d like to retrieve the secret and set an environment variable MY_SECRET with the value of the secret? There are multiple documents referencing examples on how to retrieve secrets but I am a bit confused on the difference between locals, function within locals and user variables.
#1) using locals and data source
# https://www.packer.io/plugins/datasources/amazon/secretsmanager
data "amazon-secretsmanager" "basic-example" {
name = "packer_test_secret"
key = "packer_test_key"
version_stage = "example"
}
locals {
value = data.amazon-secretsmanager.basic-example.value
secret_string = data.amazon-secretsmanager.basic-example.secret_string
version_id = data.amazon-secretsmanager.basic-example.version_id
secret_value = jsondecode(data.amazon-secretsmanager.basic-example.secret_string)["packer_test_key"]
}
# 2) using function
# https://www.packer.io/docs/templates/hcl_templates/functions/contextual/aws_secretsmanager
locals {
secret = aws_secretsmanager("my_secret", null)
}
source "null" "first-example" {
communicator = "none"
}
build {
name = "my-build-name"
sources = ["null.first-example"]
provisioner "shell-local" {
environment_vars = ["MY_SECRET=${local.secret}"]
}
}
# 3) using user variables
# https://www.packer.io/docs/templates/legacy_json_templates/user-variables
{
"variables": {
"password": "{{ aws_secretsmanager `globalpassword` }}"
}
}}