In this article, we will learn about how to create a CI/CD Pipeline using AWS Services: AWS CodeCommit, AWS CodeBuild, AWS Pipeline, AWS ECS & Fargate.
Refer to these blogs, as we will use the same application and workflow
Once we have developed and deployed the application/product, It needs to be continuously updated based on user feedback or the addition of new features. This process should be automated, as without automation we have to run the same development and deployment steps/commands again and again for every change to the application.
With Continuous Integration and Continuous Delivery Pipeline, we can automate the complete workflow from building, testing, packaging, and deploying, which will be triggered when there are any changes to an existing application or we can say if there is any new commit to an existing code repository.
Source: Parasoft
Now, let’s start building the pipeline.
First, we will create an Application load balancer, which will make our application scalable and highly available.
Source: Author
Source: Author
In the previous blog, we have learned how to create a Task definition in ECS which is needed to run Fargate jobs. We will use the same task definition to create a Fargate Service. Please refer to the previous blog to understand the concepts and to follow along.
Source: Author
Source: Author
Source: Author
Source: Author
Source: Author
Source: Author
Our Service is up and running! Now we can build a CI/CD Pipeline.
It is a version control service similar to Github, hosted by AWS. For Demo, we will use a CodeCommit repository and push our application code to it (If you want you can use Github also).
Source: Author
Source: Author
Source: Author
Source: Author
git clone URL
git add .
git commit -m 'app:v1.0'
git push
Once you have pushed the code, your CodeCommit repository will look as follows
Source: Author
The next step is to set up Continuous Integration functionality on the CodeCommit repository.
AWS CodeBuild is managed CI Service which compiles source code, runs tests, and packages the source code which can be used for deployment.
To use CodeBuild, we need a buildspec.yml file that contains commands used to compile, test, and package our code. For our purpose, in the buildspec.yml file, we will write commands to containerized our application using docker and push it to ECR. It will look as follows
#Required mapping. Represents the buildspec version. version: 0.2 #Required sequence. Represents the commands CodeBuild runs during each phase of the build. phases: pre_build: commands: - echo Logging in to Amazon ECR... - CODEBUILD_RESOLVED_SOURCE_VERSION="${CODEBUILD_RESOLVED_SOURCE_VERSION:-$IMAGE_TAG}" - IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) - echo image_tag $IMAGE_TAG - REPO="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com" - IMAGE_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG" - echo Repository $REPO - docker login -u AWS -p $(aws ecr get-login-password --region $AWS_DEFAULT_REGION) $REPO build: commands: - echo Build started on `date` - echo Building the Docker image... - docker build -t $IMAGE_URI . post_build: commands: - bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi" - echo Build stage successfully completed on `date` - echo Pushing the Docker image... - docker push $IMAGE_URI - printf '[{"name":"sentiment-analysis-container","imageUri":"%s"}]' "$IMAGE_URI" > images.json artifacts: files: images.json
I won’t go much into details of it as most of the commands are docker related and syntax such as pre_build, build, etc are self-explanatory. If you want to deep dive into it refer to AWS documentation.
One thing to highlight is artifacts files, here we are saving an ‘images.json‘ file containing the container name that we have created earlier in ECS(Task definition container name) and the Image URI of the container that we will push to ECR. This artifacts file will be needed in the CodePipeline stage.
CodePipeline will automatically build and deploy our application when there are any changes in the code repository.
Source: Author
Source: Author
Source: Author
Source: Author
Now, let’s test the complete Pipeline by changing the source code and pushing the changes to the Code commit repository.
Make some changes(highlighted text in below image) in the ‘home.html’ file in the templates folder
Source: Author
Now push the changes to the repository
Source: Author
Once the changes are pushed, CodePipeline will trigger the CI/CD process and creates a new deployment of the AWS Fargate Service with the new image build. You can verify it by going to Fargate Services and check the Deployments tab.
Source: Author
Now, to verify our changes are made and deployed successfully, visit the DNS name in the browser as we did earlier
Source: Author
Hooray! We have successfully created a CI/CD Pipeline leveraging AWS services.
To clean up the AWS resources, you can delete the Service and cluster created (if you don’t have free credits).
https://aws.amazon.com/blogs/compute/building-deploying-and-operating-containerized-applications-with-aws-fargate/
https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html
Machine Learning Engineer, Solving challenging business problems through Data, Machine Learning, and Cloud.
Connect @ Linkedin
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.