Deploy Codes to AWS from Bitbucket

Below is an example Bitbucket pipeline that deploys an artifact built from a Maven project to AWS Elastic Beanstalk:

image: maven:3.3.9

pipelines:
   default:
      - step:
         caches:
            - maven
         script: # Modify the commands below to build your repository.
            - mvn -B verify # -B batch mode makes Maven less verbose
   custom:
      deploy_to_dev:
         - variables: #list variable names under here
            - name: version
            - name: AWS_ACCESS_KEY_ID
            - name: AWS_SECRET_ACCESS_KEY
         - step:
            caches:
              - maven
            script: # Modify the commands below to build your repository.
              - mvn -B verify # -B batch mode makes Maven less verbos
            artifacts: 
              - target/* 
         - step:
            name: "Deploy to Dev01"
            deployment: dev01
            script:
               - pipe: atlassian/aws-elasticbeanstalk-deploy:0.2.1
               variables:
                  AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
                  AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
                  AWS_DEFAULT_REGION: "<your region>"
                  APPLICATION_NAME: "<your application name>"
                  ENVIRONMENT_NAME: "<your environment name>"
                  ZIP_FILE: "target/<artifact name>-${version}.jar"
                  S3_BUCKET: "<S3 repo for jar files>"
                  VERSION_LABEL: "<artifact name>-${version}-${BITBUCKET_COMMIT:0:8}"

Notes

  1. A custom pipeline deploy_to_dev is created here to be run manually in Bitbucket for deploying a jar file built by Maven.
  2. I use variables here to supply the AWS access key id and secret access key values. You can also set them up as pipeline environment variables as suggested in the Bitbucket documentation.
  3. I also add the variables version to specify the version to be deployed. This should be the same as the package version in the Maven pom file.
  4. The line “deployment: dev01” marks the step as a deployment. A deployment with the name dev01 has to be added to the Bitbucket repository under Settings->Pipelines->Deployments.
  5. I append the version label in Elastic Beanstalk with the commit id in the last line (VERSION_LABEL:…). This will allow me to deploy multiple snapshot versions.

BitBucket also supports deployments to AWS S3 and using CodeDeploy. See their documentation here for details.

Refer to the Git Repository for the Elastic Beanstalk implementation here for more information about variables available and example pipelines.