Skip to main content
Welcome back. This lesson covers secrets management for SDLC automation with AWS CodeBuild, focusing on when and how to use AWS Secrets Manager and AWS Systems Manager Parameter Store (Parameter Store). We will demonstrate storing and retrieving secrets, mapping secrets into CodeBuild buildspec.yml, and the required IAM/KMS permissions. By the end you should be able to choose the right service for build and deployment secrets and avoid common pitfalls (rotation, cross-account access, and encryption). Decision framework — Secrets Manager vs Parameter Store
  • Secrets Manager (purpose-built for secrets)
    • Native rotation support (built-in for supported managed resources; custom rotation via Lambda for custom secrets)
    • Always encrypted with AWS KMS
    • Stores JSON key/value secrets and supports referencing a specific JSON key
    • Per-secret monthly cost (suitable for high-value credentials that require rotation)
  • Systems Manager Parameter Store (general configuration storage)
    • Types: String, StringList, SecureString
    • Standard tier is free (4 KB per parameter, up to 10,000 parameters)
    • SecureString uses KMS (AWS-managed or customer-managed CMKs)
    • Cost-effective at scale for many configuration values and non-rotating secrets
When to choose:
  • Use Secrets Manager for credentials that require frequent or automatic rotation (e.g., database credentials).
  • Use Parameter Store (Standard or SecureString) for configuration values or many non-rotating secrets where cost is a factor.
Comparison summary
A screenshot of the AWS Systems Manager Parameter Store console showing a list of parameters (e.g., /devops-020-130/api-key, build-environment) with their Tier, Type (String, SecureString, StringList) and last-modified timestamps. The UI also shows controls like "Create parameter," "View details," and a notification about upgrades.
Creating a secret in Secrets Manager When you store a secret in AWS Secrets Manager, the wizard prompts for the secret type. Choose a managed type for supported AWS resources (RDS, DocumentDB, Redshift) or choose “Other” for custom secrets (Docker Hub, third-party credentials, API tokens). For custom secrets you provide key/value pairs which Secrets Manager stores as a JSON object. Example key/values for Docker Hub credentials:
  • username: MyCompany
  • password: SuperSecretPassword123!
  • registry: docker.io
Important: Secrets Manager secrets are stored as JSON and you can reference individual keys in CodeBuild using secret-id:jsonKey notation (see the buildspec.yml example below).
A screenshot of the AWS Secrets Manager "Store a new secret" page showing key/value pairs (username=mycompany, password=SuperSecretPassword123!, registry=docker.io) and an encryption key selection. The interface includes Add row/Remove buttons and Next/Cancel controls.
Configuration and rotation You can set the secret name, description, resource-based permissions, and enable cross-Region replication (replication incurs additional cost). On the rotation step you can enable automatic rotation with a schedule (min 1 day, max 365 days). For supported AWS resources (for example, RDS), rotation can be configured without custom code; custom secrets (Docker Hub, API keys) require a Lambda rotation handler.
A screenshot of the AWS Secrets Manager console on the "Configure secret" step, showing a secret name field populated with "prod/AppBeta/Mysql" and an optional description and tags section. The left pane shows the multi-step flow (Choose secret type, Configure secret, Configure rotation, Review).
Rotation schedule builder example:
A screenshot of the AWS Secrets Manager "Configure rotation" page showing the automatic rotation toggle enabled and rotation schedule settings (schedule expression builder selected with months, start day and time fields).
Sample Python code (boto3) to retrieve a secret from Secrets Manager
After storing a secret you can view the secret and its key/value structure in the Secrets Manager console.
A screenshot of the AWS Secrets Manager console showing a secret named "devops-020-130/docker-credentials" with key/value entries for password, registry, and username (values: demo-docker-password-change-in-production, docker.io, demo-docker-user). The page also shows tabs like Overview, Rotation, Versions, Replication, and options to edit or close.
Systems Manager Parameter Store (SSM) overview Parameter Store is ideal for configuration data and lightweight secrets. Use path-style names (e.g., /devops-020-130/test-parameter) to organize parameters. Select a tier (Standard is free through reasonable limits) and choose a type (String, StringList, SecureString).
  • SecureString uses KMS for encryption; for cross-account decryption you must use a customer-managed CMK (the default AWS-managed CMK cannot be used cross-account).
  • Use StringList for comma-separated lists such as feature flags.
A screenshot of the AWS Systems Manager Parameter Store "My parameters" page showing a list of stored parameters (e.g., /devops-020-130/api-key) with their types, tiers, and last modified timestamps. The console also shows controls like Create parameter, View details, Edit, and Manage settings.
Example: creating a SecureString parameter for a CodeBuild NPM auth token. Use Standard tier and choose an appropriate KMS key (customer-managed key is recommended if you need cross-account access).
Screenshot of the AWS Systems Manager Parameter Store showing the "My parameters" list. It displays several /devops-020-130 parameters (e.g., api-key, npm-auth-token) with tiers/types like Standard, SecureString and String and a "Create parameter" button.
StringList example (feature flags stored as comma-separated values):
An AWS Systems Manager Parameter Store overview for the parameter "/devops-020-130/feature-flags," showing parameter details (ARN, type StringList, tier) and the value entries ENABLE_LOGGING, ENABLE_METRICS, ENABLE_TRACING.
Referencing Secrets and Parameters from CodeBuild (buildspec.yml) CodeBuild supports importing environment variables from both SSM Parameter Store and Secrets Manager in the env: section of buildspec.yml:
  • parameter-store — reference SSM parameters by name or ARN.
  • secrets-manager — reference Secrets Manager secrets using secret-id or secret-id:jsonKey to map a specific JSON key to an environment variable.
Advanced buildspec.yml example (consolidated for this demo):
Permissions required for CodeBuild to retrieve secrets and parameters The CodeBuild service role must have permission to read the Secrets Manager secret (secretsmanager:GetSecretValue) and/or SSM parameters (ssm:GetParameter, ssm:GetParameters). If the secret/parameter is protected by a customer-managed CMK, the role also needs kms:Decrypt and kms:DescribeKey on that CMK. If KMS is not permitted, CodeBuild will fail with AccessDeniedException. Example IAM policy fragments (trimmed for clarity):
If you use Secrets Manager or SSM SecureString with a customer-managed CMK, ensure the CodeBuild role is allowed to call kms:Decrypt and kms:DescribeKey on that CMK. For cross-account access, the CMK’s key policy must explicitly allow the other account.
Other considerations and exam-focused notes
  • Rotation: Secrets Manager supports automatic rotation (1–365 day schedule). Custom secrets require a Lambda rotation handler; some AWS services (RDS) have built-in rotation without custom code.
  • Cross-account access: For CodeBuild in Account A to read secrets or SecureString parameters in Account B, grant both the resource-level access (Secrets Manager/SSM) and the CMK key policy permissions for kms:Decrypt. You may need to reference full ARNs in buildspec.yml.
  • Buildspec syntax: Use secret-id:jsonKey in env.secrets-manager to map a specific JSON key to an environment variable. SSM parameter names or ARNs go under env.parameter-store.
  • Cost tradeoffs: Secrets Manager is priced per secret and is often the best choice for rotation-enabled credentials. Parameter Store Standard tier is free and more cost-effective for many configuration values.
References and further reading Thanks for reading.

Watch Video

Practice Lab