We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.
If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”
Customize cookie preferences
We use cookies and similar tools (collectively, "cookies") for the following purposes.
Essential
Essential cookies are necessary to provide our site and services and cannot be deactivated. They are usually set in response to your actions on the site, such as setting your privacy preferences, signing in, or filling in forms.
Performance
Performance cookies provide anonymous statistics about how customers navigate our site so we can improve site experience and performance. Approved third parties may perform analytics on our behalf, but they cannot use the data for their own purposes.
Allowed
Functional
Functional cookies help us provide useful site features, remember your preferences, and display relevant content. Approved third parties may set these cookies to provide certain site features. If you do not allow these cookies, then some or all of these services may not function properly.
Allowed
Advertising
Advertising cookies may be set through our site by us or our advertising partners and help us deliver relevant marketing content. If you do not allow these cookies, you will experience less relevant advertising.
Allowed
Blocking some types of cookies may impact your experience of our sites. You may review and change your choices at any time by selecting Cookie preferences in the footer of this site. We and selected third-parties use cookies or similar technologies as specified in the AWS Cookie Notice.
Unable to save cookie preferences
We will only store essential cookies at this time, because we were unable to save your cookie preferences.
If you want to change your cookie preferences, try again later using the link in the AWS console footer, or contact support if the problem persists.
Manage blue/green deployments of microservices to multiple accounts and Regions by using AWS code services and AWS KMS multi-Region keys - AWS Prescriptive Guidance
Created by Balaji Vedagiri (AWS), Ashish Kumar (AWS), Faisal Shahdad (AWS), Anand Krishna Varanasi (AWS), Vanitha Dontireddy (AWS), and Vivek Thangamuthu (AWS)
Summary
Notice: AWS CodeCommit is no longer available to new customers. Existing customers of AWS CodeCommit can continue to use the service as normal. Learn more
This pattern describes how to deploy a global microservices application from a central AWS account to multiple workload accounts and Regions in accordance with a blue/green deployment strategy. The pattern supports the following:
Software is developed in a central account, whereas workloads and applications are spread across multiple accounts and AWS Regions.
A single AWS Key Management System (AWS KMS) multi-Region key is used for encryption and decryption to cover disaster recovery.
The KMS key is Region-specific and has to be maintained or created in three different Regions for pipeline artifacts. A KMS multi-Region key helps retain the same key ID across Regions.
The Git workflow branching model is implemented with two branches (development and main) and code is merged by using pull requests (PRs). The AWS Lambda function that is deployed from this stack creates a PR from the development branch to the main branch. The PR merge to the main branch initiates an AWS CodePipeline pipeline, which orchestrates the continuous integration and continuous delivery (CI/CD) flow and deploys the stacks across accounts.
This pattern provides a sample infrastructure as code (IaC) setup through AWS CloudFormation stacks to demonstrate this use case. The blue/green deployment of microservices is implemented by using AWS CodeDeploy.
Prerequisites and limitations
Prerequisites
Four active AWS accounts:
A tools account to manage the code pipeline and maintain the AWS CodeCommit repository.
Three workload (test) accounts for deploying the microservices workload.
This pattern uses the following Regions. If you want to use other Regions, you must make the appropriate modifications to the AWS CodeDeploy and AWS KMS multi-Region stacks.
Tools (AWS CodeCommit) account: ap-south-1
Workload (test) account 1: ap-south-1
Workload (test) account 2: eu-central-1
Workload (test) account 3: us-east-1
Three Amazon Simple Storage Service (Amazon S3) buckets for the deployment Regions in each workload account. (These are called S3BUCKETNAMETESTACCOUNT1, S3BUCKETNAMETESTACCOUNT2 and S3BUCKETNAMETESTACCOUNT3 later in this pattern.)
For example, you can create these buckets in specific accounts and Regions with unique bucket names as follows (replace xxxx with a random number):
##In Test Account 1aws s3 mb s3://ecs-codepipeline-xxxx-ap-south-1 --region ap-south-1##In Test Account 2aws s3 mb s3://ecs-codepipeline-xxxx-eu-central-1 --region eu-central-1##In Test Account 3aws s3 mb s3://ecs-codepipeline-xxxx-us-east-1 --region us-east-1#Example##In Test Account 1aws s3 mb s3://ecs-codepipeline-18903-ap-south-1 --region ap-south-1##In Test Account 2aws s3 mb s3://ecs-codepipeline-18903-eu-central-1 --region eu-central-1##In Test Account 3aws s3 mb s3://ecs-codepipeline-18903-us-east-1 --region us-east-1
Limitations
The pattern uses AWS CodeBuild and other configuration files to deploy a sample microservice. If you have a different workload type (for example, serverless), you must update all relevant configurations.
Architecture
Target technology stack
AWS CloudFormation
AWS CodeCommit
AWS CodeBuild
AWS CodeDeploy
AWS CodePipeline
Target architecture
Automation and scale
The setup is automated by using AWS CloudFormation stack templates (IaC). It can be easily scaled for multiple environments and accounts.
Tools
AWS services
AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and Regions.
AWS CodeBuild is a fully managed build service that helps you compile source code, run unit tests, and produce artifacts that are ready to deploy.
AWS CodeCommit is a version control service that helps you privately store and manage Git repositories, without needing to manage your own source control system.
AWS CodeDeploy automates deployments to Amazon Elastic Compute Cloud (Amazon EC2) or on-premises instances, AWS Lambda functions, or Amazon Elastic Container Service (Amazon ECS) services.
AWS CodePipeline helps you quickly model and configure the different stages of a software release and automate the steps required to release software changes continuously.
Git is an open-source, distributed version control system that works with the AWS CodeCommit repository.
Docker is a set of platform as a service (PaaS) products that use virtualization at the operating-system level to deliver software in containers. This pattern uses Docker to build and test container images locally.
cfn-lint and cfn-nag are open-source tools that help you review CloudFormation stacks for any errors and security issues.
Clone the sample repository into a new repository in your work location:
##In work location
git clone https://github.com/aws-samples/ecs-blue-green-global-deployment-with-multiregion-cmk-codepipeline.git
AWS DevOps
Package the Cloudformation resources.
In this step, you package the local artifacts that the CloudFormation templates reference to create the infrastructure resources required for services such as Amazon Virtual Private Cloud (Amazon VPC) and Application Load Balancer.
The templates are available in the Infra folder of the code repository.
Package and deploy the CloudFormation stacks for the infrastructure
Task
Description
Skills required
Clone the repository.
Clone the sample repository into a new repository in your work location:
##In work location
git clone https://github.com/aws-samples/ecs-blue-green-global-deployment-with-multiregion-cmk-codepipeline.git
AWS DevOps
Package the Cloudformation resources.
In this step, you package the local artifacts that the CloudFormation templates reference to create the infrastructure resources required for services such as Amazon Virtual Private Cloud (Amazon VPC) and Application Load Balancer.
The templates are available in the Infra folder of the code repository.
Push a sample (NGINX) image to the Amazon Elastic Container Registry (Amazon ECR) repository named web (as set in parameters). You can customize the image as required.
To log in and set the credentials for pushing an image to Amazon ECR, follow the instructions in the Amazon ECR documentation.
where Poc-Service refers to your sample application.
Verify that the services are accessible from the Application Load Balancer by using a fully qualified domain name (FQDN) or DNS from a browser or by using the curl command.
AWS DevOps
Push a sample image and scale Amazon ECS
Task
Description
Skills required
Push a sample image to the Amazon ECR repository.
Push a sample (NGINX) image to the Amazon Elastic Container Registry (Amazon ECR) repository named web (as set in parameters). You can customize the image as required.
To log in and set the credentials for pushing an image to Amazon ECR, follow the instructions in the Amazon ECR documentation.
where Poc-Service refers to your sample application.
Verify that the services are accessible from the Application Load Balancer by using a fully qualified domain name (FQDN) or DNS from a browser or by using the curl command.
AWS DevOps
Task
Description
Skills required
Create a CodeCommit repository in the tools account.
Create a CodeCommit repository in the tools account by using the codecommit.yaml template, which is in the code folder of the GitHub repository. You must create this repository only in the single Region where you plan to develop the code.
Create an S3 bucket for managing artifacts generated by CodePipeline.
Create an S3 bucket for managing artifacts generated by CodePipeline by using the pre-reqs-bucket.yaml template, which is in the code folder of the GitHub repository. The stacks must be deployed in all three workload (test) and tools accounts and Regions.
Create a multi-Region KMS key with primary and replica keys that CodePipeline will use. In our example, ToolsAccount1region - ap-south-1 will be the primary Region.
Set the CMKARN variables to pass to CodeBuild projects. The values are available in the output of the ecs-codepipeline-pre-reqs-KMS template stack (the key ID will be same in all Regions and starts with mrk-). Or, you can get the CMKARN values from the tools account. Export them in all account sessions:
Set up the CodeBuild project in the tools account.
Use the codebuild_IAM.yaml template from the code folder of the GitHub repository to set up AWS Identity and Access Management (IAM) for AWS CodeBuild in a single Region in the tools account:
Use the codedeploy.yaml template in the code folder of the GitHub repository to set up CodeDeploy in all three workload accounts. The output of mainInfraStack includes the Amazon Resource Names (ARNs) of the Amazon ECS cluster and Application Load Balancer listener.
Note
The values from the infrastructure stacks are exported already, so they are imported by the CodeDeploy stack templates.
Create a CodeCommit repository in the tools account.
Create a CodeCommit repository in the tools account by using the codecommit.yaml template, which is in the code folder of the GitHub repository. You must create this repository only in the single Region where you plan to develop the code.
Create an S3 bucket for managing artifacts generated by CodePipeline.
Create an S3 bucket for managing artifacts generated by CodePipeline by using the pre-reqs-bucket.yaml template, which is in the code folder of the GitHub repository. The stacks must be deployed in all three workload (test) and tools accounts and Regions.
Create a multi-Region KMS key with primary and replica keys that CodePipeline will use. In our example, ToolsAccount1region - ap-south-1 will be the primary Region.
Set the CMKARN variables to pass to CodeBuild projects. The values are available in the output of the ecs-codepipeline-pre-reqs-KMS template stack (the key ID will be same in all Regions and starts with mrk-). Or, you can get the CMKARN values from the tools account. Export them in all account sessions:
Set up the CodeBuild project in the tools account.
Use the codebuild_IAM.yaml template from the code folder of the GitHub repository to set up AWS Identity and Access Management (IAM) for AWS CodeBuild in a single Region in the tools account:
Use the codedeploy.yaml template in the code folder of the GitHub repository to set up CodeDeploy in all three workload accounts. The output of mainInfraStack includes the Amazon Resource Names (ARNs) of the Amazon ECS cluster and Application Load Balancer listener.
Note
The values from the infrastructure stacks are exported already, so they are imported by the CodeDeploy stack templates.
Clone the CodeCommit repository that was created in the codecommitrepoStack by using the git clone command, as described in the AWS CodeCommit documentation.
Update the input artifacts with the required details:
JSON file: Update AccountID in the file in three places of this file. Rename the three files to include the account IDs.
YAML files: Update the task definition ARN and version. Rename the three files to include the account IDs.
Modify the index.html file to make a few minor changes to the home page.
Copy the following files to the repository and commit:
Start or restart the pipeline and verify the results.
Access the service from the Application Load Balancer using by using an FQDN or DNS, and verify that the updates have been deployed.
Call and test the pipeline
Task
Description
Skills required
Push changes to the CodeCommit repository.
Clone the CodeCommit repository that was created in the codecommitrepoStack by using the git clone command, as described in the AWS CodeCommit documentation.
Update the input artifacts with the required details:
JSON file: Update AccountID in the file in three places of this file. Rename the three files to include the account IDs.
YAML files: Update the task definition ARN and version. Rename the three files to include the account IDs.
Modify the index.html file to make a few minor changes to the home page.
Copy the following files to the repository and commit: