How to Deploy Elastic Beanstalk Application using Bitbucket Pipelines

Emre Bozkurt
4 min readMay 16, 2024

--

AWS Elastic Beanstalk Image
Image Source

Understanding AWS Elastic Beanstalk

AWS Elastic Beanstalk is a service provided by Amazon Web Services that simplifies the process of deploying and managing applications in the cloud. It abstracts much of the underlying infrastructure and allows developers to focus on writing code.

Easy-to-use service for deploying and scaling web applications and services developed with various programming languages. It automates many aspects of infrastructure management. Applications hosted on Elastic Beanstalk are automatically configured with load balancing, health monitoring, auto-scaling, and other features that reduce operational overhead.

Creating IAM User for Pipelines

First of all, we’ll need to create the credentials that would be used to access Elastic Beanstalk and all other services it uses. In order to do that, we’ll need to create a new user account on your AWS account and provide it the following policy: AdministratorAccess-AWSElasticBeanstalk

Since this permission policy has full authorization over Elastic Beanstalk and all other services it uses, you can alternatively determine the permissions required only for deployment and create a permission policy with only these permissions and give it to the user.

User with attached policy
User with attached policy

In addition, there is no need to grant console access permission for this user. After creating the user, we need to create an access key and store the generated access key and secret key we created and then we will use it on bitbucket.

Generated user access key

Setup Repository on Bitbucket

First, we need to enable the bitbucket pipelines option under pipeline settings in the repository settings.

Enabled pipelines option
Enabled pipelines option

Then we add the user access key and secret key we created in AWS as repository variables to use as variables in the pipeline.

Filled repository variables
Repository variables

I will show it through the staging application in the repository here, but you can use it in your production and testing application by defining the same variables according to the environment, again from the pipeline deployment tab under the repository settings, we define the variables we set for the staging application.

Here I define it here, assuming that we use it in the worker application, if you do not use the worker application in your own application, you do not have to define WORKER_ENVIRONMENT variable.

Staging environment variables

After completing all these settings on the repository, we can create the bitbucket-pipelines.yml file.

Creating Bitbucket Pipelines Configuration File

We will create the file using Atlassian’s default pipeline image and the elastic beanstalk pipe image to deploy the elastic beanstalk application. There are several other methods to deploy the elastic beanstalk application, such as creating and running a python file in the pipeline that will do this job.

You can find more detailed information about Atlassian’s default pipeline images here or alternatively you can create your own custom image according to the requirements used in the pipeline.

https://support.atlassian.com/bitbucket-cloud/docs/use-docker-images-as-build-environments

When the pipeline is run, in the first step we wait for the application to become a package, and then we wait for the worker machine to be deployed, and in the second step, in order to ensure that the application version uploaded to S3 while the worker machine is deployed is not uploaded again because it was already uploaded the first time, we ensure that the staging environment of the application is only deployed with the deploy-only command using the same application version while deploying.

If you are not using a worker machine, you can replace the worker machine with the staging environment and upload it without using commands and then deploy it. Here pipe variables and steps can be changed and adapted according to the infra of your application.

You can access and review more detailed information about the commands used in pipe.

https://bitbucket.org/atlassian/aws-elasticbeanstalk-deploy

That’s it and thank you for reading.

Done!
Done!

Have a happy deployment, cheers.

--

--