Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
To remediate “Logging should be enabled for CodeBuild project environment” using the AWS Management Console:
-
Sign in and open CodeBuild
- Go to AWS Management Console → search for CodeBuild → open AWS CodeBuild.
-
Select the project
- In the left pane, click Build projects.
- Click the name of the project you want to fix.
-
Edit the project
- On the project details page, click Edit (top right).
- Scroll down to the Logs or Build logs section.
-
Enable CloudWatch Logs
- Under CloudWatch logs, select Enabled.
- Choose a Log group:
- Either pick an existing CloudWatch Log group, or
- Click the option to Create new and give it a name (e.g.,
/aws/codebuild/<project-name>).
- (Optional but recommended) Set a stream name format or accept the default.
-
Enable S3 Logs (optional but recommended)
- Under S3 logs, select Enabled.
- Choose or create an S3 bucket to store logs.
- (Optional) Specify a bucket prefix (e.g.,
codebuild-logs/<project-name>/).
-
Verify service role permissions
- Note the Service role listed in the project configuration (e.g.,
codebuild-<project-name>-service-role). - In a new tab, go to IAM → Roles → open that role.
- Confirm it has permissions like:
- For CloudWatch Logs:
logs:CreateLogGrouplogs:CreateLogStreamlogs:PutLogEvents
- For S3 logs:
s3:PutObject(for the chosen bucket/prefix)
- For CloudWatch Logs:
- If missing, attach or update a policy to include these actions for the relevant Log Group and S3 bucket.
- Note the Service role listed in the project configuration (e.g.,
-
Save the project
- Go back to the Edit build project page.
- Scroll to the bottom and click Update build project (or Save).
-
Validate logging
- Start a new build for that project.
- Go to:
- CloudWatch → Logs → Log groups → open your log group → verify build logs appear.
- S3 → your log bucket → verify log files are being created (if S3 logs enabled).
Using CLI
Using CLI
Below are concise, step-by-step AWS CLI instructions to enable logging for an existing AWS CodeBuild project.Assumptions:
Inspect
If it already exists, you can ignore any “resource already exists” error.
4. Build the
You will use
6. Run
Here is a generic example that includes the most common parameters and sets If you prefer to do it manually (without Make sure all fields match your existing project values; only
You should see:(or your S3 configuration if you enabled it).If you share your current
- You already have a CodeBuild project named
MY-CODEBUILD-PROJECT. - You want to enable CloudWatch Logs (and optionally S3 logs).
1. Get the current project configuration
You need the full current config so you can pass it back intoupdate-project (CodeBuild requires most fields, not just logs).project.json and locate the entry under "projects" → first object. That is the full project definition.2. (Optional but recommended) Create a CloudWatch Logs log group
Pick a log group name, e.g./codebuild/MY-CODEBUILD-PROJECT:3. (Optional) Create an S3 bucket for logs
If you also want S3 logs, create / choose a bucket, e.g.my-codebuild-logs-bucket:4. Build the logsConfig JSON
You will use --logs-config in update-project.4.1 Example: Enable only CloudWatch Logs
4.2 Example: Enable both CloudWatch Logs and S3 logs
5. Extract required fields from the existing project
Fromproject.json, extract each of the following values from the project object:namedescription(if present)sourceartifactsenvironmentserviceRoletimeoutInMinutes(if present)queuedTimeoutInMinutes(if present)encryptionKey(if present)tags(if present)vpcConfig(if present)badgeEnabled(if present)buildTimeout,queuedTimeout(old fields) as applicable- Any other fields you see that are set (you should generally re-supply them).
jq, but the safest is to reuse everything from that object.Example using jq to extract common fields:6. Run update-project with the new logs config
Here is a generic example that includes the most common parameters and sets logsConfig from logs-config.json.jq), just plug the JSON fragments directly, e.g.:logsConfig should be changing.7. Verify logging is enabled
project.json (redacted), I can give you an exact aws codebuild update-project command for your environment.Using Python
Using Python
To remediate “Logging should be enabled for CodeBuild project environment” with Python, you need to:
Make sure the CodeBuild service role has permission to write to that S3 bucket.
This script can be run once per project (or loop over all projects) to remediate the “logging disabled” finding programmatically.
- Identify the project(s)
- Enable at least one logging destination (CloudWatch Logs or S3) in
logsConfig - Update the CodeBuild project via
boto3
1. Prerequisites
boto3installed:- AWS credentials configured (env vars,
~/.aws/credentials, or an attached IAM role). - IAM permissions for:
codebuild:BatchGetProjectscodebuild:UpdateProjectlogs:CreateLogGroup(if you need to create the group)logs:DescribeLogGroups
2. Decide logging configuration
Example: enable CloudWatch Logs for a project:- Log group name:
/aws/codebuild/my-project-logs - Stream name:
codebuild-log-stream
3. Python script to enable CloudWatch Logs
4. To enable S3 logging (optional or in addition)
Adjust thelogsConfig block:This script can be run once per project (or loop over all projects) to remediate the “logging disabled” finding programmatically.
Using Terraform
Using Terraform
logs_config is an in-place update and does not force replacement of the CodeBuild project, but it will overwrite any existing logs configuration in the same way as the CLI command (including disabling S3 logs if you do not also configure s3_logs here).To verify, terraform plan should show an update to aws_codebuild_project.this.logs_config.cloudwatch_logs with status changing to ENABLED (and group_name/stream_name set as specified), with no destroy/create replacement of the project resource.
