Skip to content

Learn how to automate application deployment into EC2 using CodeDeploy as soon as application source code is pushed on GitHub.

Notifications You must be signed in to change notification settings

julien-muke/aws_codedeploy_using_github

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 

Repository files navigation

aws CI/CD Pipeline form GitHub to AWS EC2

In this demo, we are going learn how to upload code from our local computer to a remote repository in GitHub. Once the code is in GitHub it automatically triggers a code pipeline to take that code and deploy it on our AWS EC2 using CodeDeploy.

CI_CD

  • Setup IAM Roles
  • Setup EC2 as Deployment Server
  • Setup CodeDeploy Pipeline
  • Setup application & Deployment Group
  • Integrate GitHub as a source/repository
  • Deploy Application on EC2 once the code is push GitHub it automatically triggers a code pipeline to take that code and deploy it on EC2 using Code deploy.

➡️ Step 1 - Setup IAM Roles

A. First, create an IAM Role for EC2

To create a role:

  1. Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
  2. In the navigation pane of the console, choose Roles and then choose Create role.

Screenshot 2024-06-12 at 14 01 20

  1. Choose AWS service as Trusted entity type
  2. Choose EC2 as Use case

Create-role-IAM-Global(7)

  1. Choose AmazonEC2RoleforAWSCodeDeploy policies to attach to your new role

Create-role-IAM-Global(8)

  1. Enter Role name EC2CodeDeployRole
  2. You will see a Trust policy already attached to you role
  3. You will see the Permissions policy already attached to you role

Create-role-IAM-Global(9)

B. Second, create an IAM Role for CodeDeploy

Let's follow the same steps as our first IAM Role, with a CodeDeploy use case:

  1. In the navigation pane of the console, choose Roles and then choose Create role.
  2. Choose AWS service as Trusted entity type
  3. Choose CodeDeploy as Use case
  4. Choose AWSCodeDeployRole policies to attach to your new role
  5. Enter Role name CodeDeployRole
  6. Click "Next" and save changes

➡️ Step 2 - Setup EC2 as Deployment Server

To launch an instance:

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation bar at the top of the screen, you can use the selected Region, or optionally select a Region that is closer to you.
  3. From the EC2 console dashboard, in the Launch instance pane, choose Launch instance.

Launch instance

  1. Under Name and tags, for Name, enter a descriptive name for your instance, i will name it MyInstance
  2. Under Application and OS Images (Amazon Machine Image), Choose Quick Start, and then choose the operating system (OS) for your instance. For your first Linux instance, we recommend that you choose Amazon Linux.
  3. From Amazon Machine Image (AMI), select an AMI that is marked Free Tier eligible.

Screenshot 2024-06-15 at 09 30 48

  1. Under Instance type, for Instance type, choose t2.micro, which is eligible for the Free Tier. In Regions where t2.micro is not available, t3.micro is eligible for the Free Tier.
  2. Under Key pair, for this demo choose Proceed without a key pair.

Launch instance2

  1. Under Network settings, select "Create security group" then choose "Allow SSH traffic from" and "Allow HTTP traffic from the internet"

Launch instance3

  1. Under Advanced details, select EC2CodeDeployRole that we created as IAM instance profile

Launch instance4

  1. Allow tags in metadata by choosing "Enable"
  2. For User data, let's Upload a file with the user data, copy and paste file below:
#!/bin/bash
sudo yum -y update
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user
wet https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
sudo yum install -y python-pip
sudo pip install awscli

ec2

  1. Click "Launch instance"

➡️ Step 3 - Setup CodeDeply Pipeline

To create an application for an in-place deployment using the CodeDeploy console:

  1. Sign in to the AWS Management Console and open the CodeDeploy console at https://console.aws.amazon.com/codedeploy.
  2. In the navigation pane, expand Deploy, and then choose Getting started.
  3. Choose Create application

codedeploy1

  1. In Application name, enter the name of your application MyApp
  2. From Compute Platform, choose EC2/On-premises
  3. Choose Create application

codedeploy2

  1. On your application page, from the Deployment groups tab, choose Create deployment group

deploymentgroup

  1. In Deployment group name, enter a name that describes the deployment group MyApp-deployment-group
  2. In Service role, choose a service role that grants CodeDeploy access to your target instance, the one that we created
  3. In Deployment type, choose In-place.
  4. In Environment configuration, select Amazon EC2 instances
  5. In the Key and Value fields, enter Name as key and MyInstance as value
  6. In Deployment settings, choose a deployment configuration to control the rate your application is deployed to instances, such as all at once.
  7. Uncheck Enable load balancing
  8. Choose Create deployment group

Create-deployment-group-CodeDeploy-us-east-1

➡️ Step 4 - Create a pipeline in CodePipeline

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.
  2. On the Welcome page, choose Create pipeline.

CodePipeline1

NOTE: To create a pipeline in the console, you must provide the source file location and information about the providers you will use for your actions.

I have uploaded in this repository, all the requided files for this demo, where we have index.html (source file) appspec.yml (where we give the instruction to the deployment) and scripts ( where we install all dependencies)

  1. In Pipeline name, enter the name for your pipeline
  2. In Pipeline type, choose V2 type pipelines
  3. In Service role, Choose New service role to allow CodePipeline to create a new service role in IAM.
  4. Choose Next

Create-new-pipeline-CodePipeline-us-east-1 (2)

  1. Next, lets' Add the source stage, if it's your first time to add a source stage with a third-party provider such as GitHub, there are few configurations you need to make first.

:octocat: Create a connection to GitHub (console)

Before you begin:
* You must have created an account with GitHub.
* You must have already created a GitHub code repository with all the code uploaded (check the code above).

  1. Choose to create a pipeline. Follow the steps in Create a Pipeline to complete the first screen and choose Next. On the Source page, under Source Provider, choose GitHub (Version 2).
  2. Under Connection, if you have not already created a connection to your provider, choose Connect to GitHub.

Screenshot 2024-06-15 at 12 48 08

  1. After you choose to create the connection, the Connect to GitHub page appears.

GitHub1

  1. Under GitHub Apps, choose an app installation or choose Install a new app to create one.

GitHub2

  1. On the Install AWS Connector for GitHub page, choose the account where you want to install the app.
  2. In Repository name, choose the name of your third-party repository my is aws_codedeploy_using_github where all my files are saved.
  3. Choose Save.

github3

  1. Choose a repository in your GitHub account.
  2. Choose main as Default branch
  3. For Output artifact format, choose CodePipeline default
  4. For Trigger type, choose No trigger, then click Next

Create-new-pipeline-CodePipeline-us-east-1 (3)

  1. Under Add build stage, since our deployment will be automatically made as soon as we push the source code, choose Skip build stage.

build1

  1. Uner deploy stage, for Deploy provider choose AWS CodeDeploy
  2. For Region US East (N. Virginia) or which ever region is close to you.
  3. Enter the application name MyApp
  4. For Deployment group, choose MyApp-deployment-group that we created, then click Next.

build2

  1. Review page, review your pipeline configuration, and then choose Create pipeline to create the pipeline.

➡️ Step 5 - View and test AWS CodePipeline

The Pipelines page displays a list of all your pipelines for that Region are shown.

The name, type, status, version, creation date, and date when last modified of all pipelines associated with your AWS account are displayed, along with the most recently started execution time.

To view pipelines, Choose View details next to a specific row to display a details dialog box listing the most recent executions.

viewpipeline1

A detailed view of the pipeline, including the state of each action in each stage and the state of the transitions, is displayed.

myApplication_pipeline-CodePipeline-us-east-1 (1)

Next, let's go to our EC2 Instance and test the deployment

  1. Back to EC2 console, filter the running instance that we created
  2. choose MyInstance
  3. Scroll down, under details copy the Public IPv4 DNS, open a new tab and paste it in your browser.

instance3w

  1. As you can see our website has been successfully deployed on AWS EC2.

site

Let's test our CI/CD Pipeline

  1. Go to your code files and edit something, i'm going to add WOW before congratilations

code

  1. As soon as you push your code to GitHub it will automatically trigger a code pipeline and deploy it on our AWS EC2 using CodeDeploy and update the changes.

test2

💰 Cost

All services used are eligible for the AWS Free Tier. However, charges will incur at some point so it's recommended that you shut down resources after completing this tutorial.

About

Learn how to automate application deployment into EC2 using CodeDeploy as soon as application source code is pushed on GitHub.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published