Deployment using AWS CodeDeploy and BitBucket Pipeline

Posted By:insisivecloud
Posted on:23 Apr 2019

Introduction:

Deploying any code repository manually to an EC2 machines or any dedicated on premises servers is a time consuming job. Maintainability and operating your application becomes difficult. To resolve this problem, there are different tools available in the market like Jenkins, AWS CodeDeploy,.. etc.

In this blog, we are going to deploy python code repository to AWS EC2 machines using AWS CodeDeploy and BitBucket pipeline services.

AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services. Bitbucket Pipelines is an integrated CI/CD service, built into Bitbucket. It allows you to automatically build, test and even deploy your code, based on a configuration file in your repository.

Prerequisites:

  • Valid AWS Account details. If you don’t have an AWS account then we will have one more blog for explaining
    • Signing Up in AWS
    • Launching an Auto Scaling Group
  • Valid Bitbucket account details with Pipelines access enable. If you don’t have it then upgrade your bitbucket plan.

Deployment Steps:

Configure Deployment settings in below service

  • Configuring AWS Services
  • Configuring Bitbucket pipeline
  • Integrate BitBucket and CodeDeploy
Configuring AWS Services:
Prerequisite:
  1. Login into your aws console (https://aws.amazon.com/console/).
  2. Need a S3 bucket to store the code repository. If you don’t have it then create a S3 bucket.
    • Click on https://s3.console.aws.amazon.com/s3/home?region=us-east-1 .
    • Select the S3 bucket and copy the name of the S3 bucket that you want to use for Code Deploy.
  3. Create a IAM Role for CodeDeploy Service.
    • Go to https://console.aws.amazon.com/iam/home?region=us-east-1#/roles and click on ‘Create Role’.
    • Select ‘AWS Service’ in Trusted Entities.
    • Select ‘CodeDeploy’ in ‘Choose the service that will use this role’.
    • Select ‘CodeDeploy’ in ‘Select your use case’ and click on ‘Next Permissions’.
    • Check whether ‘AWSCodeDeployRole’ is auto selected or not. If it is selected then click on ‘Create Tags’.
    • Enter Name and Value. This is an optional. If you want to skip then just click on ‘Review’.
    • Enter Name of the Role and click on ‘Create Role’.
  4. Create an EC2 Role to access the S3 Bucket.
    • Go to https://console.aws.amazon.com/iam/home?region=us-east-1#/roles and click on ‘Create Role’.
    • Select ‘AWS Service’ in Trusted Entities and select ‘EC2’ in ‘Choose the service that will use this role’ and click on ‘Next Permissions’.
    • Click on ‘Create Policy’ in attach permissions policy.
    • Click on ‘JSON’ and enter below json snippet.
    • Click on ‘Review Policy’ , enter the name of the Policy and click on ‘Create Policy’.
    • Click on ‘Refresh’ in the Create Role – Policy creation window and search for your policy name and click on ‘Next: Tags’.
    • Enter Name and Value. This is an optional. If you want to skip then just click on ‘Review’.
    • Enter Name of the Role and click on ‘Create Role’
  5. Launch an Auto Scaling Group using EC2 Role that was created in Step 4 and attach a valid Load Balancer to the Auto Scaling Group.  (Need steps to launch an Auto Scaling Group. It will be posted shortly)
Configuring AWS CodeDeploy Service:

Note: In this Blog, we are going to cover Blue – Green Deployment

  1. Go to CodeDeploy service. (https://console.aws.amazon.com/codesuite/codedeploy/applications?region=us-east-1).
  2. Click on ‘Create Application’
  3. Enter the name of the Application , Select the Compute Platform as ‘EC2/On-premises’ and click on ‘Create Application’.
  4. Once Application get created, click on ‘Create Deployment Group’.
  5. Enter the deployment name.
  6. Select the service role which we created in Configuring ‘AWS Services – Prerequisites – Step 3’ section.
  7. Select Blue-Green deployment.
  8. Select ‘Automatically copy Amazon EC2 Auto Scaling Group ‘ and select Auto Scaling Group in ‘Choose the Amazon EC2 Auto Scaling group where the current application revision is deployed’ which was configured in ‘AWS Services – Prerequisites – Step 5’
  9. Go to deployment setting and select ‘Reroute traffic immediately’ in Traffic rerouting.
  10. Select Timeline for terminating original instance after successful deployment.
  11. Select Deployment Configuration as ‘CodeDeployDefault.OneAtATime’.
  12. Select the Load Balancer (This is a mandatory field for Blue Green Deployment).
  13. If you need to RollBack to the Original State in case of deployment failure then go to ‘Advanced – Optional’ and select ‘Rollbacks’.
    • Uncheck the ‘Disable Rollbacks’.
    • Select Rollback when deployment fails.
  14. Click on ‘Create Deployment Group’.
Configuring Bitbucket Services:
  1. Create codedeploy_deploy.py in code repo. Find a sample file in (https://bitbucket.org/awslabs/aws-codedeploy-bitbucket-pipelines-python/src/master/).
  2. Create ‘bitbucket-pipelines.yml’ in the same location where codedeploy_deploy.py available.
    • If necessary, update the python version.
    • Each step will be launched in a docker. Mention commands that are to be executed as part of the deployment.
    • Keyword deployment stands to pick proper environment from bitbucket pipeline environment settings.
    • It will zip entire code repo.
  3. Create ‘appspec.yml’ file in the same location. This file will helpful to drive AWS Codedeploy deployment process. 
    • version – Version number of appspec.yml.
    • os        –  Value should be either ‘linux’ nor ‘windows’.
    • files      – Files will be downloaded from S3 bucket and move to the destination folder during ‘Install’ phase in code deployment process.
    • Hooks  – Different events that can be handled via hooks. For example, ApplicationStop, BeforeInstall, AfterInstall, ..etc.
Integrating CodeDeploy and Bitbucket Services:
  1. Login to Bitbucket server.(https://id.atlassian.com/login)
  2. Go to ‘Settings’ of your repository.
  3. Go to ‘Pipelines’ section.
  4. Click on ‘Settings’ and enable the ‘enable pipelines’.
  5. Go to Deployment section and add any variables that require at environment level like development, staging and production. You can specify same in ‘bitbucket-pipelines.yml’
  6. Go to Repository Variables and enter system level variables. Please add all below variable.

Now, It’s time to trigger your deployment process. Once you committed in the code repository, check whether Pipeline get triggered or not.

  • Go to ‘Pipelines’ section in your bitbucket repository.
  • Once ‘codedeploy_deploy.py’ step is in progress, go to AWS CodeDeploy console.
  • Check whether any deployments got triggered or not. Once deployment status changes to ‘success’ then deployment completed successfully.

Open Issues :

  1. AWS CodeDeploy: As part of Blue Green Deployment, codedeploy creates replica of current Auto Scaling Group. Target Groups/Load Balancer are not getting attach to the replace Auto Scaling Group. Need to attach it manually. As of 19-Jan-2019, AWS Team is working on it.
  2. You will receive ‘Rate Exceeded’ error in pipeline with Blue-Green Deployment mode. Please add retry mechanism in ‘codedeploy_deploy.py’ in GetDeployment method. Please find reference below https://docs.aws.amazon.com/general/latest/gr/api-retries.html .

Coming Soon :

  1. How to Launch an Auto Scaling Group.
  2. Configuring AWS CodeDeploy with ‘In-Place’ deployment mode instead of Blue-Green Deployment.
  3. Video that will help for deploying your code repository using AWS CodeDeploy and Bitbucket pipeline.

References:

https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html
https://confluence.atlassian.com/bitbucket/get-started-with-bitbucket-pipelines-792298921.html
https://bitbucket.org/awslabs/aws-codedeploy-bitbucket-pipelines-python/src/master/

Leave a Reply

Your email address will not be published. Required fields are marked *