💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to deploy apps to AWS Elastic Beanstalk with GitHub Actions

einaregilsson/beanstalk-deploy -
GitHub Action
v22
616
Contributors
Contributor - keevieContributor - phamtriduyContributor - MicaelJarniac
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: Deploy master
on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:

- name: Checkout source code
uses: actions/checkout@v2

- name: Generate deployment package
run: zip -r deploy.zip . -x '*.git*'

- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v22

beanstalk-deploy logo

beanstalk-deploy

GitHub action (and command line script) to deploy apps to Elastic Beanstalk


The content explains how to use the beanstalk-deploy GitHub action to deploy applications to AWS Elastic Beanstalk. It involves a creation package, action configuration, optional parameters, and security considerations.

Now, this is a fully automated deployment through GitHub Actions into AWS Elastic Beanstalk, keeping our applications updated with the hardly earned changes. It also follows security best practices, thus securing the deployment process.

Automated deployment to AWS Elastic Beanstalk makes this process smooth, cost-effective, and reduces manual interventions and errors. This way, it also ensures that it is secure by sticking to the permissions and best practices of AWS IAM.

How to use the beanstalk-deploy action in GitHub Actions?

name: Deploy to master
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2
- name: Generate deployment package
run: zip -r deploy.zip. -x '*.git*'
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v22
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: MyApplicationName
environment_name: MyApplication-Environment
version_label: 12345
region: us-west-2
deployment_package: deploy.zip

It will create a deployment package, publish to S3. It is supposed to wait until the deployment has completed and logs all messages being sent from the environment.

How to deploy an already existing version to AWS Elastic Beanstalk

  • Deploy Existing Version:
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v22
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: MyApplicationName
environment_name: MyApplication-Environment
version_label: 12345
region: us-west-2

The deployment_package input is optional for not using a deployment package and deploying an already created version and, with this being used, the version_label will need to be passed to the action. This is handy for redeploying already built versions without having to regenerate them.

Optional parameters: aws_session_token Temporary security credentials use_existing_version_if_available Will deploy an existing version, if available. Otherwise, we will create it from the deployment package wait_for_deployment, which Indicates whether to wait for the deployment to complete. Default: accurate wait_for_environment_recovery Wait time for the environment to be Green after deployment. version_description Description of the version being created.

  • environment_name Defaults to an empty value in version 10; when omitted, the action creates the version but does not deploy the version.
  • existing_bucket_name Name of existing bucket to which the deployment package will be uploaded.
  • max_backoff_retries Number of exponential back-off retries. When omitted, value defaults to 10.

The optional parameters were for having maximum flexibility in the deployment process. At the same time, it allows the user to do a lot more specific configurations for the deployment at a use case level that one might end up facing in the field.

How to make sure AWS permissions will be in the right place?

  • AWS Permissions:
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"RegisterTaskDefinition",
"Effect":"Allow",
"Action":[
"ecs:RegisterTaskDefinition"
],
"Resource":"*"
},
{
"Sid":"PassRolesInTaskDefinition",
"Effect":"Allow",
"Action":[
"iam:PassRole"
],
"Resource":[
"arn:aws:iam::<aws_account_id>:role/<task_definition_task_role_name>",
"arn:aws:iam::<aws_account_id>:role/<task_definition_task. These policies help to support the given action to register task definitions, pass IAM roles, and deploy services.

## How to manage support of AWS CodeDeploy for ECS services?
- **Deploy to ECS with CodeDeploy**:
```yaml
- name: Deploy to Amazon ECS
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: task-definition.json
service: my-service
cluster: my-cluster
wait-for-service-stability: true
codedeploy-appspec: appspec.json
codedeploy-application: my-codedeploy-application
codedeploy-deployment-group: my-codedeploy-deployment-group

Additional configuration is needed for ECS services using the CodeDeploy deployment controller. Ensuring the action has the necessary permissions for CodeDeploy operations is crucial for successful deployments.

How to use beanstalk-deploy as a command line program?

beanstalk-deploy.js MyApplicationName MyApplication-Environment 12345 us-west-2 deploy.zip

The best part is that this command line app can be used outside GitHub Actions in performing deployments directly from our command line with the same parameters and setup.