Continuous Deployment : Using AWS Code Commit, AWS Code Deploy and Jenkins (Part 4)

Use AWS CodeDeploy to Deploy  Amazon EC2 Instances Behind an Elastic Load Balancer and Auto Scaling
Let’s assume you have an application architecture designed for high availability that includes an Elastic Load Balancer in front of multiple application servers belonging to an Auto Scaling Group. Elastic Load Balancing enables you to distribute incoming traffic over multiple servers and Auto Scaling allows you to scale your EC2 capacity up or down automatically according to your needs In this blog post, we will show how you can use CodeDeploy to avoid downtime when updating the code running on your application servers in such an environment. We will use the CodeDeploy rolling updates feature so that there is a minimum capacity always available to serve traffic and use a simple script to take EC2 instances out of the load balancer as and when we deploy new code on it.
So let’s get started. We are going to:
  • Set up the environment with Elastic Load Balancer and Auto Scaling Group
  • Set up the Jenkins to pull the latest Code from AWS CodeCommit Repo
  • Build a application bundle, which includes the deployment scripts, and upload it to Amazon S3
  • Create an AWS CodeDeploy application and a deployment group
  • AWS Code Deploy will pull the zip file in all the Auto Scaled Instance
  • Start the zero-downtime deployment
  • Monitor your deployment
Set up the AWS environment
You can skip this step if already you have a AWS infrastructure that have Elastic Load Balancer and Auto Scaling Group configured.
Let’s get started by setting up some AWS resources
To simplify the setup process, you can use a sample AWS CloudFormation template that sets up the following resources for you:
  1. An Auto Scaling group and its launch configuration. The Auto Scaling group launches by default three Amazon EC2 instances. The AWS CloudFormation template installs Apache on each of these instances to run a sample website. It also installs the AWS CodeDeploy Agent, which performs the deployments on the instance. The template creates a service role that grants AWS CodeDeploy access to add deployment lifecycle event hooks to your Auto Scaling group so that it can kick off a deployment whenever Auto Scaling launches a new Amazon EC2 instance.
  2. The Auto Scaling group spins up Amazon EC2 instances and monitors their health . The Auto Scaling Group spans all Availability Zones within the region for fault tolerance.
  3. An Elastic Load Balancing load balancer, which distributes the traffic across all of the Amazon EC2 instances in the Auto Scaling group
Simply execute the following command using the AWS Command Line Interface (AWS CLI), or you can create an AWS CloudFormation stack with the AWS Management Console by using the value of the –template-url option shown here:
aws cloudformation create-stack \
 --stack-name "CodeDeploySampleELBIntegrationStack" \
 --template-url "http://s3.amazonaws.com/aws-codedeploy-us-east-1/templates/latest/CodeDeploy_SampleCF_ELB_Integration.json" \
 --capabilities "CAPABILITY_IAM" \
 --parameters "ParameterKey=KeyName,ParameterValue=<my-key-pair>"
Note: AWS CloudFormation will change your AWS account’s security configuration by adding two roles. These roles will enable AWS CodeDeploy to perform actions on your AWS account’s behalf. These actions include identifying Amazon EC2 instances by their tags or Auto Scaling group names and for deploying applications from Amazon S3 buckets to instances. For more information, see the AWS CodeDeploy service role and IAM instance profile documentation.
Create AWS CodeDeploy Application
AWS CodeDeploy is a flexible and reliable deployment configuration and management system that enables you to deploy and update applications running on instances while avoiding downtime. You can view, create, and diagnose applications. Select an application to view details and deploy your application to instances.
Go to AWS CodeDeploy -> Get Started -> Custom Deployment This will bring you to Create New Application page.
CodeDeploy
Fill in the Application Name and Deployment Group Name
The next part is selecting the instances where you want to deploy the application. An Auto Scaling group of EC2 instances running with Apache and the CodeDeploy agent are installed and fronted by an Elastic Load Balancing. In Add Instances part you can search and select your Instances or AutoScaling Group by tag names.
Here we select the instance that we had launched.
tagnames
Deployment Configuration
Choose from a list of default and custom deployment configurations. A deployment configuration is a set of rules that determines how fast an application will be deployed and the success or failure conditions for a deployment.
Deployment Config , here we can set it to CodeDeployDefault.OneAtATime.
Service Role , here we attach the role CodeDeploy that we had created.
Finally select Create Application. Here we finish with configuring AWS CodeDeploy
Now next part would be to configure Jenkins.
Create a Project and Configure the CodeDeploy Jenkins Plugin
Now we’ll create a project in Jenkins and configure the Jenkins plugin to poll for code updates from the AWS CodeCommit repository.
Create a freestyle project by clicking New Item option
jenkins
On the project configuration page, under Source Code Management, choose Git. Paste the URL you noted when you created the AWS CodeCommit repository
repo
In Build Triggers, select the Poll SCM check box. In the Schedule text field, type H/2 * * * *. This tells Jenkins to poll CodeCommit every two minutes for updates. (This may be too frequent for production use, but it works well for testing because it returns results frequently.)
build
Under Post-build Actions, choose Add post-build actions, and then select the Deploy an application to AWS CodeDeploy check box.
post-build
Choose Use temporary credentials, and then paste the value of CodeDeployRoleArn that we noted before
JenkinsCodeDeployRoleArn
Or you can use your IAM User Access Keys and Secret keys
Note the External ID field displayed on this page. This is a unique random ID generated by the CodeDeploy Jenkins plugin. This ID can be used to add a condition to the IAM role to ensure that only the plugin can assume this role. To keep things simple, we will not use the External ID as a condition, but we strongly recommend you use it for added protection in a production scenario, especially when you are using cross-account IAM roles.
Choose Test Connection. Confirm the text “Connection test passed” appears, and then choose Save to save your settings
Next to create an Application bundle, which includes the deployment scripts, and upload it to Amazon S3

Comments

Popular posts from this blog

Datastax Error : Cannot start node if snitch's data center (dc1) differs from previous data center (dc2)

Datastax Error : Cassandra - Saved cluster name Test Cluster != configured name

Configure Nagios plugin " check_logfiles " for scanning log file

Popular posts from this blog

Datastax Error : Cannot start node if snitch's data center (dc1) differs from previous data center (dc2)

Datastax Error : Cassandra - Saved cluster name Test Cluster != configured name

Configure Nagios plugin " check_logfiles " for scanning log file