AWS Builder Center

Accelerating DevOps on AWS: Building a CI/CD Pipeline with CodePipeline, CodeBuild, and ECS

Streamline your software delivery lifecycle by automating build, test, and deployment processes using AWS native DevOps tools. This tutorial demonstrates how to create a fully automated CI/CD pipeline that deploys containerized applications to Amazon ECS with zero downtime.

Accelerating DevOps on AWS: Building a CI/CD Pipeline with CodePipeline, CodeBuild, and ECS

Manual deployments are slow, error-prone, and stressful. In modern software development, speed and reliability aren't optional — they're competitive advantages. AWS provides a complete suite of managed DevOps services that let you automate your entire software delivery pipeline without managing any infrastructure.
In this guide, we'll build a production-ready CI/CD pipeline that automatically builds, tests, and deploys containerized applications to Amazon ECS whenever code is pushed to GitHub.

Architecture Overview

Our pipeline consists of four stages:
  1. Source: GitHub repository triggers the pipeline on every push to the main branch.
  2. Build: AWS CodeBuild compiles the application, runs unit tests, and creates a Docker image stored in Amazon ECR.
  3. Deploy: AWS CodeDeploy performs a blue/green deployment to Amazon ECS, ensuring zero downtime.
  4. Notify: SNS sends Slack/email notifications on success or failure.

Step 1: Containerize Your Application

Start by creating a Dockerfile in your project root:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Push this to an Amazon ECR repository. ECR provides secure, scalable container storage with integrated IAM access controls and image scanning for vulnerabilities.

Step 2: Configure CodeBuild

Create a buildspec.yml file to define your build process:
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REPO
build:
commands:
- echo Build started on date
- docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $ECR_REPO/$IMAGE_REPO_NAME:$IMAGE_TAG
post_build:
commands:
- docker push $ECR_REPO/$IMAGE_REPO_NAME:$IMAGE_TAG
- printf '{"ImageURI":"%s"}' $ECR_REPO/$IMAGE_REPO_NAME:$IMAGE_TAG > imageDetail.json
artifacts:
files:
- imageDetail.json
- taskdef.json
- appspec.yaml
CodeBuild automatically scales compute resources based on build demand, so you never wait in queue.

Step 3: Set Up CodeDeploy for Blue/Green Deployments

Blue/green deployments eliminate downtime by running two identical environments simultaneously:
  • Blue: Current production version
  • Green: New version being deployed
CodeDeploy gradually shifts traffic from blue to green, automatically rolling back if health checks fail. Configure this in your appspec.yaml:
version: 0.0
Resources:
  • TargetService:
    Type: AWS::ECS::Service
    Properties:
    TaskDefinition: <TASK_DEFINITION>
    LoadBalancerInfo:
    ContainerName: "web"
    ContainerPort: 3000

Step 4: Orchestrate with CodePipeline

CodePipeline ties everything together. Define your pipeline stages:
  1. Source Action: Connect to GitHub via OAuth or personal access token.
  2. Build Action: Point to your CodeBuild project and buildspec.yml.
  3. Deploy Action: Reference your ECS service and CodeDeploy application.
Enable pipeline execution history to track every deployment, view logs, and debug failures quickly.

Best Practices for Production Pipelines

Security

  • Use IAM roles instead of access keys for pipeline permissions.
  • Enable ECR image scanning to catch vulnerabilities before deployment.
  • Store secrets in AWS Secrets Manager, never in environment variables or code.

Reliability

  • Add manual approval gates before production deployments.
  • Implement automated rollback triggered by CloudWatch alarms.
  • Run integration tests in a staging environment before promoting to production.

Performance

  • Use CodeBuild caching for dependencies (npm, pip, Maven) to reduce build times by 50%+.
  • Parallelize independent test suites across multiple CodeBuild containers.
  • Optimize Docker layers to minimize image size and pull times.

Monitoring and Observability

A pipeline is only as good as your ability to detect problems. Set up:
  • CloudWatch Logs: Capture build and deployment logs for debugging.
  • CloudWatch Alarms: Trigger notifications on failed deployments or high error rates.
  • X-Ray Tracing: Track request latency across microservices post-deployment.
  • Pipeline Metrics: Monitor deployment frequency, lead time, and failure rate to measure DevOps maturity.

Conclusion

Automating your CI/CD pipeline on AWS transforms software delivery from a bottleneck into a competitive advantage. By combining CodePipeline, CodeBuild, CodeDeploy, and ECS, you get a fully managed, scalable, and secure deployment workflow that frees your team to focus on building features — not fixing broken deployments.
Start with a simple pipeline for one service, validate it works reliably, then expand to cover your entire application portfolio. The investment pays for itself within weeks.

What's your biggest DevOps challenge? Share your pipeline setup in the comments!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article