Using bitbucket pipelines to deploy to your AWS Opsworks stack.

Recently Bitbucket introduced their Pipelines beta feature. While it has some examples on how to leverage the power of these Piplelines on AWS it didn't have a proper example on how to deploy your app on AWS Opsworks.

If you do want to however, this is an approach you could use to do so.

Enable pipelines on your repository. If provided a sample yml file to start off with, you could use the below one or keep the defaults to overwrite in the next step.

Use the below example (bitbucket-pipelines.yml) to have it kick off a deployment on your (existing) application in an Opsworks Stack.

# This is a sample build configuration for Javascript.
# Check our guides at for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.

image: python:3.4.4

    - step:
        script: # Modify the commands below to build your repository.
          #- npm install
          #- npm test
          - chmod +x deploy_opsworks.bash
          - ./deploy_opsworks.bash

The 'deploy_opsworks.bash' script uses environment variables you can set within your repository at Bitbucket. It uses those environment variables passing it to the aws cli to deploy your application in the right stack. The file is quite self explanatory. (For some reason I had to specify that the file is executable, otherwise it would not execute the bash script.)

#!/bin/bash -ex

echo "Installing awscli"

pip install awscli

echo "Deploying application"

aws opsworks --region us-east-1 create-deployment --stack-id $STACK_ID --app-id $APP_ID --command "{\"Name\":\"deploy\"}"

curl $SENTRY_URL -X POST -H 'Content-Type: application/json' -d '{"version": "'$BITBUCKET_COMMIT'"}'

As you can see, I also tell Sentry that a new release has been deployed, but you can leave this step out of course.