buildspec.yml, and how to handle related concerns like IAM permissions, custom images, caching, and pipeline integration.
What is AWS CodeBuild?
- AWS CodeBuild is a fully managed continuous integration (CI) service that builds, tests, and packages your code.
- It pulls source from repositories (for example, CodeCommit, GitHub, or Bitbucket), runs the steps in your buildspec file, produces artifacts (S3, ECR), and streams logs/metrics to CloudWatch.
- CodeBuild focuses on CI/build tasks; deployments are typically handled by CodeDeploy, CloudFormation, or other tools.

CodeBuild provisions the environment and downloads the source, then reads and executes the commands defined under these phases in your
buildspec.yml.

buildspec.yml. It demonstrates install, pre_build, build, and post_build phases, using environment variables like AWS_ACCOUNT_ID and AWS_DEFAULT_REGION set in the project or pipeline.
Use
aws ecr get-login-password piped to docker login --password-stdin — this is the recommended current method for authenticating Docker to Amazon ECR. The older aws ecr get-login --no-include-email form is deprecated.post_build phase
- CodeBuild’s artifact destination (S3 or ECR) is configured in the CodeBuild project settings;
post_buildis where you run commands to push or publish artifacts. - If you want CodeBuild to automatically upload certain files to S3, configure the artifact paths in the project settings in addition to any
post_buildcommands.
- ECR:
ecr:GetAuthorizationToken,ecr:BatchCheckLayerAvailability,ecr:PutImage,ecr:InitiateLayerUpload, etc. - S3:
s3:PutObject,s3:GetObjectfor artifact and cache access. - CloudWatch:
logs:CreateLogGroup,logs:CreateLogStream,logs:PutLogEvents.
ecr:BatchGetImage and ecr:GetDownloadUrlForLayer.


- Provision compute resources and start the build environment.
- Download source code (from CodeCommit/GitHub/Bitbucket).
- Execute
install,pre_build,build, andpost_buildphases. - Upload artifacts and finish the job.
- Provisioning failure: job fails immediately.
- Source download failure: build cannot continue and is marked failed.
- Command failures: if any command in
install,build, orpost_buildexits non-zero, CodeBuild marks the build as failed. - Partial artifacts: CodeBuild may still attempt to upload available artifacts depending on configuration — useful for debugging incomplete outputs.
- Managed images: quick bootstrapping for common runtimes.
- Custom images: reproduce specific OS/tooling, enable ARM builds, or include preinstalled dependencies.


- Local cache (per-instance): Docker layer cache, source cache, custom paths.
- S3 cache (shared): store cache artifacts in S3 across builds.

- Source changes in CodeCommit/GitHub trigger CodePipeline.
- CodePipeline invokes CodeBuild to run tests and produce artifacts.
- CodeDeploy (or another deployment tool) handles deployment to QA/staging/production.

- Open the CodeBuild console and view the build’s log group and log stream.
- Use CloudWatch metrics (build duration, success/failure counts) to create alarms and dashboards.


- The buildspec file (
buildspec.yml) controls the main CodeBuild phases:install,pre_build,build, andpost_build. - Use the
installphase to prepare dependencies,pre_buildfor validations,buildto create artifacts, andpost_buildto publish them. - Ensure the CodeBuild execution IAM role has least-privilege permissions for resources the build needs (ECR, S3, CloudWatch).
- CodeBuild integrates with CodePipeline and CodeDeploy for end-to-end CI/CD, supports caching to speed builds, and can run parallel/batch builds across platforms.
- Monitor builds via CloudWatch Logs and metrics for reliable CI operations.
- AWS CodeBuild Documentation
- AWS CodePipeline Documentation
- Amazon ECR Documentation
- Amazon CloudWatch Logs
- AWS IAM Documentation