- August 25, 2021
- Posted by: Supriya Barkund
- Category: Blogs
GitHub Actions: Performs the build and test (Continuous Integration)
AWS CodeDeploy: Automates the deployment process to EC2 (Continuous Deployment)
All the project codes are committed in the GitHub repository. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. It takes place once the user triggers a push event to the respective repository.
It will perform the code build process and run the automated tests. Once it is done, GitHub Actions will run the deployment job, which will trigger the AWS CodeDeploy to do the deployment part.
CodeDeploy will help to automate the deployment by fetching the latest committed code in GitHub and updating GitHub.
Prerequisites:
- Github Account
- AWS Account
Agenda:
- Create IAM Role for EC2.
- Create IAM Role for CodeDeploy.
- Create EC2 Instance
- Launch EC2 Instance
- Install CodeDeploy Agent on EC2 Instance and nodejs.
- CodeDeploy Service Configuration.
- Github Project.
- Github actions workflow pipeline.
We will be using ap-south-1, an AWS region, for this blog setup.
Quick Note: Please select a particular region of AWS Services where you will deploy this application and, CodeDeploy, Github will use the selected region.
A. Create an IAM Role for EC2.
Search for IAM Service in the search bar.
On to right side
Access Management -> Roles
Click on Create role.
Select the type of trusted entity as AWS Service
Choose a use case as EC2.
Click on the Next: Permissions button
On the permission page, select AmazonEC2RoleforAWSCodeDeploy policy and click on the Next: Tags button.
Leave the Tags tab as it is optional and click on the Next: Review button.
Pass ec2_role as the Role name and click on Create Role button.
Open the ec2_role role and go to the Trust Relationships tab.
Click on the Edit trust relationship tab. Copy-paste the below content and click on the Update Trust Policy button.
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Principal”: {
“Service”: “ec2.amazonaws.com”
},
“Action”: “sts:AssumeRole”
}
]
}
B. Create an IAM Role for CodeDeploy.
Search for IAM Service in the search bar.
On to right side select
Access Management -> Roles
Roles
Click on Create role.
Select type of trusted entity as AWS Service
Choose a use case as EC2.
Click on the Next: Permissions button.
On the permission page, add below policies
AWSCodeDeployRole
Click on the Next: Tags button. Tags could be ignored. Click on the Next: Review button.
Pass codedeploy_role as Role name and click on Create Role button.
Open the codedeploy_role role and go to the Trust Relationships tab.
Click on the Edit trust relationship tab. Copy-paste the below content and click on the Update Trust Policy button.
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Principal”: {
“Service”: “codedeploy.amazonaws.com”
},
“Action”: “sts:AssumeRole”
}
]
}
C. Create an EC2 Instance
Search for EC2 service in the search bar.
Click on the EC2 Dashboard on the right side
Click on the Launch Instance button.
Select AMI setup as per your project requirement.
This blog needs the below setup
Ubuntu Server 18.04 LTS (HVM), SSD Volume Type (64-bit Arm). Click on Select.
Choose an Instance Type as t2.micro. Click on Next: Configure Instance Details
To establish the connection between the EC2 instance and code deploy, Select ec2_role in IAM Role, which we created before and click on Next: Add Storage
Let the Add Storage part be as it is. Click on the Add Tags button.
On the tag page, add the tag as key/value pair. In our case, I have added blog_app/nodejs; The tag will require creating a code deploy service. Click on the Next: Configure Security Group button.
In the Configure Security Group page, Add Rule called Custom TCP Rule, a select source called anywhere. Click on the Review and Launch button.
Review all details once and click on the Launch button. Wait for a few minutes to launch EC2 Instance.
Download the key/pair if you want to login into an instance using ssh.
D. Launch EC2 Instance
Go to EC2 Dashboard, right-click on the EC2 instance generated earlier, and click on connect.
Take note of the public IP and username on the connect to instance page.
You can connect to an instance using an ssh client. Check the third tab on the same page.
We will be logging into the instance using the PPK file. Convert generated PEM file into PPK and log in using putty ssh client software.
Alright, now our EC2 is set up. Let’s catch up on the AWS CodeDeploy configurations.
E. Install CodeDeploy Agent on EC2 Instance and nodejs.
To ensure our application can run properly on the EC2 server, we need to install the required packages. Since we are using Node Application, we must install nodejs related packages and a git module to enable the server environment to use the git services.
Login into an instance with a public IP, username, and PPK file. Now we are on the EC2 server. Install the required packages by running the command below.
sudo apt update
sudo apt install nodejs -> node -v
sudo apt install npm -> npm -v
sudo apt install git -> git –version
npm install pm2 -g
The CodeDeploy agent is a software package that, when installed and configured on an instance, makes it possible for that instance to be used in CodeDeploy deployments.
Use below commands to deploy code deploy agent on EC2 instance using putty ssh client.
sudo apt update
sudo apt install -y ruby
sudo apt install wget
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
sudo service codedeploy-agent status
Clone github repo into EC2 server.
git clone https://github.com/your-repo/blog_app
F. CodeDeploy Service Configuration.
AWS CodeDeploy is a fully managed deployment service that automates software deployments to various compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers.
Search for CodeDeploy in the search bar and select it.
Create an application as nodeblog_application and select the compute platform as EC2/On-premises. Click on the Create application button.
Click on the Create deployment group button. Give the deployment group name nodeblog_deployment.
Select the service role as codedeploy_role that was created
Deployment type as In-place
In Environment configuration, select Amazon EC2 Instance and select tag as blog_app/nodejs
In Deployment settings select CodeDeployDefault.OneAtATime.
Click on Create deployment group without a load balancer.
G. Github Project.
Create any node js project you wish and create appspec.yml in the root directory—appspec.yml file used by the CodeDeploy service to manage deployment.
version: 0.0
os: Linux
files:
– source:
destination: /home/ubuntu/blog_app
hooks:
ApplicationStop:
– location: scripts/application_stop.sh
timeout: 300
runas: ubuntu
ApplicationStart:
– location: scripts/application_start.sh
timeout: 300
runas: ubuntu
Github uses aws.yml file to manage Github actions.
scripts/application_start.sh
#!/bin/bash
# give permission to the files inside /secure_docs directory
sudo chmod -R 777 /home/ubuntu/blog_app
# navigate into current working directory
cd /home/ubuntu/blog_app
# install node modules
npm install
# start our node app in the background using pm2
sudo pm2 start ‘npm start.’
scripts/application_stop.sh
#!/bin/bash
# stop existing node servers
echo “Stopping any existing node servers.”
PKILL node
I. Github Actions workflow pipeline
First, create an IAM user with a full AWSCodeDeployFullAccess policy and generate an access key and secret access for the user to configure GitHub Action.
Configure the secrets in Github Repository.
To create the CI/CD workflow in GitHub Actions, create a file named .github/workflows/aws.yml in our application root that will contain the GitHub action workflows. You can use the code below in the terminal as the guide to achieving this process.
name: Automate Deploy to Amazon EC2 using Github actions
on:
push:
branches: [ master ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Step 1
– name: Checkout to repo
uses: actions/checkout@v2
# Step 2
– name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS-region: ap-south-1
# Step 3
– name: Create CodeDeploy Deployment
id: deploy
run: |
aws deploy create-deployment \
–application-name nodeblog_application \
–deployment-group-name nodeblog_deployment \
–deployment-config-name CodeDeployDefault.OneAtATime \
–github-location repository=${{ github.repository }},commitId=${{ github.sha }}
Now make a change to your repository. Your changes should automatically deploy to your EC2 server.
Please check the IP address generated for your application. Access the application from a web browser or Postman.
Amazing blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog stand out. Please let me know where you got your theme. With thanks
This will be a excellent web site, would you be involved in doing an interview about how you developed it? If so e-mail me!
Wonderful article! We will be linking to this particularly great article on our site. Keep up the great writing.
You are so cool! I don’t suppose I’ve truly read through anything like this before. So good to discover somebody with genuine thoughts on this subject. Seriously.. thank you for starting this up. This web site is one thing that’s needed on the web, someone with a little originality!
Way cool! Some extremely valid points! I appreciate you writing this post plus the rest of the site is really good.
Thank you for your time, I followed the article and I’m at the point where I committed the code for .github/workflows/aws.yml.
When I go into the repo > Actions, it keeps showing it failed saying there is a yml error: You have an error in your yaml syntax on line 45
Still trying to figure out how to fix this, if you happen to have any ideas.
Line 45 is: aws deploy create-deployment \
Sincerely,
Jordan
I’ve recently started a web site, the info you offer on this website has helped me greatly. Thanks for all of your time & work.
We аbsolutely love yօur blog and find a lot of your post’s to bе what precisely I’m looking for.
Does one оffeг guest writers to write cօntent for you personally?
I ѡouldn’t mind writing a post oг elaborating on many of the
subjects ʏou wrіte with regards to here. Again, awesome blog!