i tried to setup AWS CodePipeline and AWS CodeDeploy to deploy application on Amazon EC2 using github as a source

i tried to setup AWS CodePipeline and AWS CodeDeploy to deploy application on Amazon EC2 using github as a source

Clock Icon2021.11.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction:

In this article, we will learn about how to create a CI/CD Pipeline using github and AWS Services:  AWS CodeDeploy, AWS Pipeline, AWS EC2

AWS CodeDeploy is fully managed Deployment Service that automate deployment to variety of Compute Service like Amazon EC2,AWS lambda , your on-premises etc

AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change, based on the release model you define.

CodeDeploy makes it easier for you to:

  • Rapidly release new features.
  • Update AWS Lambda function versions.
  • Avoid downtime during application deployment.
  • Handle the complexity of updating your applications, without many of the risks associated with error-prone manual deployments.

Hands-On:

Setup IAM Role

Create a role for CodeDeploy + EC2

  • ROLE 1:
    • Select Your Use Case: EC2

    • Permissions Policies: AmazonEC2RoleForAWSCodeDeploy
    • Role Name: EC2_CodeDeploy_DevIO,AmazonS3FullAccess

  • ROLE 2:
    • Select Your Use Case: CodeDeploy

    • Permissions Policies: AWSCodeDeployRole
    • Role Name : CodeDeploy_DevIO

 

Setup EC2 as deployment server

 

Choose AMI: Amazon Linux 2

Instance Type : t2.micro

no. of Instances: 1

Security groups: enable port port 22 for ssh and HTTP 80 for application

User Data: add below script in user data to add the required

change the link according to your region in line-6: bucket-name is the name of the Amazon S3 bucket that contains the CodeDeploy Resource Kit files for your region. region-identifier is the identifier for your region.to find bucket name and region Click here


#!/bin/bash
sudo yum -y update
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user
wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
sudo yum install -y python-pip
sudo pip install awscli

ADD Tag: Name: DevelopersIOEC2Instance

 

lauch the instance without keypair

Setup application and deploymentGroup

  1. Create application
    1. Application Name : DevelopersIODemo
    2. Compute Platform : EC2/OnPremises
  2. Create Application
  3. Create Deployment Group
    1. Enter a deployment group name: DevelopersIoDmoDeploymentGroup
    2. Service role ARN: CodeDeploy_DevIO
    3. Deployment type: In-place
    4. Deployment configuration: CodeDeployDefault.AllAtOnce
    5. Environment configuration: Amazon EC2 instances
      1. Name: DevelopersIOEC2Instance

Setup github with Sample App:

bellow code will download sample app from s3

mkdir codedeploy-sample
cd codedeploy-sample
curl -O http://s3.amazonaws.com/aws-codedeploy-us-east-1/samples/latest/SampleApp_Linux.zip
unzip SampleApp_Linux.zip
rm SampleApp_Linux.zip

this command will upload the sample app in github

git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:YOUR_USERNAME/YOUR_REPOSITORY.git
git push -u origin master

 

Setup CodePipeline:

  1. choose codepipeline from developer tool in aws services
  2. create pipeline
  3. choose pipeline setting:
    1. pipeline name: DevelopersIO Pipeline
    2. Service role : create new service role
    3. next

Add source stage:

  • source provider: Github(version 2)
  • connection: connect to github // add username and password of your github account
  • repository name: aws_codedeploy_using_Github //add repository name where you pushed sample_app as it consist of settings
  • branch name: main //select the working branch where you will push product for deployment
  • select start the pipeline on source code change
  • output artifact format: codepipelinedefault
  • click next -> create

 

finally:

we have successfully created a cicd pipeline now every time we push a new code into github this pipeline will automatically deploy the application in Amazon EC2

Resources:

https://aws.amazon.com/blogs/devops/automatically-deploy-from-github-using-aws-codedeploy/

https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-four-stage-pipeline.html#tutorials-four-stage-pipeline-pipeline-create

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.