How to Set Up CI/CD with AWS CodePipeline and ECS: A Step-by-Step Guide

Nic Lasdoce
06 Jul 20235 minutes read

Learn how to set up a seamless CI/CD pipeline using AWS CodePipeline and ECS. This step-by-step guide provides clear instructions to accelerate software delivery and ensure quality standards. Whether you're new to CI/CD or an experienced developer, leverage the power of AWS tools to streamline your software development process.

Introduction

In today's software development landscape, implementing an effective CI/CD (continuous integration/continuous deployment) pipeline is crucial for accelerating software delivery and maintaining high-quality standards. This article provides a direct and practical guide on how to set up a robust CI/CD workflow using AWS CodePipeline and ECS (Elastic Container Service). Whether you're a seasoned developer or new to CI/CD practices, follow the step-by-step instructions outlined here to leverage the power of AWS tools and streamline your software delivery process. Let's dive in and get your CI/CD pipeline up and running with AWS CodePipeline and ECS.

Stage 1: ECS Deployment

Section 1: Container Registry

  1. Create elastic container registry at https://console.aws.amazon.com/ecr/repositories?region=us-east-1

Section 2: Load Balancer

  1. Create a load balancer target group with port 80 and type = IP at https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#TargetGroups:
  2. Create application load balancer at https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LoadBalancers:sort=loadBalancerName
  3. Add a listener to load balancer
    1. Without SSL
      1. Add a listener on port 80 and forward to the target group
    2. With SSL
      1. Create SSL certificate using AWS ACM
      2. Add a listener to port 443 with ACTION forward to the target group
      3. Add a listener on port 80 with ACTION redirect to port 443

Section 3: ECS Tasks

  1. Create a task definition (use Fargate)
    1. Make sure to add the target container
  2. Go to the task definition you just created then create a service
    1. When selecting the port mapping and load balancer (application), make sure to select the ones created in previous steps
      1. port 443 if with SSL
      2. port 80 if no SSL
      3. Target group = target group from Load Balancer Step 1 on previous slide

Section 4: DNS Routing

  1. Go to AWS route 53 and select the domain name to be used in Load balancer
  2. Create a new DNS Record
    1. Set record type to "A"
      1. Tick the "Alias" for easier mapping (this is AWS Route 53 only feature, if you are using other DNS then use CNAME in step 7.2.1)
      2. On record name add the subdomain you wish to route
      3. On route traffic to, select the Application Load balancer (if using Route 53 and Alias, else use appropriate CNAME)

Stage 2: CI/CD Setup

Section 1: Source Stage

  1. Go to https://console.aws.amazon.com/codesuite/codepipeline/pipelines and click Create Pipeline
  2. Under Source Stage, select Bitbucket or Github
  3. Click connect to Bitbucket/GitHub
  4. Select repository and branch that will trigger the pipeline
  5. Make sure to tick “Start the pipeline on source code change”

Section 2: Build Stage

  1. Select AWS Codebuild for Build Provider
  2. If you do not have a build project then click on Create Project. Note that it is common that there is only 1 build project for all environments (dev, staging, prod) because we assume they are identical and can be configured via Environment Variables to do some changes
    1. Under Environment select Managed Image
    2. Select Amazon Linux 2 as Operating System
    3. Tick the “Privileged” flag
    4. Select Use a buildspec file
      1. Add the buildspec file to source code. You can use this as reference:
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI=${REPOSITORY_URI}
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
- ENV=${ENV:=development}
- IMAGE_NAME=${IMAGE_NAME:=api}
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:latest .
# - docker run $REPOSITORY_URI:latest npm run test
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$ENV
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- docker push $REPOSITORY_URI:$ENV
- echo Writing image definitions file...
- printf '[{"name":"%s","imageUri":"%s"}]' $IMAGE_NAME $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json

Section 3: Deploy Stage

  1. Under Deploy provider, select Amazon ECS
  2. Select cluster name
  3. Select Service name
  4. If you used the buildspec in the Stage 2 Section 2.2.d then it automatically create imagedefinitions.json so you can leave the remaining fields empty

Bonus

If you are a founder needing help in your Software Architecture or Cloud Infrastructure, we do free assessment and we will tell you if we can do it or not! Feel free to contact us at any of the following:
Social
Contact

Email: nic@triglon.tech

Drop a Message

Tags:
Software Development
TechStack
AWS
NodeJS

Nic Lasdoce

Software Architect

Unmasking Challenges, Architecting Solutions, Deploying Results

Member since Mar 15, 2021

Tech Hub

Unleash Your Tech Potential: Explore Our Cutting-Edge Guides!

Stay ahead of the curve with our cutting-edge tech guides, providing expert insights and knowledge to empower your tech journey.

View All
8 AWS Services That Will Help You Better Use AWS
10 Jun 20244 minutes read
Monolith: From Zero to Millions
14 May 20244 minutes read
View All

Get The Right Job For You

Subscribe to get updated on latest and relevant career opportunities