Contents:
This section shows how to implement a CI/CD pipeline that builds Docker images and deploys to ECS Fargate automatically. We will use:
Open AWS CodeCommit Console → Create repository.
Repository name: fargate-microservices
.
Note the repository HTTPS/SSH clone URL. Example (HTTPS):
git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/fargate-microservices
Add your application source code (Dockerfile, app code, buildspec.yml
— see below) and push to the repository.
fargate-build
.fargate-microservices
.aws/codebuild/standard:7.0
(or latest).AmazonEC2ContainerRegistryPowerUser
(or granular ECR push permissions)Create a file named buildspec.yml
in the root of your repository. Example (keep indentation exactly as below):
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com
- REPOSITORY_URI=<ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/fargate-microservice
- IMAGE_TAG=latest
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:$IMAGE_TAG .
- docker tag $REPOSITORY_URI:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $REPOSITORY_URI:$IMAGE_TAG
- printf '[{"name":"fargate-microservice","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files:
- imagedefinitions.json
Notes:
<ACCOUNT_ID>
with your AWS account ID.imagedefinitions.json
is needed by CodePipeline ECS deploy action.IMAGE_TAG
strategy that fits you (e.g., using commit SHA).Open AWS CodePipeline Console → Create pipeline.
Pipeline name: fargate-cicd-pipeline
.
Service role: let CodePipeline create new role or use existing with proper permissions.
Add stages:
Source stage
fargate-microservices
main
Build stage
fargate-build
(created above)imagedefinitions.json
Deploy stage
fargate-workshop-cluster
)api-service
or fargate-service
)imagedefinitions.json
Create the pipeline. The pipeline will run automatically for the current commit.
Make a small change in your repository (e.g., update a README or bump version).
Commit & push to CodeCommit:
git add . git commit -m “Test pipeline” git push origin main
CodePipeline should trigger automatically:
imagedefinitions.json
Verify:
[{ "name": "<container-name-in-taskdef>", "imageUri": "<uri>" }]
.Expected outcome: committing code to the repository triggers an automated build, image push to ECR, and deployment to your ECS Fargate service.