Saturday, October 17, 2020

AWS Code Deploy using GitHub

 Description: Here I have explain, What is AWS code deploy and How to implement it with GitHub?

What is AWS Code Deploy?

It is service that automates the code deployments to any instance like EC2 instance or Instance which running on-premise. Helps rapidly release new features and avoid downtime during deployment.

Architect 



Prerequisites:
  1. GitHub Repository with web application [Here In my case harpal1990/AppTest]
  2. AWS account

IAM Roles : Two Roles require for AWS code deploy one for code deploy and another for EC2 instance. 
  • To create role open AWS console, navigate to IAM -- Roles -- Create Role 
  • Create the following IAM roles and attached the policies 
Role1:
Name : EC2Role
Permission : AmazonEC2FullAccess, AmazonS3FullAccess
[Allow EC2 instance and AmazonS3 access]

Role2:
Name :  CodeDeployRole
Permission: AWSCodeDeployRole, AmazonS3FullAccess

  • To create IAM role navigate to IAM -- Roles  -- Create role  once you click on  create role select EC2 Service 
  • Here I have selected 2 Permission to allow access S3 bucket are as follow and save with EC2Role name and click on create 
AmazonEC2FullAccess, AmazonS3FullAccess


  • Now create another role with Name CodeDeployServieRole for Code Deploy and assigned "AWSCodeDeployRole"
AWSCodeDeployRole, AmazonS3FullAccess


  • Once you create Role you need to edit policy and modify relationship
  • Update Service by "codedeploy.region.amazonaws.com" change region 
  • Attach IAM role  CodeDeployInstanceRole with EC2 instance 
Prepare EC2 instance with Code Deploy Agent
  • For create instance open AWS console and navigate to EC2 service. Create Amazon Linux 2 AMI 


  • Create EC2 instance with pre install package like ruby, python, aws-cli 
#!/bin/bash
sudo yum -y update
sudo yum -y install ruby
sudo yum -y install httpd
service httpd start
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

  • Allow port 80 in security group for apache
  • Assign common tag to EC2 instance, Here I have assigned tag Name and value "CodeDeploy"
  • Review and launch EC2 instance, It will take some time to create.

Create Application 
  • Before create application I am going to create one directory called script on GitHub repository and put service start and stop script on same 

  • To create application open AWS console and Navigate to Developer Tools -- Code Deploy -- Applications 

  • Once you click on Create application, you will ask for Application Name and Compute Platform. You have 3 options for compute platform [EC2/On-premises, AWS Lambda and Amazon ECS] for this tutorial I am using EC2/On-premises

  • Create Deployment Group  fill all require details for deployment like Name, Service role create for CodeDeployment 



  • Give tag which I have assigned to EC2 instance, Here I have given Name as Tag and CodeDeploy as a value. So you can see 2 unique matches found



  • Currently no load balancer is required so I haven't use it, At last click on create deployment group


  • Create Pipeline for same application project

  • Once you click on Next it ask to connect source stage, Here In this demo I have used GitHub as repository. Once you click on GitHub it ask for credentials and then repository name


  • The next step to select build provider, Here I am skip it you can choose Jenkins or AWS codebuild
  • At last click on review and create. 
  • Once it create successfully It will upload appspec.yml file to repository. Now modify appspec.yml as per your requirement

  • Code Deploy Stage to deploy application. Fill all the require details

  • Review all settings and click on Create Pipeline. It will take some time to create
  • Once Pipeline created Create Deployment and run pipeline. Once pipeline run successfully it will upload content to all EC2 instance location define in appspec.yml file.
  • To verify you nee to browse one of EC2 instance IP address it shows index page as follow
  • Now try to modify on Index page and re run pipeline you will get changes.









No comments:

Post a Comment