Set up a CI/CD pipeline for hybrid workloads on Amazon ECS Anywhere by using AWS CDK and GitLab
Created by Dr. Rahul Sharad Gaikwad (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
Amazon ECS Anywhere is an extension of the Amazon Elastic Container Service (Amazon ECS). It provides support for registering an external instance, such as an on-premises server or virtual machine (VM), to your Amazon ECS cluster. is feature helps reduce costs and mitigate complex local container orchestration and operations. You can use ECS Anywhere to deploy and run container applications in both on-premises and cloud environments. It removes the need for your team to learn multiple domains and skill sets, or to manage complex software on their own.
This pattern describes a step-by-step approach to provision an Amazon ECS cluster with Amazon ECS Anywhere instances by using Amazon Web Services (AWS) Cloud Development Kit (AWS CDK) stacks. You then use AWS CodePipeline to set up a continuous integration and continuous deployment (CI/CD) pipeline. Then, you replicate your GitLab code repository to AWS CodeCommit and deploy your containerized application on the Amazon ECS cluster.
This pattern is designed to help those who use on-premises infrastructure to run container applications and use GitLab to manage the application code base. You can manage those workloads by using AWS Cloud services, without disturbing your existing, on-premises infrastructure.
Prerequisites and limitations
Prerequisites
An active AWS account.
A container application running on on-premises infrastructure.
A GitLab repository where you manage your application code base. For more information, see Repository
(GitLab). AWS Command Line Interface (AWS CLI), installed and configured. For more information, see Installing or updating the latest version of the AWS CLI (AWS CLI documentation).
AWS CDK Toolkit, installed and configured globally. For more information, see Install the AWS CDK (AWS CDK documentation).
npm, installed and configured for the AWS CDK in TypeScript. For more information, see Downloading and installing Node.js and npm
(npm documentation).
Limitations
For limitations and considerations, see External instances (Amazon ECS Anywhere) in the Amazon ECS documentation.
Product versions
AWS CDK Toolkit version 2.27.0 or later
npm version 7.20.3 or later
Node.js version 16.6.1 or later
Architecture
Target technology stack
AWS CDK
AWS CloudFormation
AWS CodeBuild
AWS CodeCommit
AWS CodePipeline
Amazon ECS Anywhere
Amazon Elastic Container Registry (Amazon ECR)
AWS Identity and Access Management (IAM)
AWS System Manager
GitLab repository
Target architecture
This diagram represents two primary workflows described in this pattern, provisioning the Amazon ECS cluster and setting up the CI/CD pipeline that sets up and deploys the CI/CD pipeline, as follows:
Provisioning the Amazon ECS cluster
When you deploy the first AWS CDK stack, it creates a CloudFormation stack on AWS.
This CloudFormation stack provisions an Amazon ECS cluster and related AWS resources.
To register an external instance with an Amazon ECS cluster, you must install AWS Systems Manager Agent (SSM Agent) on your VM and register the VM as an AWS Systems Manager managed instance.
You must also install the Amazon ECS container agent and Docker on your VM to register it as an external instance with the Amazon ECS cluster.
When the external instance is registered and configured with the Amazon ECS cluster, it can run multiple containers on your VM, which is registered as an external instance.
The Amazon ECS cluster is active and can run the application workloads through containers. The Amazon ECS Anywhere container instance runs in on-premises environment but is associated with the Amazon ECS cluster in the cloud.
Setting up and deploying the CI/CD pipeline
When you deploy the second AWS CDK stack, it creates another CloudFormation stack on AWS.
This CloudFormation stack provisions a pipeline in CodePipeline and related AWS resources.
You push and merge application code changes to an on-premises GitLab repository.
The GitLab repository is automatically replicated to the CodeCommit repository.
The updates to the CodeCommit repo automatically starts CodePipeline.
CodePipeline copies code from CodeCommit and creates the deployable application build in CodeBuild.
CodePipeline creates a Docker image of the CodeBuild build environment and pushes it to the Amazon ECR repo.
CodePipeline initiates CodeDeploy actions that pull the container image from the Amazon ECR repo.
CodePipeline deploys the container image on the Amazon ECS cluster.
Automation and scale
This pattern uses the AWS CDK as an infrastructure as code (IaC) tool to configure and deploy this architecture. AWS CDK helps you orchestrate the AWS resources and set up Amazon ECS Anywhere and the CI/CD pipeline.
Tools
AWS services
AWS Cloud Development Kit (AWS CDK) is a software development framework that helps you define and provision AWS Cloud infrastructure in code.
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 CodePipeline helps you quickly model and configure the different stages of a software release and automate the steps required to release software changes continuously.
AWS Command Line Interface (AWS CLI) is an open-source tool that helps you interact with AWS services through commands in your command-line shell.
Amazon Elastic Container Registry (Amazon ECR) is a managed container image registry service that’s secure, scalable, and reliable.
Amazon Elastic Container Service (Amazon ECS) is a fast and scalable container management service that helps you run, stop, and manage containers on a cluster. This pattern also uses Amazon ECS Anywhere, which provides support for registering an on-premises server or VM to your Amazon ECS cluster.
Other tools
Node.js
is an event-driven JavaScript runtime environment designed for building scalable network applications. npm
is a software registry that runs in a Node.js environment and is used to share or borrow packages and manage deployment of private packages. Vagrant
is an open-source utility for building and maintaining portable virtual software development environments. For demonstration purposes, this pattern uses Vagrant to create an on-premises VM.
Code repository
The code for this pattern is available in the GitHub CI/CD pipeline for Amazon ECS Anywhere using AWS CDK
Best practices
Consider the following best practices when deploying this pattern:
Epics
Task | Description | Skills required |
---|---|---|
Verify the AWS CDK version. | Verify the version of the AWS CDK Toolkit by entering the following command.
This pattern requires version 2.27.0 or later. If you have an earlier version, follow the instructions in the AWS CDK documentation to update it. | DevOps engineer |
Verify the npm version. | Verify the version of npm by entering the following command.
This pattern requires version 7.20.3 or later. If you have an earlier version, follow the instructions in the npm documentation | DevOps engineer |
Set up AWS credentials. | Set up AWS credentials by entering the
| DevOps engineer |
Task | Description | Skills required |
---|---|---|
Clone the AWS CDK code repository. |
| DevOps engineer |
Bootstrap the environment. | Deploy the CloudFormation template to the account and AWS Region that you want to use by entering the following command.
For more information, see Bootstrapping in the AWS CDK documentation. | DevOps engineer |
Task | Description | Skills required |
---|---|---|
Install the package dependencies and compile the TypeScript files. | Install the package dependencies and compile the TypeScript files by entering the following commands.
These commands install all the packages from the sample repository. For more information, see npm ci | DevOps engineer |
Build the project. | To build the project code, enter the following command.
For more information about building and deploying the project, see Your first AWS CDK app in the AWS CDK documentation. | DevOps engineer |
Deploy the Amazon ECS Anywhere infrastructure stack. |
| DevOps engineer |
Verify stack creation and output. |
| DevOps engineer |
Task | Description | Skills required |
---|---|---|
Set up your VM. | Create a Vagrant VM by entering the | DevOps engineer |
Register your VM as an external instance. |
This sets up your VM was an Amazon ECS Anywhere external instance and registers the instance in the Amazon ECS cluster. For more information, see Registering an external instance to a cluster | DevOps engineer |
Verify the status of Amazon ECS Anywhere and the external VM. | To verify whether your VM is connected to the Amazon ECS control plane and running, use the following commands.
| DevOps engineer |
Task | Description | Skills required |
---|---|---|
Create a branch in the CodeCommit repo. | Create a branch named
| DevOps engineer |
Set up repo mirroring. | You can mirror a GitLab repository to and from external sources. You can select which repository serves as the source. Branches, tags, and commits are synced automatically. Set up a push mirror between the GitLab repository that hosts your application and the CodeCommit repository. For instructions, see Set up a push mirror from GitLab to CodeCommit NoteBy default, mirroring automatically syncs the repository. If you want to manually update the repositories, see Update a mirror | DevOps engineer |
Deploy the CI/CD pipeline stack. | Deploy the
| DevOps engineer |
Test the CI/CD pipeline. |
| DevOps engineer |
Task | Description | Skills required |
---|---|---|
Clean up and delete the resources. | After you walk through this pattern, you should remove the proof-of-concept resources you created. To clean up, enter the following commands.
| DevOps engineer |
Troubleshooting
Issue | Solution |
---|---|
Errors about missing packages when installing package dependencies. | Enter one of the following commands to resolve missing packages.
or
|
When you run the
| The |
An Amazon ECS health check returns
| Restart the Amazon ECS agent on your Vagrant VM by entering the following commands.
|
Related resources
Amazon ECS Anywhere demo
(video) Amazon ECS Anywhere workshop samples
(GitHub) Repository mirroring
(GitLab documentation)