Sub topic 11
Referencing Values from CloudFormation and AWS Compute Services
This chapter assumes you can read a CloudFormation template, you know how an IAM role attaches to a Lambda function, and you have already created the parameters and secrets from earlier chapters. Here you will see how templates and compute services pick up values without storing them in code.
The goal: no values in templates or images
A template or container image gets copied into repositories, build logs, and artifact buckets. If a password sits in it, every one of those places now holds the password. Instead, the template should hold a reference, and the value should be fetched at deploy time or at run time.
Two moments of resolution
A value can be resolved at one of two moments:
- Deploy time: CloudFormation reads the value while it creates or updates the stack. The value is then baked into the resource configuration.
- Run time: The application reads the value when it starts or when it needs it, using its IAM role.
Deploy-time resolution is simple, but the resolved value is stored in the resource. Run-time resolution keeps the value out of the resource, and it picks up changes without a stack update.
Dynamic references in CloudFormation
A dynamic reference is a string in the form {{resolve:service:name}}. CloudFormation replaces it during the deployment. The template below uses one for a plain String parameter and passes only the secret's name.
Resources:
AppFunction:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.12
Handler: app.handler
Role: !GetAtt AppRole.Arn
Code:
S3Bucket: my-artifact-bucket
S3Key: app.zip
Environment:
Variables:
DB_HOST: '{{resolve:ssm:/myapp/prod/db/host}}'
DB_SECRET_NAME: myapp/prod/dbDB_HOST is configuration, so resolving it at deploy time is fine. DB_SECRET_NAME is only a name. The function reads the secret itself with GetSecretValue, so the password never appears in the template or in the function's environment variables.
The template relies on an AppRole resource. That role needs the policy from the IAM chapter, scoped to the parameter path, the secret, and the KMS key.
Which reference to use
| Reference | Resolves | Typical use |
|---|---|---|
| `{{resolve:ssm:name}}` | String parameter | Hostnames, feature flags, AMI IDs |
| `{{resolve:ssm-secure:name}}` | SecureString parameter | Only in properties that support it |
| `{{resolve:secretsmanager:id:SecretString:json-key}}` | Secrets Manager secret | Only in properties that support it |
Not every resource property accepts ssm-secure or secretsmanager references, so check the resource documentation first. Also remember that a secret resolved into a property such as a Lambda environment variable is visible to anyone who can describe that function.
Compute services
Prefer the run-time pattern for secrets on compute services, because the IAM role of the service does the authorization.
- Lambda: Read the secret in code with the role's permissions, and cache it between invocations.
- ECS: A task definition can inject a secret or parameter into a container through its
secretssetting, which uses avalueFromARN. The task execution role needs permission to read it. - EC2: The instance profile role lets your application call the APIs directly at startup.
Exercise
Why does the template pass DB_SECRET_NAME to the function instead of resolving the password into an environment variable?
Show answer
A resolved password would be stored in the function configuration, visible to anyone who can describe the function, and it would go stale after rotation. Passing only the name lets the function fetch the current value at run time using its IAM role.