Skip to main content

Triage and Remediation

Remediation

Using Console

To remediate “Plaintext AWS Credentials In Environment Variables” for an AWS CodeBuild project using the AWS Console:
  1. Open the CodeBuild project
    • Sign in to the AWS Management Console.
    • Go to CodeBuild.
    • In Build projects, click the relevant project name.
  2. Edit environment settings
    • On the project detail page, choose Edit (top right).
    • Scroll to the Environment section.
    • Under Environment variables, identify any variables that contain:
      • AWS_ACCESS_KEY_ID
      • AWS_SECRET_ACCESS_KEY
      • AWS_SESSION_TOKEN
      • Or any other credentials / secrets in plain text.
  3. Remove plaintext AWS credentials
    • For each environment variable that contains an AWS key or secret in Plaintext:
      • Click the trash bin icon to delete it or
      • If you must keep a variable name, clear the plaintext value and plan to replace it with a secure reference (Secrets Manager/Parameter Store).
  4. Use IAM roles instead of access keys (preferred)
    • In the same Environment section, under Service role, ensure that:
      • The CodeBuild project has an appropriate service role (e.g., codebuild-your-project-service-role) with required permissions attached.
    • If needed, open the role in IAM (link next to role name) and:
      • Attach or adjust IAM policies to grant the build the permissions it needs.
    • With a correctly configured service role, you no longer need AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY in environment variables.
  5. (Optional) Use secure environment variables via Secrets Manager or Parameter Store If you must keep non-AWS secrets (e.g., API keys, passwords):
    • Create secret/parameter first:
      • For Secrets Manager:
        • Go to AWS Secrets ManagerStore a new secret.
        • Add your secret value.
        • Note the Secret name.
      • For Systems Manager Parameter Store:
        • Go to Systems ManagerParameter StoreCreate parameter.
        • Type: SecureString, put your secret value.
        • Note the Parameter name (e.g. /myapp/DB_PASSWORD).
    • Reference it in CodeBuild:
      • Back in the CodeBuild project Edit page → EnvironmentEnvironment variables.
      • Click Add environment variable.
      • Set:
        • Name: the variable name (e.g., DB_PASSWORD).
        • Type:
          • Parameter (for Parameter Store) or
          • Secrets Manager (for Secrets Manager).
        • Value:
          • For Parameter Store: the parameter name (e.g., /myapp/DB_PASSWORD).
          • For Secrets Manager: the secret name or ARN.
      • Ensure the CodeBuild service role has permissions to access:
        • ssm:GetParameter (and Decrypt) for Parameter Store, or
        • secretsmanager:GetSecretValue for Secrets Manager.
  6. Save the changes
    • Scroll down and click Update environment (if shown), then Save or Update at the bottom of the page.
  7. Verify
    • Start a new build of the project.
    • Check Build logs to confirm:
      • No credentials are being echoed or logged.
      • The build still has the required access via IAM role or secure secrets references.
This removes plaintext AWS credentials from CodeBuild environment variables and replaces them with the proper, least-privilege IAM role and secure secret storage.
Below are concise, CLI-focused steps to remediate plaintext AWS credentials in AWS CodeBuild project environment variables.

1. Identify Projects With Plaintext Credentials

Check your CodeBuild projects’ environment variables for AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, etc.
Look in .projects[0].environment.environmentVariables for any plaintext AWS creds or other secrets.

2. Decide the Correct Remediation Pattern

Prefer IAM role over environment credentials:
  • Let the CodeBuild service role or an attached instance role have the necessary permissions.
  • For non-AWS secrets, use SSM Parameter Store or Secrets Manager with type=PARAMETER_STORE or SECRETS_MANAGER.
You must first:
  • Create/identify an IAM role with proper permissions, and ensure CodeBuild uses it (serviceRole).
  • Or store secrets in SSM/Secrets Manager and grant CodeBuild role permission to read them.

3. Remove Plaintext AWS Credentials From Environment

  1. Get the existing project configuration:
  1. Edit project.json:
    • Remove any environment variables like:
      • AWS_ACCESS_KEY_ID
      • AWS_SECRET_ACCESS_KEY
      • AWS_SESSION_TOKEN
      • Any other hardcoded credentials.
    • Keep the structure under "environment" the same.
    • Do not touch required fields (name, source, artifacts, serviceRole, etc.) other than editing/removing env vars.
Example environment section before:
Example after (remove the credential vars):
  1. Update the project using the modified JSON:
Adjust fields to match what exists in project.json; omit any args your project doesn’t use.

4. (Optional) Replace With Secure References

If you still need to pass non-AWS secrets to the build:

Using SSM Parameter Store

  1. Store secret:
  1. Grant CodeBuild role access (ssm:GetParameter on that parameter).
  2. Add a parameter store env var (edit project.json again):
  1. Re-run aws codebuild update-project as above.

Using Secrets Manager

  1. Create secret:
  1. Grant CodeBuild role secretsmanager:GetSecretValue on that secret.
  2. Add env var with type=SECRETS_MANAGER.

5. Verify

Run a build and confirm:
  • No plaintext AWS credentials appear in:
    • Project configuration
    • Build logs
    • CloudTrail StartBuild events’ environment variables.
  • Build still succeeds, and IAM role/secret references are functioning.
To remediate this, you must remove plaintext credentials from the CodeBuild environment and instead pull them securely at runtime (e.g., from IAM role, Secrets Manager, or SSM Parameter Store). Below are step-by-step instructions, including Python (boto3) examples.

1. Understand what “plaintext credentials” means in CodeBuild

In CodeBuild, this is bad:
  • Environment variable type: PLAINTEXT
  • Name: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, etc.
  • Value: literal credential string
You want no static AWS keys in environment variables at all.

2. Prefer IAM role over any stored credentials

First choice: let CodeBuild’s service role have permissions to what the build needs.
Then your build just uses the normal AWS SDK with no keys in env vars.
  1. Go to IAM → Roles.
  2. Find the role used by your CodeBuild project (shown under the project’s Service role).
  3. Attach or adjust a policy to allow the resources/actions it needs (e.g., S3, ECR, etc.).
  4. In your buildspec/code, do not set any AWS credentials. Just use the SDK normally.
No Python code change needed other than removing explicit credential usage.

3. If you must store secrets: use Secrets Manager or SSM

If (for some reason) you need non-IAM secrets (API keys, passwords, etc.):

3.1 Store the secret

Example with Secrets Manager:
Give the CodeBuild role permission to read this secret:

4. Remove plaintext environment variables from the CodeBuild project

4.1 Using the AWS Console

  1. Go to CodeBuild → Build projects → Your project.
  2. Click Edit.
  3. Under Environment → Additional configuration → Environment variables:
    • Delete any variables like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, etc. of type Plaintext.
  4. Save.

4.2 Using Python (boto3) to remove/redesign env vars

This example:
  • Fetches the project config
  • Filters out sensitive plaintext variables
  • Optionally replaces them with references to SSM/Secrets Manager (type PARAMETER_STORE or SECRETS_MANAGER)

5. Accessing secrets or AWS from your build (Python example)

5.1 Using IAM role (no env vars)

buildspec.yml example:
build_script.py:
No credentials set anywhere.

5.2 Using Secrets Manager at runtime (if needed)

buildspec.yml:
build_script.py:

Summary
  1. Remove all plaintext AWS credentials from CodeBuild environment variables (console or boto3).
  2. Grant needed permissions via the CodeBuild IAM role.
  3. For non-IAM secrets, use Secrets Manager or SSM and fetch them at runtime in your Python build code.
This change does not force replacement of the aws_codebuild_project resource; it updates the existing project’s environment to remove any environment_variable blocks named AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY. Be careful to preserve all other needed environment variables when editing.Verification with terraform plan should show the aws_codebuild_project resource being updated, with the environment[0].environment_variable entries for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY being removed and no other unintended changes.