As a follow up to the presentation, I would like to present a demo of a simple pipeline using CodeCommit repository.
We are going to use
us-east-1region for this demo.
Table of Contents
- Create AWS CLI User
- Create IAM Roles
- Create a CodeCommit repository
- Add sample code to your CodeCommit repository
- Create an EC2 Linux instance and install the CodeDeploy agent
- Create an application in CodeDeploy
- Create a pipeline in CodePipeline
- Modify code in your CodeCommit repository
- Clean Up
- Summary
- Referrals
Step 1. Create AWS CLI User
- Navigate to
IAM>Users> ClickAdd users - Enter a
User Name, SelectAccess key - Programmatic accessand ClickNext: Permissions - Select
AdministatoratAdd user to groupand ClickNext: Tags - Click
Next: Review - Click
Create user - Click
Download.csvandClose - Again navigate to
IAM>Users> Click on the user that was created in the previous step - Navigate to
Security Credentials - Click on
Generate CredentialsunderHTTPS Git credentials for AWS CodeCommit - Click
Download credentialsandClose - Setup AWS CLI as shown below.
Step 2. Create IAM Roles
We are going to create two IAM Roles: one for EC2 and the other one for Code Deploy
EC2InstanceRole
- Navigate to
IAM> clickRoles> clickCreate role. - Select
EC2underCommon use casesand clickNext. - Search for and select the policy named
AmazonEC2RoleforAWSCodeDeploy, and then clickNext. - Enter a name for the role as
EC2InstanceRoleand then clickCreate role.
CodeDeployRole
- Navigate to
IAM> clickRoles> clickCreate role. - Under
Use cases for other AWS services:> SelectCodeDeployand clickNext. -
AWSCodeDeployRolemanaged policy will be automatically added, so clickNext - Enter a name for the role as
CodeDeployRoleand then clickCreate role.
Step 3. Create a CodeCommit repository
- Navigate to
CodeCommit - On the
Repositoriespage > clickCreate repository> EnterMyDemoRepoas Repository name and then clickCreate. - Copy the
git clonecommand fromClone the repositoryand execute the following command
mkdir codedeploy
git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo
Cloning into 'MyDemoRepo'...
warning: You appear to have cloned an empty repository.
Note: Enter
git credentialswhen cloning the repo for the first time.
Step 4. Add sample code to your CodeCommit repository
-
We will clone github code to
/tmpfolder and then copy the contents to codecommit'sMyDemoRepo
mkdir -p /tmp; cd /tmp; git clone https://github.com/kasukur/codedeploy.git cp -rf codedeploy/MyDemoRepo/* ~/codedeploy/MyDemoRepo/. -
Commit and push the files to CodeCommit
MyDemoReporepository
cd MyDemoRepo git add -A git commit -m "Add sample application files" git push
Note: I have used the code from SampleApp_Linux.zip and Solid State HTML5 template. Alternatively, you can download both manually and add them to codecommit's
MyDemoRepo
The repository tree should look like this:
MyDemoRepo
│-- appspec.yml
│-- index.html
│-- LICENSE.txt
└-- scripts
│-- install_dependencies
│-- start_server
└-- stop_server
AppSpec file must be a YAML-formatted file named appspec.yml and it must be placed in the root of the directory structure of an application's source code, otherwise deployments fail. It is used by CodeDeploy to determine:
- What it should install onto your instances from your application revision in Amazon S3 or GitHub.
- Which lifecycle event hooks to run in response to deployment lifecycle events.
version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: root
- location: scripts/start_server
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: root
Step 5. Create an EC2 Linux instance and install the CodeDeploy agent
We are going to create an EC2 instance, where we deploy a sample application using CodeDeploy agent.
We are also going to attach an IAM role EC2InstanceRole to the instance (known as an instance role) to allow it to fetch files that the CodeDeploy agent uses to deploy your application.
- Navigate to
EC2>Instances> ClickLaunch Intances - Enter Name as
MyCodePipelineDemo - Select
Amazon Linux 2 AMI1and Instance Typet2.micro Free tier eligible - You could create a key pair or choose Proceed without a key pair. Creating a key pair would allow you to logon to the EC2 instance and check the application.
- Select
Allow SSH traffic from My IPandAllow HTTP traffic from the internet - Click
Advanced Details> SelectEC2InstanceRoleunderIAM instance profileand enter the following in theuser dataand clickLaunch Instance
Note: New EC2 console adds a tag with
key: NameandValue: MyCodePipelineDemo. In case you don't see the Tags, please add them underTagssection by selecting the EC2 instance in EC2 console.
#!/bin/bash
sudo yum -y update
sudo yum install -y ruby
sudo yum install -y aws-cli
sudo cd /home/ec2-user
sudo wget https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
Note: the user data installs ruby, aws-cli and CodeDeploy agent
Step 6. Create an application in CodeDeploy
Create an application in CodeDeploy
- Navigate to
CodeDeploy>Applications. - In Application name, enter
MyDemoApplication. - In Compute Platform, choose
EC2/On-premises. - Choose
Create application.
Create a deployment group in CodeDeploy
A deployment group is a resource that defines deployment-related settings like which instances to deploy to and how fast to deploy them.
- On the page that displays your application, choose
Create deployment group. - In Deployment group name, enter
MyDemoDeploymentGroup. - In Service Role, choose the service role
CodeDeployRole, which we created earlier. - Under
Deployment type, chooseIn-place. - Under
Environment configuration, chooseAmazon EC2 Instances. In the Key field, enterName. In the Value field, enter theMyCodePipelineDemo. - Under
Deployment settings, chooseCodeDeployDefault.OneAtaTime. - Under
Load Balancer, make sureEnable load balancingis not selected. You do not need to set up a load balancer or choose a target group for this example. - Click
Create deployment group.
Step 7. Create a pipeline in CodePipeline
We are now ready to create the pipeline.
In this step, we create a pipeline that runs automatically when code is pushed to your CodeCommit repository.
Create a CodePipeline pipeline
- Navigate to
Pipelines> clickCreate pipeline. - In Pipeline name, enter
MyFirstPipeline. - In Service role, choose
New service roleto allow CodePipeline to create a service role in IAM and clickNext - Select
AWS CodeCommitin Source provider. SelectMyDemoRepoin Repository name. Selectmasterin Branch name. SelectAmazon CloudWatch Events (recommended)underChange detection options. SelectCodePipeline defaultunderOutput artifact formatand clickNext. - Click
Skip build stageas we are deploying a static website. - Click
skipatYour pipeline will not include a build stage. Are you sure you want to skip this stage? - At deploy stage, Select
AWS CodeDeployinDeploy provider, SelectMyDemoApplicationinApplication name, SelectMyDemoDeploymentGroupinDeployment groupand then clickNext. - Review the information and then click
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. You can view progress and success and failure messages as the CodePipeline sample deploys the webpage to the Amazon EC2 instance in the CodeDeploy deployment.
If Deploy fails with an error, one of the reasons could be that
codedeploy-agentmay not be running on the EC2 instance, which we can check by running the following command
sudo service codedeploy-agent status The AWS CodeDeploy agent is running as PID 3436 -
Now the pipeline has succeeded but when we check
index.html. it appears that the stylesheets...etc are missing
Let's fix this by updatingappspec.yml
from:source: /index.htmltosource: /
version: 0.0 os: linux files: - source: / destination: /var/www/html/ hooks: BeforeInstall: - location: scripts/install_dependencies timeout: 300 runas: root - location: scripts/start_server timeout: 300 runas: root ApplicationStop: - location: scripts/stop_server timeout: 300 runas: rootIf source is a single slash ("/" for Amazon Linux, RHEL, and Ubuntu Server instances, or "\" for Windows Server instances), then all of the files from your revision are copied to the instance.
-
Commit and push the changes
git add -A git commit -m "updated appspec.yml" git push The pipeline will automatically pick up the changes and run the Deploy because of
Amazon EventBridgeRule
Step 9
Step 10
Step 12
Step 8. Modify code in your CodeCommit repository
- Update the following line of HTML code in
index.htmland push the changes. Change the value from:This is Solid State, to:This is Solid State - Version 1 -
Commit and push the changes
git status git commit -am "updated index.html" git push -
index.html got updated on our EC2 instance.
grep -i version /var/www/html/index.html <h2>This is Solid State - Version 1</h2>
Step 9. Clean Up
- Terminate
MyCodePipelineDemoEC2 Instance. - Delete
MyFirstPipelineunderPipelines, which will removeAmazon CloudWatch Events rulerelated to the pipeline. - Delete
MyDemoApplicationunderApplications. - Delete
MyDemoRepounderCodeCommit>Repositories. - Empty and delete S3 bucket prefixed with codepipeline.
Summary
- In this demo, we learnt how easy it is to setup a simple pipeline using CodeCommit, CodeDeploy and Pipeline.
Referrals
- Tutorial: Create a simple pipeline (CodeCommit repository)
- Cover Image by @darya_jumelya from unsplash.

























Top comments (0)