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.
- 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.
A. First, create an IAM Role for EC2
To create a role:
- Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
- In the navigation pane of the console, choose Roles and then choose Create role.
- Choose AWS service as Trusted entity type
- Choose EC2 as Use case
- Choose
AmazonEC2RoleforAWSCodeDeploy
policies to attach to your new role
- Enter Role name
EC2CodeDeployRole
- You will see a Trust policy already attached to you role
- You will see the Permissions policy already attached to you role
B. Second, create an IAM Role for CodeDeploy
Let's follow the same steps as our first IAM Role, with a CodeDeploy use case:
- In the navigation pane of the console, choose Roles and then choose Create role.
- Choose AWS service as Trusted entity type
- Choose
CodeDeploy
as Use case - Choose
AWSCodeDeployRole
policies to attach to your new role - Enter Role name
CodeDeployRole
- Click "Next" and save changes
To launch an instance:
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- 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.
- From the EC2 console dashboard, in the Launch instance pane, choose
Launch instance
.
- Under Name and tags, for Name, enter a descriptive name for your instance, i will name it
MyInstance
- 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.
- From Amazon Machine Image (AMI), select an AMI that is marked Free Tier eligible.
- 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.
- Under Key pair, for this demo choose Proceed without a key pair.
- Under Network settings, select "Create security group" then choose "Allow SSH traffic from" and "Allow HTTP traffic from the internet"
- Under Advanced details, select
EC2CodeDeployRole
that we created as IAM instance profile
- Allow tags in metadata by choosing "Enable"
- 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
- Click "Launch instance"
To create an application for an in-place deployment using the CodeDeploy console:
- Sign in to the AWS Management Console and open the CodeDeploy console at https://console.aws.amazon.com/codedeploy.
- In the navigation pane, expand Deploy, and then choose Getting started.
- Choose Create application
- In Application name, enter the name of your application
MyApp
- From Compute Platform, choose EC2/On-premises
- Choose Create application
- On your application page, from the Deployment groups tab, choose Create deployment group
- In Deployment group name, enter a name that describes the deployment group
MyApp-deployment-group
- In Service role, choose a service role that grants CodeDeploy access to your target instance, the one that we created
- In Deployment type, choose In-place.
- In Environment configuration, select Amazon EC2 instances
- In the Key and Value fields, enter Name as key and MyInstance as value
- In Deployment settings, choose a deployment configuration to control the rate your application is deployed to instances, such as all at once.
- Uncheck Enable load balancing
- Choose Create deployment group
- Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.
- On the Welcome page, choose Create pipeline.
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)
- In Pipeline name, enter the name for your pipeline
- In Pipeline type, choose V2 type pipelines
- In Service role, Choose New service role to allow CodePipeline to create a new service role in IAM.
- Choose Next
- 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.
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).
- 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).
- Under Connection, if you have not already created a connection to your provider, choose Connect to GitHub.
- After you choose to create the connection, the Connect to GitHub page appears.
- Under GitHub Apps, choose an app installation or choose Install a new app to create one.
- On the Install AWS Connector for GitHub page, choose the account where you want to install the app.
- In Repository name, choose the name of your third-party repository my is
aws_codedeploy_using_github
where all my files are saved. - Choose Save.
- Choose a repository in your GitHub account.
- Choose
main
as Default branch - For Output artifact format, choose
CodePipeline default
- For Trigger type, choose No trigger, then click Next
- Under Add build stage, since our deployment will be automatically made as soon as we push the source code, choose Skip build stage.
- Uner deploy stage, for Deploy provider choose
AWS CodeDeploy
- For Region US East (N. Virginia) or which ever region is close to you.
- Enter the application name
MyApp
- For Deployment group, choose
MyApp-deployment-group
that we created, then click Next.
- Review page, review your pipeline configuration, and then choose Create pipeline to create the pipeline.
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.
A detailed view of the pipeline, including the state of each action in each stage and the state of the transitions, is displayed.
Next, let's go to our EC2 Instance and test the deployment
- Back to EC2 console, filter the running instance that we created
- choose
MyInstance
- Scroll down, under details copy the Public IPv4 DNS, open a new tab and paste it in your browser.
- As you can see our website has been successfully deployed on AWS EC2.
- Go to your code files and edit something, i'm going to add
WOW
before congratilations
- 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.
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.