Simple CI/CD CodePipeline to deploy code on EC2

Rasanpreet
AWS Tip
Published in
7 min readSep 29, 2022

--

Overview

We’ll create an AWS CodePipeline in this post to deploy code from a CodeCommit repository to a single Amazon EC2 instance. When you push a change to the CodeCommit repository, the pipeline gets started. The pipeline uses CodeDeploy as the deployment service to deploy your changes to an Amazon EC2 instance.

Pipeline components

The AWS pipeline includes the following elements:

· AWS Codecommit

· AWS CodeDeply

· AWS CodePipeline

· AWS Artifact Bucket

AWS CodePipeline Architecture

Pre-requisites

1. One EC2 is required. Verify that the Apache web server and CodeDeploy agent are set up and running on that EC2 instance. A software package called the CodeDeploy agent enables an instance to be used in CodeDeploy deployments. Refer to the instructions for installing the CodeDeploy agent at https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html.

2. Ensure that the EC2 instance should have EC2 tags (e.g., Key=Name, Value=CodeDeploy). CodeDeploy will look for EC2 instances with this tag in order to deploy the application. Open port 80 and port 22 for the EC2 instance’s security group as well.

3. An IAM role with AmazonEC2RoleforAWSCodeDeploy and AmazonSSMManagedInstanceCore managed policy attached to the EC2 instance.

Steps walkthrough

Create a CodeCommit repository

In CodeCommit, you first establish a new repository. When your pipeline executes, it pulls source code from this repository. Before pushing code to the CodeCommit repository, you also set up a local repository where you may update and manage the code.

  1. From the AWS Console, access the CodeCommit Dashboard. Select Create repository from the Repositories page.

2. On the Create repository screen, select Create after giving your repository a name (for instance, demo-repo).

To set up a local repository

For setting up the authentication with the new CodeCommit repository please follow the link https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html?icmpid=docs_acc_console_connect_np

The local repository copy will now be configured to sync with the remote CodeCommit repository.

1. Run the following command, it will create a directory called demo-repo and clone the empty repository.

git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/demo-repo

2. Download and unzip the sample application code in the empty repository directory (i.e., demo-repo) using the S3 link:http://s3.amazonaws.com/aws-codedeploy-us-east-1/samples/latest/SampleApp_Linux.zip

3. After adding the sample code, using the following git commands, this will upload the code to the remote CodeCommit repo.

4. Next, make that your sample code files have been uploaded by visiting your repository in the AWS interface.

Creating a CodeDeploy service role

For CodeDeploy to work, we need to create and assign a service role with the appropriate permissions. Let’s build a role and add AWSCodeDeployRole managed policy to it. This role will make deployments possible for CodeDeploy.

  1. Select create a role from the IAM console. AWS service should be selected under Select trusted entity. Select CodeDeploy under “Use case”.

2. The role already has the AWSCodeDeployRole manage policy attached. Select Next.

3. Enter a name(e.g., CodeDeployRole) for the role, and then choose to Create role. After successful role creation will have the below screen.

Application setup in CodeDeploy

An application in CodeDeploy is a resource that contains the software you want to deploy. Later, you use this application with CodePipeline to automate deployments of the sample application to the Amazon EC2 instance.

  1. First Select Create application from the applications menu on the CodeDeploy Service page of the AWS Console.

2. Since we will be deploying the application in Ec2, mention the CodeDeploy name(e.g., EC2-CodeDeploy) and choose Ec2/On-premises as a compute platform. after which select Create Application.

Create a deployment group

A deployment group is a resource that specifies parameters relating to deployments, such as which instances to deploy to and how quickly.

  1. Select Create deployment group on the screen that shows your application.

2. Type MyDeploymentGroup into the Deployment group name field. Select the service role you created in the preceding step under Service role (for example, CodeDeployRole).

3. Select In-place under Deployment type. Make the Amazon EC2 Instances selection under Environment configuration. In the Tag group, Enter the tags you assigned to the EC2 instance (i.e., Key= Name, Value= CodeDeploy).

4. Under Agent configuration, choose the Never option if the code-deploy agent has already been deployed on an EC2 server; otherwise, choose another option to have the System Manager install the agent on your target servers automatically.

5. Under Deployment configuration, choose CodeDeployDefault.OneAtaTime. Make sure Enable load balancing is not chosen under the Load Balancer option for this example. Choose to Create deployment group.

6. After successful configuration, the screen below will appear.

Configure CodePipeline for deployment

Your first pipeline can now be created and run. This stage involves building a pipeline that launches whenever new code is pushed to your CodeCommit repository.

  1. Open the CodePipeline console in AWS Console and select Create pipeline.

2. Select New Service Role and type the name of the pipeline (e.g., first-pipeline). Select Next after leaving the Advanced settings default values in place.

3. Select AWS CodeCommit under the Source provider in the source stage. Select the name of the repository that we previously created with the Repository name (e.g., demo-repo). Select master under Branch name. Keep the default settings under Change detection options. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository, and then choose Next step.

4. Select Skip build stage under Add build stage, then select Next. If necessary, you can set up CodeBuild later to do integration tests or create binaries.

5. Select AWS CodeDeploy under Deploy provider in Add Deploy Stage. Select EC2-CodeDeploy under the Application name. Select MyDeploymentGroup under Deployment group, then select Next step.

6. After reviewing the data in the Review stage, select Create pipeline.

7. Next, the pipeline starts running after it finishes the creation step. It downloads the code from the CodeCommit repository and deploys it to the Ec2 instance using CodeDeploy deployment. You can view progress and success and failure messages on AWS CodePipeline Console.

8. Next, verify the results. Copy the Public DNS address of EC2 and open it in your web browser. The web page displays the sample application page that is pushed to your CodeCommit repository.

We have just built a straightforward CICD pipeline using AWS CodePipeline. Now the pipeline will run automatically whenever code changes are made to the CodeCommit repository and deploy the code changes in Amazon EC2.

--

--

AWS Ambassador & Community Builder. He enjoys creating Cloud infra with IaC & building CI/CD pipelines. https://www.linkedin.com/in/rasanpreet/