Implementing a Two-Stage CodePipeline with CodeCommit and CodeDeploy
Tags: devops, cloud, aws, tutorial
Published: 2024-01-05 19:03:58


In this guide, CodePipeline is utilized to deploy code stored in a CodeCommit repository to a solitary Amazon EC2 instance. The pipeline activates upon pushing changes to the CodeCommit repository, facilitating the deployment of modifications to an Amazon EC2 instance through CodeDeploy as the designated deployment service.

**The pipeline has two stages:**
• A source stage (Source) for your CodeCommit source action.
• A deployment stage (Deploy) for your CodeDeploy deployment action.

**Step 1: Create a CodeCommit repository.**
• Navigate to the CodeCommit console and click on **create repository**.
• **Repository name**-enter a name for your repository- **MySampleRepo**
• **Description** – Add a description for the repository or leave blank.
• Choose **Create**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwxmdpoyxr7ghpu22gmh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouyaldtk2e6rmeisz2mt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieumlbuk0qmwm7i3fcx0.png)

**Step 2: Set up your local repository.**
In this step, you will set up a local repository to connect to the remote CodeCommit repository.

• Git Bash in the location that you want to set up as your local repository. (Ensure you have git installed and already created Git credentials for your IAM user -
https://docs.aws.amazon.com/console/codecommit/connect-gc-np)
• With your new repository open in the console, choose **Clone URL** on the top right of the page, and then choose **Clone HTTPS or Clone SSH** to copy the command based on the connection type. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/groc7y4m8elflnfokmhj.png)
• Run the command on your local machine (same location you Git bash earlier) to clone the repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mrdqplkc57o89ovj4bx.png)
• Copy the Sample application code to the local repository and run the following Git commands. 



```
git status - To display the status of the files
git add .   – To stage the files for tracking
git commit  – To commit the files
git push    – To push files to remote repository
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erwfo0jvlvfxkv5ds862.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1ijp9occjm5dwpww7et.png)

• Confirm that the codes have been pushed to the remote repository, Check the CodeCommit repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo8cr4z538xfj7lt0lg4.png)

**Step 3: Create Roles.**
In this step we will create 2 roles.
**1. Instance Role:** This role allows the installation and management of the code deploy agent on the instance and also allow the instance to be managed by SSM.
**To create an instance role**

- Navigate to **IAM** console and choose **Roles**.
- Choose **Create role**.
- Under Select type of trusted entity, select AWS service. 
- Under Choose a use case, select **EC2**. 
- Choose Next: Permissions.
- Search for and select the policy named- **AmazonEC2RoleforAWSCodeDeploy**.
- Search for and select the policy named-**AmazonSSMManagedInstanceCore**. 
- Choose Next: Tags.
- Choose Next: Review. Enter a name for the role- **EC2instanceprofilerole**.
- Choose **Create role**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4ltxkofah5lesl2inqi.png)
**2. CodeDeploy Service Role:** This role allows CodeDeploy to perform deployments.
**To create CodeDeploy Service role**

- Navigate to **IAM** console and choose **Roles**.
- Choose **Create role**.
- Under Select type of trusted entity, select AWS service. 
- Under Choose a use case, select **CodeDeploy**. 
- Choose Next
- Enter a name for the role- **CodedeployRole**
- Choose **Create role**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntop22wufx0l7pevkve8.png)

**Step 4: Launch Amazon EC2 Linux Instance.**
**To launch an instance**

- Navigate to Amazon EC2 console, choose **Instances**, and select **Launch instances**.
- **Name**- enter **MySamplePipelineServer**. This assigns the instance a **tag Key** of **Name** and a **tag Value** of **MySamplePipelineServer**. 
- **Application and OS Images (Amazon Machine Image)**- Select **Amazon Linux 2023 AMI** -Free tier eligible.
- **Instance type**- Choose **t2. micro** - Free tier eligible.
- **Key pair (login)**- Choose a key pair or create one. You can also choose **Proceed without a key pair**.


- **Network settings**- do the following.
**_Auto-assign Public IP_**, make sure the status is Enable.
**_Security group_**, choose Create a new security group.
**_In the row for SSH_**, under **Source type**, choose **My IP**.
**_Choose Add security group_**, choose **HTTP**, and then under **Source type**, choose **Anywhere**.
**- Advanced details-** In **IAM instance profile**, choose the IAM role you created in the previous procedure -**EC2Instanceprofilerole**
**- Summary-** under Number of instances, enter 1.
Choose **Launch instance.**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9uxg9esyanz4hj39ni6a.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rsuy98crsp51tz55nbm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hqn1uzlr6jkrdi6gfnz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szr6otcdco3gstu5s5hs.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsoulz86xb5m91d12sgq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njurzywkq9kzp882b73e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4s3od4ksp7zjfhb2m4xw.png)

**Step 5: Create and Application and Deployment Group in CodeDeploy**
**To create an application group**

- Navigate to CodeDeploy console and choose **Applications**. 
- Choose **Create application**.
- Application name- enter **MySampleApplication**.
- Compute Platform- Choose **EC2/On-premises**.
- Choose **Create application**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb30xlxijdx3okfn5mv0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8gn29869w20oxjp55au.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn5ggvmfnm6ytdwubguc.png)

**To create a deployment group in CodeDeploy**
A deployment group is a resource that defines deployment-related settings like which instances to deploy to and how fast to deploy them.

- Choose **Create deployment group**.
- **Deployment group name**- enter **MySampleDeploymentGroup**.
- **Service role**- choose the service role you created earlier- **Codedeployrole**
- **Deployment type**- choose **In-place**.
- **Environment configuration**- choose Amazon **EC2 Instances**. In the Key field, enter **Name**. In the Value field, enter the name you used to tag the instance- **MySamplePipelineServer**.
- **Agent configuration with AWS Systems Manager**- choose **Now and schedule updates**. 
- **Deployment configuration**- choose **CodeDeployDefault.OneAtaTime**.
- **Load Balancer**- make sure Enable load balancing is not selected. We do not need  a load balancer or a target group for this project.
- Choose **Create deployment group**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2t7viqk70e7hyro1ve98.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egftby0dz9cr26agaqw7.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jd2vm05xdkwjd6uekmo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jfqdjb1jl1rg7xqhttx.png)
Step 6: Create a CodePipeline Pipeline
In this step, you create a pipeline that runs automatically when code is pushed to your CodeCommit repository.

**To create a CodePipeline pipeline**

- Navigate to CodePipeline  console and choose **Create pipeline**.
- Choose pipeline settings, in **Pipeline name**- enter **MySamplePipeline**.
- **Service role**- choose **New service role** to allow CodePipeline create a service role in IAM.
- Leave the settings under Advanced settings at their defaults, and then choose **Next**.
- Add source stage, in **Source provider**- choose **CodeCommit**. In **Repository name**- choose the name of the CodeCommit repository you created in Step 1- **MySampleRepo**. In **Branch name**- choose **master**, and then choose **Next step**.
- **Change detection options**- leave the defaults. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository.
- Choose **Next**.
- Add build stage, choose **Skip build stage**, and then accept the warning message by choosing **Ok**. Choose **Next**.
- Add deploy stage, in **Deploy provider**- choose **CodeDeploy**. In **Application name**- choose **MySampleApplication**. In **Deployment group**- choose **MySampleDeploymentGroup**, and then choose **Next step**.
- Review, review the information, and then choose **Create pipeline**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/url1yybh2371qu5skln7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwevwnvyx1ur5np5ysct.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/soltwf8mo7fij93lo4s1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo8b1a8u62cqwpchta9v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yrg597uybvs195kly4m.png)

The pipeline starts running after it is created. It downloads the code from your CodeCommit repository and creates a CodeDeploy deployment to your EC2 instance


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z087qgr8hrrqw1a0z4dc.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpgfhof2s3cdnihmsfxl.png)

**To verify that your pipeline ran successfully**

- Navigate to the Instance console, copy and paste the public DNS of the instance into the address bar of your web browser.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8jcukhh9k7lab78dmxe.png)

- The web page displays for the sample application you downloaded and pushed to your CodeCommit repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy59icx2n0f03brsmcpw.png)
Congratulations! You just created a simple pipeline in CodePipeline.

**Step 6: Modify the code in your CodeCommit Repository**
Your pipeline is configured to run whenever code changes are made to your CodeCommit repository. In this step, you make changes to the HTML file that is part of the sample CodeDeploy application in the CodeCommit repository. When you push these changes, your pipeline runs again, and the changes you make are visible at the web address you accessed earlier.

- Navigate to the CodeCommit Repository 
- Open the **Index.html** file and click Edit (Change the background colour to **#FF0000** “red” and  “Congratulations” to  **MySampleCodePipeline**.
- Commit changes to Master by adding Name, email and commit message
- Click commit changes
This will automatically trigger CodePipeline to deploy the new version of the application.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jzk15ez5qozmgbkgnuf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9shxv0ett8vrcdhnqkky.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkyf95j49siejkg3g8e3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0bk9khhlkm23urd2i6k.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdx5bno66dzcb54kjjz7.png)
Refresh your browser to see the new changes made to your application.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym7idowhw18mcshfjq42.png)

**Step 7: Clean Up**
First, delete the pipeline, then the CodeDeploy application and its associated Amazon EC2 instance, and finally, the CodeCommit repository.

Thanks for Reading!!!