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-1
region for this demo.
- 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
- Navigate to
IAM
>Users
> ClickAdd users
- Enter a
User Name
, SelectAccess key - Programmatic access
and ClickNext: Permissions
- Select
Administator
atAdd user to group
and ClickNext: Tags
- Click
Next: Review
- Click
Create user
- Click
Download.csv
andClose
- Again navigate to
IAM
>Users
> Click on the user that was created in the previous step - Navigate to
Security Credentials
- Click on
Generate Credentials
underHTTPS Git credentials for AWS CodeCommit
- Click
Download credentials
andClose
- Setup AWS CLI as shown below.
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
EC2
underCommon use cases
and clickNext
. - Search for and select the policy named
AmazonEC2RoleforAWSCodeDeploy
, and then clickNext
. - Enter a name for the role as
EC2InstanceRole
and then clickCreate role
.
CodeDeployRole
- Navigate to
IAM
> clickRoles
> clickCreate role
. - Under
Use cases for other AWS services:
> SelectCodeDeploy
and clickNext
. AWSCodeDeployRole
managed policy will be automatically added, so clickNext
- Enter a name for the role as
CodeDeployRole
and then clickCreate role
.
- Navigate to
CodeCommit
- On the
Repositories
page > clickCreate repository
> EnterMyDemoRepo
as Repository name and then clickCreate
. - Copy the
git clone
command fromClone the repository
and 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 credentials
when cloning the repo for the first time.
-
We will clone github code to
/tmp
folder 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
MyDemoRepo
repositorycd 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
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 AMI1
and 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 IP
andAllow HTTP traffic from the internet
- Click
Advanced Details
> SelectEC2InstanceRole
underIAM instance profile
and enter the following in theuser data
and clickLaunch Instance
Note: New EC2 console adds a tag with
key: Name
andValue: MyCodePipelineDemo
. In case you don't see the Tags, please add them underTags
section 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
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 balancing
is not selected. You do not need to set up a load balancer or choose a target group for this example. - Click
Create deployment group
.
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 role
to allow CodePipeline to create a service role in IAM and clickNext
-
Select
AWS CodeCommit
in Source provider. SelectMyDemoRepo
in Repository name. Selectmaster
in Branch name. SelectAmazon CloudWatch Events (recommended)
underChange detection options
. SelectCodePipeline default
underOutput artifact format
and clickNext
. -
Click
Skip build stage
as we are deploying a static website. -
Click
skip
atYour pipeline will not include a build stage. Are you sure you want to skip this stage?
-
At deploy stage, Select
AWS CodeDeploy
inDeploy provider
, SelectMyDemoApplication
inApplication name
, SelectMyDemoDeploymentGroup
inDeployment group
and 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-agent
may not be running on the EC2 instance, which we can check by running the following commandsudo 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.html
tosource: /
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: root
If 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 EventBridge
Rule
Step 9
Step 10
Step 12
-
Update the following line of HTML code in
index.html
and 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>
- Terminate
MyCodePipelineDemo
EC2 Instance. - Delete
MyFirstPipeline
underPipelines
, which will removeAmazon CloudWatch Events rule
related to the pipeline. - Delete
MyDemoApplication
underApplications
. - Delete
MyDemoRepo
underCodeCommit
>Repositories
. - Empty and delete S3 bucket prefixed with codepipeline.
- In this demo, we learnt how easy it is to setup a simple pipeline using CodeCommit, CodeDeploy and Pipeline.
- Tutorial: Create a simple pipeline (CodeCommit repository)
- Cover Image by @darya_jumelya from unsplash.