How to use AWS CodeDeploy to Effortlessly Deploy a Flask app to EC2?

Contents

  • Introduction 
  • Create an IAM Role 
  • Create a Code Commit Repository 
  • Launch EC2 Instance and Install Code Deploy Agent 
  • Create CodeDeploy Application 
  • Create CodePipeline 
  • Conclusion 

Introduction

  • ​​​​​​​In this article, we learn continuous deployment using the AWS Code pipeline. Use AWS CodePipeline to deploy code maintained in a CodeCommit repository to a single Amazon EC2 Instance. The Codepipeline is triggered when you push a change to the CodeCommit repository. CodeDeploy service is used for deploying your changes to an Amazon EC2 instance. 

Create IAM Role

  • 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. 

Create a Code Commit Repository

  • 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: 

  • git clone https://git-codecommit.us-east-2.amazonaws.com/v1/repos/zehncloud-pipeline-repo 

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: <User Name> 
​
  ApplicationStart: 
​
  - location: scripts/start-application.sh 
​
    runas: <User Name> 
​
    timeout: 900 
​
  ApplicationStop: 
​
  - location: scripts/stop-application.sh 
​
    runas: <User Name> 
​
    timeout: 900 
​
  AfterInstall: 
​
  - location: ./scripts/after-install.sh 
​
    runas: <User Name> 
​
    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.

Launch EC2 Instance and Install Code Deploy Agent

  • Open the Amazon EC2 console from https://console.aws.amazon.com/ec2/
  • From the dashboard select Launch Instances 
  • Choose Amazon Machine Image (AMI) you want to launch and click on select. 
  • Choose the Instance type, select t2.micro, which is free tier eligible and click on Next: Configure Instance Details. 
  • On the Configure Instance page, choose the following: 
    Number of Instances: 1 
    ​​​​​​​Auto-assign public IP: Enable. 
  • In the IAM role, choose the previously created IAM Role for EC2 “zehncloud-EC2-pipelineRole”. 
  • Configure Instance Details page remain unchanged. Choose Next: Add Storage. 
  • Add Storage page remain unchanged and then choose Next: Add Tags. 
  • Click on Add Tag. In Key, enter Name, and in Value, enter some value for identifying Instance, Example: ZehncloudPipelineInstance. Choose Next: Configure Security Group. 
  • Click on Review and Launch. 
  • On the Review Instance Launch page, click on Launch. 

Install Code Deploy Agent

​​​​​​​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
				
			

Create CodeDeploy Application

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. 

Create a Deployment Group:

  • 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. 

Create CodePipeline

  • 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. 

Conclusion

​​​​​​​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.