Monday, 26 December 2022

Automated Code Deployment on AWS using CodePipeline

 

How To Automate Code Deployment on AWS using CodePipeline, CodeCommit, CodeDeploy, CloudWatch :

Despite high competition, AWS has continued its stronghold on the cloud market till now. New services are continuously added, resulting in improved and high performance. And while AWS tries to keep things simple for developers, using the platform isn’t always easy. Especially for a big organization.

For this reason, AWS provides multiple tools to help AWS developers utilize and develop with its cloud platform and services. Let’s take a look at such tools that can help you build applications with AWS.

CodePipeline_Diagram


Developer Tools Offered By AWS Used In Case Study

The developing tool in Amazon Web Services helps their users/customers to get the software to deliver safely. It also modifies the developers and IT operations to make the service outcome even better. This helps to build, test, and deploy the application to AWS or the required destination. This tool helps the user to monitor, store, and provide surveillance to the application source code. you’ll be able to use AWS CodePipeline to orchestrate associate degree end-to-end computer code advancement using these services and third-party tools or integrate every service severally along with your existing tools.

AWS CodeCommit – It is a  fully-managed source control service that hosts secure Git-based repositories. CodeCommit makes it easy for teams to collaborate on code in a secure and highly scalable ecosystem.Code Commit

AWS CodeBuild – A fully managed continuous integration service that compiles source code, runs tests and produces software packages that are ready to deploy, on a dynamically created build server.
What is AWS CodeBuild? - AWS CodeBuild


AWS CodeDeploy – A fully managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers.

aws-codedeploy-flow

AWS CodePipeline – A fully configured continuous delivery service that helps the user to automate their released pipelines for fast and reliable application and infrastructure updates.

pipeline


Base Scenario (Use Case)

For this scenario, we will use CodePipeline to deploy code maintained in a CodeCommit repository to a single Amazon EC2 instance. Your pipeline is triggered when you push a change to the CodeCommit repository. The pipeline deploys your changes to an Amazon EC2 instance using CodeDeploy as the 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.

Deploy AWS CodePipeline


Steps To Deploy AWS CodePipeline Using AWS Developer Tools

  1. If you don’t have a FREE AWS account, then create one from here Step by Step AWS FREE Tier Account
  2. Use the region US East Ohio (us-east-2) for this
  3. Download the HTTPS Git credentials for AWS CodeCommit using the IAM console
  4. Create a CodeCommit Repository
  5. Add Sample Code to your CodeCommit Repository
  6. Create Amazon EC2 Windows instances and install the CodeDeploy agent
  7. Create an application in CodeDeploy
  8. Create your first pipeline in CodePipeline
  9. Modify Code in your CodeCommit Repository
  10. Verify your Pipeline Ran Successfully


Step 1: Download the HTTPS Git credentials for AWS CodeCommit using the IAM console

1) Click on users from the left menu and click on your user then click on security credentials Scroll down to HTTPS Git credentials for AWS CodeCommit and click on Generate Credential

HTTPS Codecommit credentials

Step 2: Create a CodeCommit Repository

1) Open the Amazon Codecommit console and choose to Create repository.

CodeCommit - AWS Developer Tools

2) On the Create repository page, in Repository name, enter a name for your repository (for example, K21repo). Then Choose Create.

Code commit repo

3) On the next screen, click on Copy next to Step3: Clone the Repository (We’ll use this to clone this blank repository in the next step).

CodeCommit-AWS-Developer-Tools


Step:3 Add Sample Code to your CodeCommit Repository

1) Run the following command to clone the repository, replacing the GIT Clone Address with the one you copied in the earlier step.
git clone <GIT Clone Address> then enter the username and password for IAM users HTTPS Git Credentials that we already downloaded

git CredentialsAfter that, it will show you a warning message that you cloned an empty repository but it’s ok

2) Next, download the sample code and save it in which we created our local repository. Unzip the files from SampleApp_Linux.zip into the local directory you created earlier (for example, /tmp/MyDemoRepo).
–> Deploy to Amazon Linux instances using CodeDeploy, download the sample application here: SampleApp_Linux.zip.

3) Open Command Prompt and make sure you are in the SampleApp_Linux folder
Run the following commands to stage all your files at once and commit the files with a commit message:
git add -A
git commit -m “Add sample application files”
git push

git push

4) The files you downloaded and added to your local repo have now been added to the master branch in your CodeCommit K21Repo repository and are ready to be included in a pipeline.

k21repo


Step 4: Create Amazon EC2 Windows instances and install the CodeDeploy agent

1) Create an IAM role that will be required to grant permission to the EC2 instance. Select the policy named AmazonEC2RoleforAWSCodeDeploy to create.

Create_Simple_Code_Pipeline2) Launch the instance on which our code will be deployed. To know more about how to launch an EC2 instance check our Blog on EC2 Instance. Just remember to add the IAM role that we have created and In Auto-assign Public IP, choose Enable.
Expand Advanced Details, and in User data, As text selected, enter the following:
#!/bin/bash
yum -y update
yum install -y ruby
yum install -y aws-cli
cd /home/ec2-user
aws s3 cp s3://aws-codedeploy-us-east-2/latest/install . –region us-east-2
chmod +x ./install
./install auto
Create_Simple_Code_Pipeline

4)On Configure Security Group page, do the following:
• Next to Assign a security group, choose to create a new security group.
• In the row for SSH, under Source, choose Anywhere.
• Choose Add Rule, choose HTTP, and then under Source, choose Anywhere.

Simple_Code_Pipeline

Leave rest settings default and click on Launch


Step 5: Create an application in CodeDeploy

1) Create an AWSCodeDeployRole that allows CodeDeploy to perform deployments

Code Deploy Role

2) Initially click on create an application in CodeDeploy, and In Compute Platform, choose EC2/On-premises.Choose to Create application.

Create Application

3) On the page that displays your application, choose to Create a deployment group. In service, role creates an IAM role under code deploy category.

Deployment Group


4) Under Deployment type, choose In-place. Under the Environment configuration, choose Amazon EC2 Instances.

deployment type

5) Under Deployment configuration, choose CodeDeployDefault.OneAtaTime. Under Load Balancer, clear Enable load balancing, leave the defaults then choose to Create a deployment group.

Deploy AWS CodePipeline


Step 6: Create your first pipeline in CodePipeline

1) Open the CodePipeline console. Choose pipeline settings, Enter your desired name and in the Service role, Choose New service role to allow CodePipeline to create a new service role in IAM.  To know more about AWS IAM refer to our blog on AWS Identity And Access Management (IAM).


2) In the Add source stage, select Source provider, choose Amazon S3. Under the S3 object key, enter the object key with or without a file path, and remember to include the file extension.

Deploy AWS CodePipeline

3) In the Add build stage, choose to Skip build stage, and then accept the warning message by choosing Skip again. Choose Next.

4) In the Add deploy stage, in Deploy provider, choose AWS CodeDeploy.Then enter your application name or choose the application name from the list. In the Deployment group, enter MyDemoDeploymentGroup, or choose it from the list, and then choose Next.

Deploy AWS CodePipeline

5) In Step 5: Review, review the information, and then choose 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.

Deploy AWS CodePipeline

Congratulations! You just created a simple pipeline in CodePipeline.
you can verify that by coping EC2 Public DNS address and then past it into the address bar of your web browser

Deploy AWS CodePipeline


Step 7: Modify Code in your CodeCommit Repository

1) In your CodeCommit repository revise the contents of the index.html file to change the background color and some of the text on the webpage, by clicking on edit

Deploy AWS CodePipeline

2) Under Author name enter K21,  then enter Email Address, Click on Commit changes

Deploy AWS CodePipeline


Step 8: Verify your Pipeline Ran Successfully

1) View the initial progress of the pipeline. The status of each stage changes from No executions yet to In Progress, and then to Succeeded. The running of the pipeline should be complete within a few minutes.

2) After Succeeded is displayed for the action status, refresh the demo page you accessed earlier in your browser.

Deploy AWS CodePipeline


 

How To Deploy Web App From S3 Bucket To EC2 Instance on AWS Using CodePipeline :

In this post, we are going to cover the case study to deploy a web application from an S3 bucket to an EC2 instance using AWS CodePipeline which is a part of CI/CD DevOps practices.

Topics, we’ll cover :

  • Overview of CI/CD
  • CI/CD Tools Offered By AWS
  • Steps To Deploy Web Application On AWS

Before deploying a web application we should understand the basic concept of Continuous Integration(CI) and Continuous Deployment(CD) what are they and What kind of tools are offered by AWS for DevOps CI/CD practices.

Overview Of CI/CD

AWS CI/CD architecture

Continuous Integration(CI) and Continous Deployment(CD) gets rid of the traditional manual gate and implements fully automated verification of the acceptance environment to determine the scenario whether the pipeline can continue to production or not.

Continuous Integration focuses on the software development life cycle (SDLC) of the individual developer in the code repository. This can be executed multiple times with a primary goal to enable early detection of integration bugs, and errors.

Continuous Delivery focuses on automated code deployment in testing or production environment, taking the approval of updates to achieve automated software release process, pre-emptively discovering deployment issues.


CI/CD Tools Offered By AWS Used In Case Study

AWS offers an end-to-end CI/CD stack comprised of the following four services:

AWS CodeCommit – It is a  fully-managed source control service that hosts secure Git-based repositories. CodeCommit makes it easy for teams to collaborate on code in a secure and highly scalable ecosystem.Code Commit

AWS CodeBuild – A fully managed continuous integration service that compiles source code, runs tests and produces software packages that are ready to deploy, on a dynamically created build server.
What is AWS CodeBuild? - AWS CodeBuild


AWS CodeDeploy – A fully managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers.

aws-codedeploy-flow

AWS CodePipeline – A fully configured continuous delivery service that helps the user to automate their released pipelines for fast and reliable application and infrastructure updates.CI/CD Services Offered By AWS pipeline

Typically in many organizations, there are many tools for code repository but we are using AWS S3 as a code repository.


Steps To Deploy Web Application Using AWS CodePipeline

We will be performing 4 steps to deploy a web application

Step 1: Create an S3 bucket for your application


1) Open the Amazon S3 console and Choose Create bucket and In Bucket name, enter a name for your bucket, and also don’t forget to enable Versioning.

2) Next, download the sample code and save it into a folder or directory on your local computer.
Choose one of the following. Choose SampleApp_Windows.zip if you want to follow the steps in this tutorial for Windows Server instances. (Do not Unzip the file while Uploading)
–> If you want to deploy to Amazon Linux instances using CodeDeploy, download the sample application here: SampleApp_Linux.zip.
–> If you want to deploy to Windows Server instances using CodeDeploy, download the sample application here: SampleApp_Windows.zip.

3) In the S3 console, Upload code in the bucket you created.

AWS S3 Bucket

To Know More  About Blue-Green Deployment

Step 2: Create Amazon EC2 Windows instances and install the CodeDeploy agent

1) Create an IAM role that will be required to grant permission to EC2 instance. Select the policy named AmazonEC2RoleforAWSCodeDeploy to create.

Instance Role For EC2

2) Launch instance on which our code will be deployed.

3) Just remember to add the IAM role that we have created. and In Auto-assign Public IP, choose Enable. Expand Advanced Details, and in User data, As text selected, enter the following:
<powershell>
New-Item -Path c:\temp -ItemType “directory” -Force
powershell.exe -Command Read-S3Object -BucketName bucket-name/latest -Key codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi
Start-Process -Wait -FilePath c:\temp\codedeploy-agent.msi -WindowStyle Hidden
</powershell>

Note: bucket-name is the name of the S3 bucket that contains the CodeDeploy Resource Kit files for your region. For example, for the US West (Oregon) Region, replace the bucket-name with AWS-code deploy-us-west-2. For a list of bucket names, see Resource Kit Bucket Names by Region.

4) On the Configure Security Group page, allow port 80 communication so you can access the public instance endpoint. Then follow the default configuration and launch the instance

Created EC2 instance


Step 3: Create an application in CodeDeploy

1) Initially create an application in CodeDeploy, and In Compute Platform, choose EC2/On-premises.Choose to Create application.

2) On the page that displays your application, choose to Create a deployment group. In service, role creates an IAM role under code deploy category. Under Deployment type, choose In-place.

3) Under Environment configuration, choose Amazon EC2 Instances.

4) Under Deployment configuration, choose CodeDeployDefault.OneAtaTime.

5) Under Load Balancer, clear Enable load balancing, leave the defaults then choose to Create a deployment group.

Application code deploy for CI/CD Services Offered By AWS


Step 4: Create your first pipeline in CodePipeline

1) Open the CodePipeline console. Choose pipeline settings, Enter your desired name and in Service role, Choose New service role to allow CodePipeline to create a new service role in IAM.  To know more about AWS IAM refer to our blog on AWS Identity And Access Management (IAM).

2) In the Add source stage, select Source provider, choose Amazon S3. Under the S3 object key, enter the object key with or without a file path, and remember to include the file extension.

3) In the Add build stage, choose to Skip build stage, and then accept the warning message by choosing Skip again. Choose Next.

4) In the Add deploy stage, in Deploy provider, choose AWS CodeDeploy.Then enter your application name or choose the application name from the list. In the Deployment group, enter MyDemoDeploymentGroup, or choose it from the list, and then choose Next.

AWS Pipeline

Congratulations! You just created a simple pipeline in CodePipeline. you can verify that by coping EC2 Public DNS address and then past it into the address bar of your web browser