Deploy to AWS with Elastic Beanstalk
With Elastic Beanstalk, you can quickly deploy and manage applications in the AWS Cloud without worrying about the infrastructure that runs those applications. Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.
Before you begin:
- An IAM user is configured with sufficient permissions to perform a deployment to your application and upload artifacts to the S3 bucket.
- You have configured the Elastic Beanstalk application and environment.
- Set up an S3 bucket where deployment artifacts will be copied. The name of your S3 bucket should be:
"<application name>-elasticbeanstalk-deployment", where <application name> is the name of your Elastic Beanstalk application.
Steps
Add your AWS credentials to Bitbucket Pipelines. In your repo go to Settings, under Pipelines, select Repository variables and add the following variables:
Basic usage variables
AWS_ACCESS_KEY_ID (*): Your AWS access key.
AWS_SECRET_ACCESS_KEY (*): Your AWS secret access key. Make sure that you save it as a secured variable.
AWS_DEFAULT_REGION (*): The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, see Regions and Endpoints.
APPLICATION_NAME (*): The name of the Elastic Beanstalk application.
ENVIRONMENT_NAME (*): Environment name.
ZIP_FILE (*): The application source bundle to deploy (zip, jar, war).
(*) = required variable.
Exampleimage: atlassian/default-image:2 pipelines: default: - step: name: "Build and Test" script: - pytest test/test.py - zip application.zip Dockerfile application.py cron.yaml Dockerrun.aws.json # Define an artifact to pass the zip file to the next step artifacts: - application.zip - step: name: "Deploy to Production" # Track production environments builds using deployments. deployment: production script: - pipe: atlassian/aws-elasticbeanstalk-deploy:0.5.0 variables: AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION: "us-east-1" APPLICATION_NAME: "application-test" ENVIRONMENT_NAME: 'production' ZIP_FILE: "application.zip"
Note
By default, the new application revision will have the version label
"<application name>-<bitbucket build number>-<short commit hash>" but this can be changed using the VERSION_LABEL variable.
Outcome
You're now set up to deploy a new version of your application to Elastic Beanstalk.
Advanced usage
For advanced use cases It's recommended the build once and deploy many approach. If you have multiple environments, use
COMMAND
variable to separate your CI/CD workflow into different operations/pipes:- COMMAND: 'upload-only': It will upload the artifact and release a version in Elastic Beanstalk.
- COMMAND: 'deploy-only': It will deploy the specified version to the desired environment(s)
AWS_ACCESS_KEY_ID (*): Your AWS access key.
AWS_SECRET_ACCESS_KEY (*): Your AWS secret access key.
AWS_DEFAULT_REGION (*): The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, see Regions and Endpoints.
APPLICATION_NAME (*): The name of the Elastic Beanstalk application.
COMMAND (*): Command to be executed during the deployment. Valid options are
all
,update-only
,deploy-only
. Default:all
.ZIP_FILE (*): The application source bundle to deploy (zip, jar, war).
S3_BUCKET: Bucket name used by Elastic Beanstalk to store artifacts. Default:
${APPLICATION_NAME}-elasticbeanstalk-deployment}
VERSION_LABEL: Version label for the new application revision. Default:
${ENVIRONMENT_NAME}_${BITBUCKET_COMMIT:0:8}_YYYY-mm-dd_HHMMSS)
.DESCRIPTION: Description for the new application revision. Default: a URL pointing to the pipeline result page.
DEBUG: Turn on extra debug information. Default:
false
.(*) = required variable.
Upload the artifact application.zip,
and create a version deploy-$BITBUCKET_BUILD_NUMBER-multiple
in Elastic Beanstalk.
pipe: atlassian/aws-elasticbeanstalk-deploy:0.5.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
APPLICATION_NAME: 'application-test'
COMMAND: 'upload-only'
ZIP_FILE: 'application.zip'
S3_BUCKET: 'application-test-bucket'
VERSION_LABEL: 'deploy-$BITBUCKET_BUILD_NUMBER-multiple'
Deploy-only variables
AWS_ACCESS_KEY_ID (*): Your AWS access key.
AWS_SECRET_ACCESS_KEY (*): Your AWS secret access key.
AWS_DEFAULT_REGION (*): The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, see Regions and Endpoints.
APPLICATION_NAME (*): The name of the Elastic Beanstalk application.
COMMAND (*): Command to be executed during the deployment. Valid options are
all
,update-only
,deploy-only
. Default:all
.- ENVIRONMENT_NAME (*): Environment name.
ZIP_FILE (*): The application source bundle to deploy (zip, jar, war).
S3_BUCKET: Bucket name used by Elastic Beanstalk to store artifacts. Default:
${APPLICATION_NAME}-elasticbeanstalk-deployment}
VERSION_LABEL: Version label for the new application revision. Default:
${ENVIRONMENT_NAME}_${BITBUCKET_COMMIT:0:8}_YYYY-mm-dd_HHMMSS)
.WAIT: Wait for deployment to complete. Default:
false
.WAIT_INTERVAL: Time to wait between polling for deployment to complete (in seconds). Default:
10
.DEBUG: Turn on extra debug information. Default:
false
.(*) = required variable.
Deploy your version
deploy-$BITBUCKET_BUILD_NUMBER-multiple
into the environmentproduction
and wait until the deployment to complete.Example- pipe: atlassian/aws-elasticbeanstalk-deploy:0.5.0 variables: AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION APPLICATION_NAME: 'application-test' COMMAND: 'deploy-only' VERSION_LABEL: 'deploy-$BITBUCKET_BUILD_NUMBER-multiple' ENVIRONMENT_NAME: 'production' WAIT: 'true'