AWS CI / CD Automation

Veerasolaiyappan
Cloudnloud Tech Community

--

In today’s fast-paced software development world, having an efficient and automated CI/CD (Continuous Integration/Continuous Deployment) pipeline is essential. AWS provides a robust set of services that enable developers to easily set up and manage CI/CD pipelines. In this blog post, we will walk through the process of creating a CI/CD pipeline using AWS CloudFormation, CodePipeline, and Amazon ECR (Elastic Container Registry). We will focus on building and deploying Docker images to an ECR repository. Let’s get started!

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • An AWS account with appropriate permissions to create and manage resources.
  • The AWS CLI (Command Line Interface) installed and configured on your local machine.

AWS CI / CD Services

  • AWS CloudFormation: Infrastructure as Code (IaC) service for provisioning and managing AWS resources using declarative templates.
  • AWS CodeCommit: Fully managed source code control service that enables secure hosting of private Git repositories.
  • AWS CodeBuild: Fully managed build service that compiles source code, runs tests, and produces software packages.
  • AWS CodeDeploy: Automated deployment service for deploying applications to EC2 instances, on-premises servers, or Lambda functions.
  • AWS CodePipeline: Continuous delivery service that orchestrates and automates the release process for applications.

Step 1: Prepare the CloudFormation Template

The first step is to prepare a CloudFormation template that defines the infrastructure resources for our CI/CD pipeline.

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
RepositoryName:
Type: String
Description: Name of the ECR repository
SourceBranch:
Type: String
Description: Branch to trigger the pipeline
Resources:
Repository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Ref RepositoryName
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Type: S3
Location: my-pipeline-artifacts
RoleArn: !GetAtt PipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: SourceAction
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeCommit
Configuration:
RepositoryName: <Your CodeCommit Repository Name>
BranchName: !Ref SourceBranch
OutputArtifacts:
- Name: SourceArtifact
RunOrder: 1
- Name: Build
Actions:
- Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: <Your CodeBuild Project Name>
InputArtifacts:
- Name: SourceArtifact
OutputArtifacts:
- Name: BuildArtifact
RunOrder: 2
- Name: Deploy
Actions:
- Name: DeployAction
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: ECR
Configuration:
RepositoryName: !Ref RepositoryName
ImageTag: latest
InputArtifacts:
- Name: BuildArtifact
RunOrder: 3
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: ECRPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ecr:GetAuthorizationToken
- ecr:BatchCheckLayerAvailability
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:PutImage
- ecr:InitiateLayerUpload
- ecr:UploadLayerPart
- ecr:CompleteLayerUpload
Resource: !Sub arn:aws:ecr:*:*:repository/${RepositoryName}
Outputs:
PipelineName:
Value: !Ref Pipeline

Step 2: Execute the CloudFormation Template

Once you have the CloudFormation template, follow these steps to execute it:

  1. Open a terminal or command prompt on your local machine.
  2. Navigate to the directory where you saved the CloudFormation template.
  3. Execute the following AWS CLI command to create the CloudFormation stack:
aws cloudformation create-stack --stack-name MyPipelineStack --template-body file://pipeline-template.yaml --parameters ParameterKey=RepositoryName,ParameterValue=<YourECRRepositoryName> ParameterKey=SourceBranch,ParameterValue=<YourSourceBranch>

Replace <YourECRRepositoryName> with the desired name for your ECR repository, and <YourSourceBranch> with the branch that will trigger the pipeline (e.g., "master" or "main").

  1. Wait for the CloudFormation stack creation to complete. You can check the status by running the following command:
aws cloudformation describe-stacks --stack-name MyPipelineStack

Step 3: Verify and Test the CI/CD Pipeline

Once the CloudFormation stack creation is complete, it’s time to verify and test our CI/CD pipeline. Here’s how you can do it:

  1. Open the AWS Management Console and navigate to the CodePipeline service.
  2. Locate the pipeline created by the CloudFormation stack. It should have a name like “MyPipelineStack-Pipeline-XXXXXXXXX”.
  3. Click on the pipeline to open its details.
  4. Review the pipeline stages and actions. It should include a “Source” stage for CodeCommit, a “Build” stage for CodeBuild, and a “Deploy” stage for ECR.
  5. Make a change to your application code and push the changes to the configured CodeCommit repository. This will trigger the pipeline and initiate the build and deployment process.
  6. Monitor the pipeline execution in the CodePipeline console. You will be able to see the progress and any potential issues.
  7. Once the pipeline completes successfully, navigate to your ECR repository in the AWS Management Console. You should see the newly built Docker image in the repository.

Congratulations! You have successfully set up a CI/CD pipeline for Docker images using AWS CloudFormation, CodePipeline, and ECR. You can now automate the build and deployment process for your Docker-based applications, ensuring faster and more reliable releases.

Hope this article is helpful to you

Follow Veerasolaiyappan for the more insightful knowledge content

--

--