Create AWS Roles and assign permission for accessing Code Deploy, Code Pipeline, EC2, and CloudWatch services.
Create a Role for Amazon EC2 Instance to access code commit, code Deploy:
Open the IAM console.
From the left panel, click on Roles. Then click on Create role.
Select AWS Service under the trusted entity, choose EC2 under use case, Click in Next: Permission.
Select the permissions for EC2, choose AmazonEC2RoleforAWSCodeDeploy, AWSCodeCommitFullAccess. Click on Next: tags. Then click on Next: Review.
On the review page, enter the Role name (example: zehncloud-EC2-pipelineRole), Role description and click on Create role.
Open the CodeCommit console from https://console.aws.amazon.com/codecommit/
Click on Create repository.
On the Create repository page, enter the name of the repository, add a description (optional), then click on Create.
Setup the local repository, and clone the HTTPS URL as follows:
Setup a sample code to your code commit repository, your file structure should be like this:
/tmp
└– zehncloud-pipeline-repo
│– appspec.yml
│– index.html
│– LICENSE.txt
└– scripts
│– install_dependencies
│– start_server
└– stop_server
You need an appspec.yml file and a couple of things to support your codepipeline.
version: 0.0
os: linux
files:
- source: /
destination: /path/to/your/destination/file-location
hooks:
BeforeInstall:
- location: ./scripts/before-install.sh
timeout: 900
runas:
ApplicationStart:
- location: scripts/start-application.sh
runas:
timeout: 900
ApplicationStop:
- location: scripts/stop-application.sh
runas:
timeout: 900
AfterInstall:
- location: ./scripts/after-install.sh
runas:
timeout: 900
Explanation of appspec.yml file hooks.
For CodeDeploy, an EC2/On-Premises deployment hook is executed once per deployment to an EC2 instance. You can specify your scripts to execute in a hook. Each hook in a lifecycle event is specified on a separate line.
version: version number
os:operating-system-name
files:
source:source-files-location
destination:destination-files-location
hooks:
deployment-lifecycle-event-name:
location:script-location
timeout:timeout-in-seconds
runas:user-name
# Here is the description of hooks available for appspec.yml file.
Start: The LIfecycle event automatically executes this hook, which initiates the Instance for deployment.
AppilicationStop: You can write a script for this hook, which is used to stop the application. This lifecycle event occurs even before the revision bundle is downloaded.
DownloadBundle: This event is not used for running scripts, but is it used by the CodeDeploy agent to download a new version of your application.
ApplicationStart: You can use this lifecycle event to run the script and start the application.
Install: This lifecycle event is also reserved for the CodeDeploy agent and is used to copy the revision files to the final destination, which you specified.
BeforeInstall: This event is used for preinstalling tasks, if you want to create a backup of the current version of your files, or do some cleanup activity, remove a folder, etc.
AfterInstall: This event is used for changing file permission or changing the configuration before the application start.
ValidateService: This event is used for executing validate logic or determining whether the deployment was successfully completed.
Run the following commands to install CodeDeploy Agent.
sudo apt-get update
sudo apt-get install ruby
sudo apt-get install wget
cd /home/ubuntu
wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
or
sudo wget https://aws-codedeploy-us-west-2.s3.amazonaws.com/latest/install
sudo chmod +x ./install
To deploy the CodeDeploy agent on Ubuntu 14.04, 16.04, and 18.04:
sudo ./install auto
sudo ./install auto > /tmp/logfile
To check the status of the CodeDeploy agent:
sudo service codedeploy-agent status
From the AWS Console, select the CodeDeply service from the search box.
From the CodeDeploy console, from the left panel click on Applications, then click on Create application.
Enter the Application Name and choose a compute platform. Click on Create application.
Click on create deployment group button.
Enter the name of the deployment group, and select a Service role.
You can choose either In-place or Blue/green deployment for the deployment type.
For Environment configuration, click on the Amazon EC2 instances checkbox.
Enter the tag key “Name” and value “ZehncloudPipelineInstance”, which you created for EC2 Instance.
For the Deployment settings you can choose CodeDeploy for All instance at once, One at a time, or Half at a time.
Untick the Enable load balancer check box. And leave the Advanced settings as default.
And click on Create deployment group.
Under the Codepipeline dashboard, from the left panel, click on pipelines.
Click on Create pipeline.
Enter the pipeline name, and select an existing Service role or a New service role.
Under the Advanced settings, select the Default location for Artifact store, and Default AWS Managed key for an encryption key.
Click on Next.
On the Add source stage page, select the AWS CodeCommit as the source provider. Enter the Repository name, and Branch name and choose Amazon Cloudwatch Events for the detection option, and for output artifact, select Codepipeline default. Then click on Next.
Skip the build stage.
On the Add deploy stage page, select AWS CodeDeploy as the deploy provider. Enter the region, Application name, and deployment group. Then click on Next.
Review the Codepipeline settings and click on Create pipeline.
The two-stage pipeline is under processing.
You can view the event by clicking on details on the Deploy stage.
Once all the event hooks are executed successfully, the code deployment is completed.
Whenever you make the changes to your code and push the code to the code commit repository, the pipeline runs automatically, and the changes are visible to your web address.