Configuring AWS CodePipeline to build and deploy in another region

Mahmoud Abdelrahman
6 min readJan 9, 2023

--

Introduction

In this article, we will explore the process of configuring AWS CodePipeline to build and deploy applications in a different region. Specifically, we will demonstrate how to set up a CodePipeline in the Mumbai region to build, push, and deploy container images to the UAE region.

AWS CodePipeline is a powerful continuous delivery service that automates release pipelines for efficient application and infrastructure updates. It allows you to define stages in your software release process, enabling you to streamline the deployment process and ensure reliable and fast updates.

However, it’s worth noting that CodePipeline is not available in all AWS regions. In our case, the UAE region does not support CodePipeline. To overcome this limitation, we will configure CodePipeline in the Mumbai region, which does offer this service. We will utilize CodeBuild, another AWS service, to build and deploy our application from Mumbai to the UAE region.

The overall process involves leveraging three key services: Amazon Elastic Container Registry (Amazon ECR), Amazon Elastic Container Service (Amazon ECS), and AWS CodeBuild. Amazon ECR provides a secure and scalable container registry to store and distribute container images, while Amazon ECS simplifies the orchestration and management of containerized applications. AWS CodeBuild, on the other hand, is a fully managed continuous integration service that compiles source code, runs tests, and produces deployable software packages.

Throughout this article, we will guide you through the step-by-step process of configuring each service to enable seamless building and deployment of your application from the Mumbai region to the UAE region. By leveraging the capabilities of these AWS services, you can enhance your deployment workflow and ensure efficient and reliable application updates.

Let’s dive into the configuration details and explore how to set up and leverage AWS CodePipeline, Amazon ECR, Amazon ECS, and AWS CodeBuild to build and deploy applications across regions.

I. Amazon Elastic Container Registry (Amazon ECR)

Amazon ECR is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere. It helps you to push container images to Amazon ECR without installing or scaling infrastructure and pull images using any management tool. It allows you to share and download images securely over Hypertext Transfer Protocol Secure (HTTPS) with automatic encryption and access controls. You can access and distribute your images faster, reduce download times, and improve availability using a scalable, durable architecture.

Configuring ECR in UAE Region

  1. Enable ECR in UAE region.
  2. Create a repository to start storing the images.

In our case, one additional repository was created “node” for the docker node:16.13.2 image to be used in the DockerFile instead of going to DockerHub each time we push a change, as DockerHub has a limit, but ECR does not.

II. Amazon Elastic Container Service (Amazon ECS)

Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that simplifies your deployment, management, and scaling of containerized applications. Simply describe your application and the resources required, and Amazon ECS will launch, monitor, and scale your application across flexible compute options with automatic integrations to other supporting AWS services that your application needs. Perform system operations such as creating custom scaling and capacity rules and observe and query data from application logs and telemetry.

Configuring ECS Cluster, Service and Task Definition with Application Load Balancer in UAE Region

  1. Create a new task definition with the latest image from your ECR repository.
  2. Create a cluster of type Networking only and specify the name of it.
  3. Create a service of type Fargate, specify the VPC and subnets, configure the right task definition, specify the number of tasks to be running, configure a load balancer to handle the running tasks and do the health checks.

III. AWS CodePipeline

AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. It allows you to define the stages of your software release process using the AWS Management Console or AWS command line interface (CLI).

Configuring CodePipeline in Mumbai region

To begin, log in to your AWS Account and navigate to CodePipeline. Make sure you are in the right region. Follow the steps below:

  1. Click on Create pipeline.
  2. Give the pipeline a name, you can specify the name of the role to be created or leave it as it is.
  3. Select your source provider, I use GitHub so I will select GitHub (Version 2). Click on Connect to GitHub, enter a name for the connection and click Connect to GitHub.
  4. Now you have to login to GitHub to establish the connection.
  5. Choose your repository and the branch to be our source.

Build and deploy phases will be discussed briefly in the next section.

IV. AWS CodeBuild

AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces ready-to-deploy software packages. With CodeBuild, you don’t need to provision, manage, and scale your own build servers. You just specify the location of your source code and choose your build settings, and CodeBuild will run your build scripts for compiling, testing, and packaging your code.

Setting up CodeBuild for Build phase and Deploy phase in CodePipeline:

a. Build Phase

1. Edit the Pipeline we created and add a new phase named Build.

2. Assign CodeBuild as the action provider and choose Mumbai region.

3. For the input artifact choose SourceArtifact.

4. Create a new build project for the Build phase.

5. Choose Custom image, assign your Environment type and Role.

6. For the Buildspec, a script is written to build the image and push it to the ECR Repository named with the latest GitHub push commit.

Creating Build phase with CodeBuild in Mumbai region
CodeBuild Project settings
Environment variables to be added to CodeBuild project
Buildspec script that builds and pushes the image

b. Deploy Phase

1. Edit the Pipeline we created and add a new phase named Deploy.

2. Assign CodeBuild as the action provider and choose Mumbai region.

3. For the input artifact choose SourceArtifact.

4. Create a new build project for the Deploy phase.

5. Choose Custom image, assign your Environment type and Role.

6. For the Buildspec, a script is written to take the latest image from ECR Repository named with the latest GitHub push commit, create a new Task Definition Revision and deploys a new ECS Service task with it.

7. A new ECS Service task will be running including the latest Task Definition Revision.

Creating Deploy phase with CodeBuild in Mumbai region
CodeBuild project for Deploy phase
CodeBuild project Environment variables for Deploy phase
Buildspec script that takes the image from ECR, Creates a new revision for Task Definition and deploys a new task with it

Conclusion

To overcome the limitation of CodePipeline not being available in the UAE region, we have configured it in the Mumbai region. By leveraging CodeBuild, we were able to bridge the gap and successfully build and deploy applications from Mumbai to the UAE region.

AWS offers a comprehensive suite of services that can be tailored to your specific needs. Experimenting with different configurations and services can further optimize your application’s performance, availability, and scalability.

We hope this article has provided you with a clear understanding of how to configure AWS CodePipeline to build and deploy applications across regions. By leveraging the power of AWS services, you can streamline your software release process, improve efficiency, and deliver updates with confidence.

Happy building and deploying with AWS CodePipeline!

--

--