CodeBuild Security
CodeBuild Security is a prime scenario target on the DVA-C02 exam because CI/CD pipelines naturally need access to private database endpoints, deployment keys, and third-party API tokens.
Hardcoding credentials directly inside your buildspec.yml or setting plain text environment variables in the CodeBuild console leaves your secrets exposed to anyone with read access to the build project or build logs.
Key Takeawaysβ
π Secrets Management in CodeBuildβ
CodeBuild provides native mechanisms to fetch secrets dynamically at runtime from SSM Parameter Store or AWS Secrets Manager without storing sensitive data in plain text.
Option A: Via the AWS Console UIβ
Inside the CodeBuild Project settings under Additional Configuration βββΊ Environment Variables:
- Plaintext: For non-sensitive config values (e.g.,
APP_ENV=production). - Parameter Store: Select
Parametertype and set the value to the SSM Parameter path (e.g.,/CodeBuild/DBPassword). - Secrets Manager: Select
Secrets Managertype and set the value to the secret name or ARN (e.g.,prod/my-db-secret).

Option B: Directly in buildspec.yml (The Developer Choice)β
You can declare SSM parameters and Secrets Manager secrets natively inside the env block of your buildspec.yml:
version: 0.2
env:
# Plain text environment variables
variables:
STAGE: "prod"
# Secrets fetched from SSM Parameter Store (SecureString / Plaintext)
parameter-store:
DB_PASSWORD: "/CodeBuild/DBPassword"
# Secrets fetched from AWS Secrets Manager
secrets-manager:
API_KEY: "prod/my-api-keys:api_key" # secret-id:json-key
DB_SECRET: "prod/my-db-secret" # Entire secret payload
phases:
build:
commands:
- echo "Building app for $STAGE..."
# Use $DB_PASSWORD and $API_KEY securely in your scripts!
π VPC Security & Network Isolationβ
By default, CodeBuild containers run in an AWS-managed isolated network outside your VPC. This means a default CodeBuild runner cannot directly access private resources (e.g., an RDS instance, ElastiCache cluster, or internal REST API sitting in a private subnet).
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CODEBUILD VPC SETUP β
βββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββ
βΌ βΌ
π Default CodeBuild (Outside VPC) π° In-VPC CodeBuild (Inside VPC)
β’ Public internet access out of the box β’ Provisioned directly inside your private subnets
β’ Cannot reach private RDS / internal endpoints β’ Can reach private RDS, EC2, ElastiCache via Security Groups
β’ Standard build execution environment β’ Requires ENIs (Elastic Network Interfaces) in subnets
β’ Requires NAT Gateway for outbound internet!
If you launch CodeBuild inside a private VPC so it can run database migrations against a private RDS database, your build runner loses default public internet access! If your build step needs to pull external packages (e.g., npm install, pip install, maven build), your private subnet must route through a NAT Gateway!
π IAM Policy Binding Checklistβ
Whenever you wire up Parameter Store, Secrets Manager, or VPC attachments in CodeBuild, the CodeBuild Service Role must carry the required IAM permissions:
- Parameter Store Access:
ssm:GetParameters - Secrets Manager Access:
secretsmanager:GetSecretValue - KMS Decryption (Required for
SecureStringor CMKs):kms:Decrypt - VPC Attachment Permissions:
ec2:CreateNetworkInterface,ec2:DescribeNetworkInterfaces,ec2:DeleteNetworkInterface(managed viaAWSCodeBuildVPCPolicy).
Exam Tipsβ
buildspec.ymlSecret Injection π¨: If a scenario asks for the most secure, clean way to inject database credentials into a CodeBuild container without hardcoding values in source codeβtarget definingparameter-storeorsecrets-managerunder theenvblock inbuildspec.yml.- Access Denied on Build Phase: If a CodeBuild job fails during the environment setup phase with an authorization error while fetching a secretβcheck if the CodeBuild Service Role has
kms:Decryptpermissions on the Customer Managed Key backing the SSM Parameter or Secret. - In-VPC Internet Connectivity: If a CodeBuild project running inside a VPC fails to download third-party npm or PyPI packagesβverify that the private subnets attached to CodeBuild have a valid route to a NAT Gateway!