Managing Build and Deployment Secrets
Builds and deployments routinely need secrets: database passwords for integration tests, tokens for private package registries, signing keys and credentials for third-party services. This lesson explains how to store those secrets in AWS Secrets Manager and Parameter Store, how CodeBuild, CodePipeline, CloudFormation and deployment targets retrieve them securely, and how to avoid the common ways secrets leak.
Learning outcomes
After completing this lesson, you will be able to:
- Compare AWS Secrets Manager and Systems Manager Parameter Store for storing build and deployment secrets.
- Explain how CodeBuild retrieves secrets and the IAM and AWS KMS permissions required.
- Describe how CloudFormation, Amazon ECS and AWS Lambda consume secrets without exposing them.
- Identify practices that prevent secrets from leaking through logs, artifacts and pipeline configuration.
Why Secrets Need Special Handling in Pipelines
A pipeline touches many systems: the repository, the build environment, artifact stores, deployment tools and the target environment. A secret written in any of these places in plain text is exposed to everyone with read access to that place. Plaintext values in a buildspec are visible to anyone who can read the repository; plaintext environment variables in a CodeBuild project are visible to anyone with codebuild:BatchGetProjects; a password written to a log is visible to anyone who can read the log group; and a configuration file containing a password inside a build artifact is stored in the artifact bucket. The rule is that secrets are stored in a dedicated secret store and retrieved at the last possible moment by an identity that is authorised to read them.
AWS Secrets Manager and Parameter Store
AWS offers two services for storing secrets, and the exam expects you to choose between them.
AWS Secrets Manager
Secrets Manager stores secrets encrypted with AWS KMS, either with the AWS managed key aws/secretsmanager or a customer managed key. Its distinguishing features are automatic rotation, using managed rotation for supported services such as Amazon RDS or a Lambda rotation function for other secrets; native cross-Region replication of secrets; resource-based policies that allow other accounts to read a secret; and versioning with staging labels such as AWSCURRENT and AWSPREVIOUS. Secrets Manager charges per secret per month and per API call.
Systems Manager Parameter Store
Parameter Store stores configuration data and secrets in a hierarchy of names such as /orders/prod/db/password. Parameters can be String, StringList or SecureString; SecureString values are encrypted with KMS. Standard parameters are free and hold up to 4 KB; advanced parameters hold up to 8 KB and support parameter policies such as expiration notifications, at a charge. Parameter Store has no built-in rotation, although rotation can be built with EventBridge and Lambda, and it has no resource-based policies; advanced parameters can be shared with other accounts through AWS RAM. Hierarchical names make it easy to grant access by path, for example to /orders/test/*.
The comparison below summarises the choice.
Retrieving Secrets in CodeBuild
CodeBuild retrieves secrets in three ways. The preferred method is the buildspec env section with parameter-store or secrets-manager mappings, or equivalently project environment variables of type PARAMETER_STORE or SECRETS_MANAGER. CodeBuild resolves them before the first phase and masks their values in the logs. The second method is calling the AWS CLI or SDK from a build command, which is useful when the secret is needed only conditionally. The third, for private Git repositories or registries, is storing the credential in Secrets Manager and referencing it as a source credential.
In every case the service role must be allowed to read the secret. The following policy statements show the minimum permissions for one Secrets Manager secret and one Parameter Store path encrypted with a customer managed key.
{
"Version": "2012-10-17",
"Statement": [
{"Effect": "Allow", "Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:eu-west-1:111122223333:secret:orders/test/db-*"},
{"Effect": "Allow", "Action": ["ssm:GetParameters", "ssm:GetParameter"],
"Resource": "arn:aws:ssm:eu-west-1:111122223333:parameter/orders/test/*"},
{"Effect": "Allow", "Action": "kms:Decrypt",
"Resource": "arn:aws:kms:eu-west-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"}
]
}The secret ARN ends with a six-character random suffix that Secrets Manager adds, which is why the resource uses a trailing -*. The kms:Decrypt statement is needed only when the secret or parameter is encrypted with a customer managed key; for the AWS managed key, the service grants decryption to principals that can read the secret in the same account. A cross-account secret must use a customer managed key, because the AWS managed key cannot be used from another account.
Secrets in Deployments
Deployment tools also consume secrets, and each has a mechanism that avoids exposing values.
AWS CloudFormation dynamic references
A template can reference a secret with {{resolve:secretsmanager:secret-id:SecretString:json-key}} or a SecureString parameter with {{resolve:ssm-secure:/path/name}}. CloudFormation resolves the value during the stack operation without storing it in the template or showing it in stack events or outputs. Secure-string dynamic references are supported only in specific resource properties that accept secrets. A template can also create a secret with a generated password and attach it to a database with AWS::SecretsManager::SecretTargetAttachment, or use the ManageMasterUserPassword property of RDS so that RDS manages the master password in Secrets Manager. Template parameters with NoEcho mask a value in the console and API output but do not protect it elsewhere, so dynamic references are preferred.
Amazon ECS
A task definition can inject secrets into containers as environment variables through the secrets section, which references a Secrets Manager secret or a Parameter Store parameter. The ECS agent retrieves the value when the task starts using the task execution role, not the task role, so the execution role needs the read and decrypt permissions. A running task does not see a rotated value until it is replaced, so rotation should be followed by a new deployment.
AWS Lambda
A Lambda function should retrieve secrets at run time with the SDK, caching them for a short period, or use the AWS Parameters and Secrets Lambda Extension, which caches values locally. Storing secrets in Lambda environment variables makes them visible to anyone with lambda:GetFunctionConfiguration, even though environment variables are encrypted at rest.
Secrets in each tool
Preventing Leaks
Several practices together keep secrets out of places where they do not belong. The list below describes each practice and the leak it prevents.
- Never echo secret values in build commands. CodeBuild masks values it resolved itself, but it cannot mask a value that was transformed, for example base64-encoded.
- Scan repositories and build outputs for secrets during the build and fail the build when one is found.
- Exclude configuration files that contain secrets from artifacts; supply secrets to the deployed application at run time instead.
- Encrypt pipeline artifact buckets with a customer managed KMS key and restrict access to the pipeline and build roles.
- Grant access to secrets by path or name prefix per environment, so a test build cannot read production secrets.
- Rotate any secret that may have been exposed, and prefer short-lived credentials such as IAM roles over long-term access keys for AWS access, so that most builds need no stored AWS secret at all.
Integration Tests Against a Test Database
A CodeBuild project runs integration tests against an Amazon RDS for PostgreSQL test database in a private subnet. The current buildspec contains the database password in plain text, and the security team requires that the password rotate every 30 days.
The password moves to a Secrets Manager secret with managed rotation configured for every 30 days. The buildspec replaces the plaintext value with a secrets-manager mapping that extracts the password key. Because each build resolves the secret when it starts, builds always use the current password and need no change after rotation. The service role is given secretsmanager:GetSecretValue on the secret ARN only. The project is placed in the VPC with a security group that the database security group allows, and a Secrets Manager interface endpoint is added so that the build can reach the service without a NAT gateway. Finally, the plaintext password is rotated immediately, because it remains in the repository history.
An ECS task fails to start with an error that it cannot retrieve a secret from Secrets Manager. The task role has secretsmanager:GetSecretValue. What is wrong?
Show answer
Secrets referenced in the task definition's secrets section are retrieved by the task execution role, not the task role. The execution role needs secretsmanager:GetSecretValue on the secret and kms:Decrypt if a customer managed key is used.
Exam Tips
Remember for the exam
- Choose Secrets Manager when a secret must rotate automatically or be shared across accounts or Regions; Parameter Store SecureString suits static secrets and configuration at lower cost.
- Reference secrets in buildspecs with env secrets-manager or env parameter-store, never as plaintext variables; CodeBuild masks resolved values in logs.
- Use CloudFormation dynamic references ({{resolve:secretsmanager:...}}, {{resolve:ssm-secure:...}}) instead of NoEcho parameters to keep secrets out of templates.
- ECS secrets are fetched by the task execution role; the task role is used by application code.
- Cross-account secret access needs a resource policy on the secret and a customer managed KMS key.