Deployment using AWS CodeDeploy and BitBucket Pipeline

Posted By:insisivecloud
Posted on:23 Apr 2019

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 deploy your code, based on a configuration file in your repository.


1. Valid AWS Account details. If you don’t have an AWS account then we will have one more blog for explaining.

a. Signing Up in AWS

b. Launching an Auto Scaling Group

2. Valid Bitbucket account details with Pipelines access enabled. 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:


1.  Login into your aws console (

2. Need a S3 bucket to store the code repository. If you don’t have it then create a S3 bucket.

(i) Click on .

(ii) 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.

In case, you you don’t have the AWS IAM Role for CodeDeploy Service follow the steps mentioned below:

Step 1: Go to and click on ‘Create Role’

IAM Role for CodeDeploy Service
IAM Role for CodeDeploy Service Screen


Step 2: Select ‘AWS Service’ in Trusted Entities.

‘AWS Service trustadvisor'
AWS Service in Trusted Entities.


Step 3:  Select ‘CodeDeploy’ in ‘Choose the service that will use this role’.

AWS CodeDeploy insisive cloud
AWS CodeDeploy


Step 4: 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’.

AWS CodeDeploy’ Next Permissions Screen


Check whether ‘AWSCodeDeployRole’ is auto selected or not. If it is selected then click on ‘Create Tags’.

AWSCodeDeployRole create Tags insisive cloud
AWSCodeDeployRole create Tags


Step 5: Enter Name and Value. This is an optional. If you want to skip then just click on ‘Review’.

Step 6: Enter Name of the Role and click on ‘Create Role’.

Enter Name of the Role Create Role insisivr cloud
Enter Name of the Role and click ‘Create Role’.


4.  Create an EC2 Role to access the S3 Bucket.

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

Create role in EC2 insisive cloud

  • Click on ‘Create Policy’ in attach permissions policy.
    • Create Permissions policy insisive cloud
    • Click on ‘JSON’ and enter below JSON snippet. 

Json insisive cloud 

  • 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 the Name of the Role and click on ‘Create Role’.

insisive cloud


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. (
  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’.

CodeDeploy_Application_Creation insisive cloud

     4. Once Application get created, click on ‘Create Deployment Group’.

Deployment blog insisive cloud

       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 in code repo. Find a sample file in (
  2. Create ‘bitbucket-pipelines.yml’ in the same location where available.

Bitbucket_pipeline_yml insisive cloud

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

Bitbucket_AppSpec deployment process insisive cloud

      • 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.(
  2. Go to ‘Settings’ of your repository.
  3. Go to ‘Pipelines’ section.
  4. Click on ‘Settings’ and enable the ‘enable pipelines’.

Integration_Enable_Pipeline insisive cloud

   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.

Integrate_Env insisive cloud

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 ‘’ 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.

Learn : How to Statically Host a Website using AWS S3

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 ‘’ in GetDeployment method. Please find reference below .



Make the most of your cloud and start saving upto 60% with insisive cloud. For any queries drop mail at: