Bitbucket Pipelines with AWS Elastic Beanstalk

Conor O'Dwyer
Level Up Coding
Published in
5 min readOct 29, 2020

--

In this tutorial, I will demonstrate how to use Bitbucket Pipelines to automate deployment updates to AWS Elastic Beanstalk when code is committed. A simple Spring Boot application will be used as an example application deployed on Elastic Beanstalk.

This tutorial assumes a basic understanding of Git, AWS, and that you have a Spring Boot project that you want to deploy on Elastic Beanstalk using Bitbucket Pipelines.

1. Bitbucket Repository Set Up

If you do not already have a Bitbucket account, you will need to create one here. If your repository is already hosted on Bitbucket you can skip to the next section, otherwise you will need to create a new repository for your Spring Boot application, as shown.

If you have an existing local repository but do not yet have it linked to a remote repository, you can follow the instructions in your new Bitbucket repository.

If you have an existing Git repository but it is hosted on a different repository such as GitHub, you can change it to point to your newly created Bitbucket repository using the following command from your local repository directory.

git remote set-url origin https://username@bitbucket.org/username/repository_name.git

2. Deploy Spring Boot on Elastic Beanstalk

If you do not already have an AWS account setup, you will need to create an account here. Everything in this tutorial is within the free tier usage limits.

If you already have your Spring Boot application deployed on Elastic Beanstalk, you can skip ahead to the next section.

You will firstly need to package your Spring Boot application as a .jar file using the mvn package command. This will create your .jar file in the /target directory according to the artifactId and version in your pom.xml file.

In the AWS console, navigate to Elastic Beanstalk and select Create Application. Enter an Application name (typically matches your Spring Boot project/repository name) and choose the Java platform branch and version which is correct for your application. Select “Upload your code” in the Application code section and upload your .jar file in the Source code origin section. Once the file is uploaded, click Create application. This will create your Elastic Beanstalk environment and application.

Once your application is deployed, you will see the Health has a status of Green. You will also be able to see the Environment name and Application name from this window, which you will need later.

You are now ready to configure Bitbucket Pipelines.

3. Enable Bitbucket Pipelines

Now that you have your Bitbucket repository setup and application running on Elastic Beanstalk, you will need to enable Bitbucket Pipelines. From your repository, click on Repository settings in the menu, scroll to the Pipelines section and select Settings. You will need to Enable Pipelines.

You will then need to create a bitbucket-pipelines.yml for your repository. You can either create this directly on Bitbucket or in your local repository and push the new file to Bitbucket. This file should be in the root directory of your project, beside the pom.xml file. Below is an example of a bitbucket-pipelines.yml file for updating your Elastic Beanstalk application.

image: maven:3.6.1pipelines:
default:
- step:
caches:
- maven
script:
- mvn clean package
artifacts:
- target/*
- step:
caches:
- maven
script:
- pipe: atlassian/aws-elasticbeanstalk-deploy:0.6.6
variables:
AWS_ACCESS_KEY_ID: '$AWS_ACCESS_KEY_ID'
AWS_SECRET_ACCESS_KEY: '$AWS_ACCESS_KEY_SECRET'
AWS_DEFAULT_REGION: 'eu-west-1'
APPLICATION_NAME: 'spring-aws-pipeline'
ENVIRONMENT_NAME: 'SpringAwsPipeline-env'
ZIP_FILE: 'target/spring-aws-pipeline-$version.jar'

The image at the beginning of the file specifies a Docker image that is used for your pipeline. In this case it is a maven image as that is required to package our Spring Boot application. In the pipelines section, the steps to be executed are specified.

The first step packages your application using the “mvn clean package” command (if you run any additional commands when packaging your application, put them here) and creates an artifact of the target/ directory where your .jar file is packaged.

The second step uses the aws-elasticbeanstalk-deploy pipe to deploy the newly created .jar file to your Elastic Beanstalk application. Using the $ sign in the bitbucket-pipelines.yml file specifies an environment variable which will be setup in Bitbucket. This is used for $AWS_ACCESS_KEY_ID, $AWS_ACCESS_KEY_SECRET and $version above. You will need to create an Access Key ID and Secret for your AWS environment with full permissions for Elastic Beanstalk if you do not already have them. AWS_DEFAULT_REGION, APPLICATION_NAME and ENVIRONMENT_NAME should be replaced with the values for your Elastic Beanstalk application. The “spring-aws-pipeline” part of the ZIP_FILE should be replaced with your Spring Boot application name.

You will then need to set the $AWS_ACCESS_KEY_ID, $AWS_ACCESS_KEY_SECRET and $version variables on Bitbucket. From your repository, click on Repository settings in the menu, scroll to the Pipelines section and select Repository variables. The version variable must match the version in the pom.xml file of your application (you will need to update this manually on Bitbucket each time it is updated for your pom.xml file).

4. Conclusion

You have now successfully configured your Spring Boot application and Bitbucket to automatically deploy to Elastic Beanstalk every time you commit to the master branch of your repository. When you push the updates for your bitbucket-pipelines.yml file, you will be able to view the Pipeline execution status by clicking on the Pipelines section in the menu. You should see that your Pipeline has been “Successful”.

--

--