Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Last active August 22, 2025 14:02
Show Gist options
  • Select an option

  • Save jweyrich/ee090a223f53700976cc4c2834f8c047 to your computer and use it in GitHub Desktop.

Select an option

Save jweyrich/ee090a223f53700976cc4c2834f8c047 to your computer and use it in GitHub Desktop.
Sample of buildspec.yml for AWS CodeBuild that builds a Docker image from code and push it to ECR to be deployed via CodePipeline
version: 0.2
env:
variables:
#
# NOTE: You need to replace these values!
#
ACCOUNT_NUMBER: "REPLACE_ME"
ECR_REGION: "REPLACE_ME"
ECR_REPOSITORY_NAME: "REPLACE_ME"
ECS_CONTAINER_NAME: "REPLACE_ME"
phases:
install:
runtime-versions:
docker: 18
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $ECR_REGION --no-include-email)
- REPOSITORY_URI=$ACCOUNT_NUMBER.dkr.ecr.$ECR_REGION.amazonaws.com/$ECR_REPOSITORY_NAME
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name”:"$ECS_CONTAINER_NAME","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json
@Kelvinskell
Copy link

Found this to be very useful.

@adshrc
Copy link

adshrc commented Dec 29, 2023

aws ecr get-login is deprecated.

Use this instead:
aws ecr get-login-password --region $__ECR_REGION__ | docker login --username AWS --password-stdin $__ACCOUNT_NUMBER__.dkr.ecr.$__ECR_REGION__.amazonaws.com

Your ServiceRole will need these Permissions:

"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"

@acrolink
Copy link

@adshrc

$__ECR_REGION__ , $__ACCOUNT_NUMBER__ , $__ECR_REGION__ need to be configured somewhere ?

@Rishiii7
Copy link

Rishiii7 commented Apr 23, 2024

@adshrc

$__ECR_REGION__ , $__ACCOUNT_NUMBER__ , $__ECR_REGION__ need to be configured somewhere ?

Yes you can configure them in environment variables in AWS codebuild during creating a new project

@rath23
Copy link

rath23 commented Aug 22, 2025

Screenshot from 2025-08-22 18-20-24

Buildspec runs in the build stage of your pipeline.

version: 0.2
phases:
install:
runtime-versions:
docker: 28
commands:
- echo "cd into $CODEBUILD_SRC_DIR/backend"
- cd $CODEBUILD_SRC_DIR/backend-flask
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $IMAGE_URL
build:
commands:
- echo Build started on date
- echo Building the Docker image...
- docker build -t backend-flask .
- "docker tag $REPO_NAME $IMAGE_URL/$REPO_NAME"
post_build:
commands:
- echo Build completed on date
- echo Pushing the Docker image..
- docker push $IMAGE_URL/$REPO_NAME
- cd $CODEBUILD_SRC_DIR
- echo "imagedefinitions.json > [{"name":"$CONTAINER_NAME","imageUri":"$IMAGE_URL/$REPO_NAME"}]" > imagedefinitions.json
- printf "[{"name":"$CONTAINER_NAME","imageUri":"$IMAGE_URL/$REPO_NAME"}]" > imagedefinitions.json

env:
variables:
AWS_ACCOUNT_ID: 651746472793
AWS_DEFAULT_REGION: ap-south-1
CONTAINER_NAME: backend-flask
IMAGE_URL: 651746472793.dkr.ecr.ap-south-1.amazonaws.com
REPO_NAME: backend-flask:latest
artifacts:
files:
- imagedefinitions.json

i am getting this error can you help

@jweyrich
Copy link
Author

jweyrich commented Aug 22, 2025

Did a few changes in the gist. Hopefully the updated version makes it easier to understand and use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment