DEV Community

SANDESH D MANOCHARYA
SANDESH D MANOCHARYA

Posted on

Deploying a Sample HTML Application to AWS EC2 with CodePipeline, CodeDeploy, and GitHub

Step 1: Create IAM Roles

Create two IAM Roles. One for the service AWS EC2 and another for the service AWS CodeDeploy.

Go to the service "IAM", select "Roles" in the left panel and click on "Create role" on the right top.

Image description

First let us create a role "Role_EC2CodeDeploy" for the service EC2.

To do so ensure that "AWS service" is selected.

From the dropdown of "Use case", under "Commonly used services", select the service or use case "EC2".

Image description

Image description

Click "Next" and "Next"

Give a name to the role as "Role_EC2CodeDeploy" and click on "Create role".

Image description

Either during the role creation or after the role creation we need to add permissions by searching CodeDeploy in the search bar and selecting the policy "AmazonEC2RoleforAWSCodeDeploy".

Image description

A new role is created. I am going to use this role on an EC2 machine. This role allows the service AWS EC2 to access another service AWS CodeDeploy.

Image description

Similarly let us create another role "Role_CodeDeploy" for the service CodeDeploy.

But this time instead of EC2, select the service or use case "CodeDeploy".

Image description

Image description

It will take the permission policy "AWSCodeDeployRole" automatically. I am going to use this role on CodeDeploy.

Image description

Give a name "Role_CodeDeploy" to the role and click on "Create role".

Image description

Now both the roles are created.

Image description

Step 2: Create EC2 Instance and Attach the Role "Role_EC2CodeDeploy"

We can launch any number of instances based on our requirements. But I will launch only one instance "Demo_AWSCodeDeploy" in this example.

I launched an Amazon Linux 2023 AMI t2.micro EC2 instance.

Make sure that you have added the ports 22 and 80 as inbound rules.

Under "Advanced details", from the IAM instance profile dropdown, select the role "Role_EC2CodeDeploy" which was created recently for the service EC2.

Under "Advanced details" itself scroll down and in the "User data" section paste the following script to automatically install all the packages or dependencies immediately after launching the EC2 instance.

_#!/bin/bash
sudo yum -y update
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
sudo yum install -y python-pip
sudo pip install awscli
_

Click on "Launch instance".

Image description

The EC2 instance "Demo_AWSCodeDeploy" is launched and running successfully.

Image description

If we click on the instance ID and see the details of the instance then we can see the IAM role "Role_EC2CodeDeploy" attached.

Image description

That is all fine. But I want to try each command individually. So I do not use the user data script.

Connect to the instance through Git Bash or any other CLI of your choice.

Image description

First let us update the package lists.

sudo yum -y update

Image description

Now let us install necessary packages.

sudo yum -y install ruby

Image description

sudo yum -y install wget

Image description

Let us create a project directory and navigate to it.

_mkdir -p /home/ec2-user/Projects/GitHub_CodeDeploy

cd /home/ec2-user/Projects/GitHub_CodeDeploy

ls -la_

Image description

Now let us download CodeDeploy agent installer.

_wget

Image description

ls -la

Image description

Let us make the installer executable.

sudo chmod +x ./install

Image description

Let us install the CodeDeploy agent in auto mode.

sudo ./install auto

Image description

Let us install pip

sudo yum install -y python-pip

Image description

Let us install awscli

sudo pip install awscli

Image description

Let us confirm the installation of awscli

aws - version

Image description

Step 3: Create an Application and Deployment Group under CodeDeploy

Go to the service "CodeDeploy", select "Applications" in the left side bar and click on "Create application".

Image description

Give a name "WebsiteForDevOpsStuffs" for the application.

Select "EC2" from Compute platform dropdown.

Click on "Create application".

Image description

An application "WebsiteForDevOpsStuffs" is created.

Click on "Create deployment group".

Image description

Give the name "DevOpsStuffsDeploymentGroup" to the deployment group.

Attach the role "Role_CodeDeploy" to the service "AmazonCodeDeploy" by selecting "Role_CodeDeploy" from the "Service role" dropdown.

Image description

Select "In place" as "Deployment type".

Select "Amazon EC2 instances" as "Environment configuration".

Image description

In the Tag group, select "Name" and name of the newly created EC2 instance as value from dropdowns.

Image description

Leave all other settings as their default values.

Disable "Load balancer".

Click on "Create deployment group".

Image description

Deployment group "DevOpsStuffsDeploymentGroup" was created successfully.

Image description

Step 4: Create a Pipeline and Integrate GitHub with CodePipeline

Go to the left panel, expand "CodePipeline" and and click on Pipelines"

Image description

Click on "Create pipeline", give a name "DevOpsStuffsPipeline" to it, and leave the rest of the things default. By default it stores the artifacts in S3.

Then click "Next".

Image description

Select GitHub (Version 2) from dropdown, paste the connection link if you already have one or click on "Connect to GitHub" to create one.

Image description

Select repository name and branch name from dropdowns.

Select "No filter" for specifying how you want to trigger the pipeline and leave rest as default.

Image description

Click on "Skip build stage" since we are not building the code in this project.

Image description

Select "AWS CodeDeploy" from the dropdown for "Deploy Provider".

Click on "Next".

Image description

It will automatically take Region, select the application and the deployment group from dropdowns.

Click on "Next".

Image description

Review all the things and click on "Create pipeline".

Image description

Now deployment will be started using AWS CodeDeploy. If we would have specified the number of EC2 instances as 4 during the time of launch in Step 2 then the application would have been deployed on all 4 EC2 instances now.

First the code will be checked out from GitHub repo.

Image description

After some time code or application will be deployed on EC2 instance or server.

Image description

You can click on "View details" to see the summary. As the code will be stored in S3 bucket by default, you can go to the service "AWS S3" and see the bucket.

Image description

Step 5: Access the Application

Let us access the application on port 80 as already we have added the inbound rule 80 in the EC2 instance.

Copy the Public DNS of the EC2 instance and paste it in the browser.

There we go!!

Image description

Let us go to GitHub repo, make some minor changes in the file index.html by clicking on Edit/Pencil icon and commit the changes as follows.

Image description

Now go back to Amazon CodePipeline and just refresh the page.

Image description

Image description

Image description

Then go back to the browser and just refresh the page.

There we go!!

The AmazonCodePipeline has automatically identified the code changes in the GitHub repo and triggered the build.

Image description

Here is the GitHub repo of the project:

Top comments (0)