Code Deployment Automation with AWS CodeCommit & CodeDeploy


Automate Code Deployment with AWS CodeCommit & CodeDeploy

As a web developer, your work may include several complex tasks such as code deployment automation to production servers or if you work in a group and face issues while uploading your code to a live website, then this blog post is a solution to overcome all such issues.

When using FTP, you must have overwritten the code of your co-developer, an issue that is quite common and happens with most of us. This guide will show you the code deployment automation with AWS CodeCommit, CodeDeploy, and Pipeline services.

The Technological Overview: 

  1. AWS CodeCommit: We will use CodeCommit to maintain our repositories (Web Content/Code Files) that will hold all project files and commit logs.
  2. EC2 Server: We will use EC2 as our production server, it will contain the live site and code for your website visitors.
  3. AWS CodeDeploy: It will hold the application to publish your code to the EC2 server. We will also create a Deployment Group that will hold the name/tag of the targeted EC2 instance where we wish to publish our code.
  4. Pipeline: We will select CodeCommit as our source and CodeDeploy as our deploy stage. Therefore, whenever any code changes are detected in CodeCommit, the pipeline will run and push those code changes to CodeDeploy (EC2 Instance).

Code Deployment Automation: Explained in Detail:

Creating an IAM and Git User for Code Deployment Automation

We will first need to create an IAM user and Git User that will make the code changes and publish them on the deployment server.

  1. Create an IAM User
    1. Allow Access to: AWSCodeCommitFullAccess, AWSCodeDeployFullAccess, AWSCodeCommitPowerUser. You will find this option in the “Attach existing policies directly” tab.
  2. Now, log in with this IAM user.
  3. Click on Security Credentials from the top right corner.
  4. Click on the AWS CodeCommit credentials tab.
  5. Under the “HTTPS Git credentials for AWS CodeCommit” section, click on the Generate Credential button.
  6. Now, copy that username and password in a safe place. This will be your Git’s main username and password to push/pull code changes.


Creating a Repository in CodeCommit

Now, let’s create a repository that will hold your exact code.

  1. Go to CodeCommit Dashboard from the IAM User.
  2. At the top right corner, select the region where you want to create your repository.
  3. Click on Create repository, give it a name, description, and click on create.
  4.  Under the “Clone the repository” section, you will see a URL of your repository. Save it in a safe place. It will look like:


Creating a Repository in CodeCommit

Uploading your project file for the first time

  1. Open the command prompt of your local system where your original code resides.
  2. In the command prompt, navigate to the path where your code resides.
  3. Now, run the following commands to push your code to the AWS repository for the first time:

git init
git config –local init.defaultBranch main
git config –global init.defaultBranch main
git add . ## this will commit all files of the project
git commit –m “Initial commit”
git push –all

Uploading your project file for the first time

  • Create EC2 Instance & Install CodeDeploy Agent
  1. Create an Instance role
    1. Open your AWS IAM Console.
    2. Go to the Roles tab >> Create role.
    3. Under Select type of trusted entity, select AWS service. Under ‘Choose a use case’, select EC2. Under ‘Select your use case’, choose EC2. Choose Next: Permissions.
    4. Search for and select the policy named AmazonEC2RoleforAWSCodeDeploy, and then choose Next: Tags.
    5. Choose Next: Review. Enter a name for the role (for example, EC2InstanceRole) and click on Create.
  2. Create an EC2 instance
    1. Go to the EC2 console. Click on Launch Instance.
    2. Choose an Amazon Machine Image (AMI), locate Amazon Linux 2 AMI (HVM), SSD Volume Type, and then choose Select. (This AMI is labelled “Free tier eligible” and can be found at the top of the list.)
    3. Choose an Instance Type page, choose the free tier eligible t2.micro type as the hardware configuration for your instance, and then choose Next: Configure Instance Details.
    4. Configure Instance Details page, do the following:
      1. In the Number of instances, enter 1.
      2. In Auto-assign Public IP, choose Enable.
      3. In the IAM role, choose the IAM role you created in the previous procedure (for example, EC2InstanceRole).
      4. Expand Advanced Details, and in the User data field, enter the following:

yum -y update
yum install -y ruby
yum install -y aws-cli
cd /home/ec2-user
chmod +x ./install
./install auto

  1. Leave the rest of the items on Step 3: Configure Instance Details page unchanged. Choose Next: Add Storage.
  2. Leave Step 4: Add Storage page unchanged, and then choose Next: Add Tags.
  3. Choose Add Tag. In Key, enter Name, and in Value, enter MyCodePipelineDemo. Choose Next: Configure Security Group. Later, you create a CodeDeploy application that deploys the sample application to this instance. CodeDeploy selects instances to deploy based on the tags that are attached to instances.
  4. Configure Security Group page, do the following:
      • Next to Assign a security group, select Create a new security group.
      • In the row for SSH, under Source, choose My IP.
      • Choose Add Rule, choose HTTP, and then under Source, choose My IP
  5. Choose Review and Launch.
  6. On the Review Instance Launch page, choose Launch. When prompted for a key pair, select ‘Proceed without a key pair’.
  7. Choose View Instances to close the confirmation page and return to the console.
  8. You can view the status of the launch on the Instances page. When you launch an instance, its initial state is ‘pending’. After the instance starts, its state changes to ‘running’, and it receives a public DNS name. (If the Public DNS column is not displayed, choose the Show/Hide icon, and then select Public DNS.)
  9. It can take a few minutes for the instance to be ready for you to connect to it. View the information in the Status Checks column to see if your instance has passed its status checks.

Create an EC2 Instance

Create an application in CodeDeploy

  • Create a CodeDeploy service role
    • Open the IAM console at
    • From the console dashboard, choose Roles.
    • Choose to Create role.
    • Under Select type of trusted entity, select AWS service. Under Choose a use case, select CodeDeploy. Under Select your use case, choose CodeDeploy. Choose Next: Permissions. The AWSCodeDeployRole managed policy is already attached to the role.
    • Choose Next: Tags, and Next: Review.
    • Enter a name for the role (for example, CodeDeployRole), and then choose to Create role.
  • Create an application in CodeDeploy
    • Open the CodeDeploy console at
    • If the Applications page does not appear, on the menu, choose Applications.
    • Choose to Create application.
    • In the Application name, enter MyDemoApplication.
    • In Compute Platform, choose EC2/On-premises.
    • Choose to Create an application.

    Create an application in CodeDeploy

  • Create a deployment group in CodeDeploy
    • On the page that displays your application, choose to Create deployment group.
    • In the Deployment group name, enter MyDemoDeploymentGroup.
    • In-Service Role, choose the service role you created earlier (for example, CodeDeployRole).
    • Under Deployment type, choose In-place.
    • Under 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 (for example, MyCodePipelineDemo).
    • Under Deployment configuration, choose CodeDeployDefault.OneAtaTime.
    • Under Load Balancer, make sure Enable load balancing is not selected. You do not need to set up a load balancer or choose a target group for this example.
    • Expand the Advanced section. Under Alarms, if any alarms are listed, choose Ignore alarm configuration.
    • Choose to Create deployment group.

Create a deployment group in CodeDeploy

Create a Pipeline

    • Create a Pipeline
    • Sign in to the AWS Management Console and open the CodePipeline console at
    • Open the CodePipeline console at
    • Select Create pipeline.
    • In Step 1: Select pipeline settings, in the Pipeline name, enter MyFirstPipeline.
    • In the Service role, choose the New service role to allow CodePipeline to create a service role in IAM.
    • Leave the settings under Advanced settings at their defaults, and then select Next.
    • In Step 2: Add source stage, in Source provider, select CodeCommit.
      In Repository name, select the name of the CodeCommit repository you created in step 1: Create a CodeCommit repository. In Branch name, choose main, and then select Next step.
    • After you select the repository name and branch, a message displays the Amazon CloudWatch Events rule to be created for this pipeline.
    • Under ‘Change detection’ options, leave the defaults. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository.
    • Select Next.
    • In Step 3: Add build stage, select Skip build stage, and then accept the warning message by choosing Skip again. Select Next.

    Note: In this tutorial, you are deploying code that requires a no-build service, so you can skip this step. However, if your source code needs to be built before it is deployed to instances, you can configure CodeBuild in this step.

    • In Step 4: Add deploy stage, in Deploy provider, select CodeDeploy.
      In the Application name, select MyDemoApplication. In the Deployment group, select MyDemoDeploymentGroup, and then select Next step.
    • In Step 5: Review the information and then select Create pipeline.
    • 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. You can view progress, ‘success’ and ‘failure’ messages as the CodePipeline sample deploys the webpage to the Amazon EC2 instance in the CodeDeploy deployment.

Create a Pipeline

You are all set now! Go to Deploy Deployments.  You will see the logs of all deployments that are successful and the ones that have failed.

To verify the changes on the site, open your EC2 Instance, search for your public DNS, which will appear as:

Once you browse it, you will see your live pages.

Create an Option Group

You have successfully completed the Code Deployment Automation. You can now push new code changes to your repository and publish them on your site automatically.  If you still face any issues, feel free to comment here.

(Visited 616 times, 1 visits today)

Leave a Reply

AlphaOmega Captcha Classica  –  Enter Security Code

This site uses Akismet to reduce spam. Learn how your comment data is processed.