Skip to content

Deploy to blue/green environments on AWS Elastic Beanstalk

License

Notifications You must be signed in to change notification settings

tmshkr/blue-green-beanstalk

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

blue-green-beanstalk

GitHub Action to automate blue/green deployment with AWS Elastic Beanstalk.

The action will create the following resources:

  • An Elastic Beanstalk application, if it doesn't already exist.
  • An Elastic Beanstalk application version, if it doesn't already exist.
  • An Elastic Beanstalk environment, if it doesn't already exist.

Getting Started

The easiest way to get started with this action is to use the example repo as a template.

Blue/Green Deployment

Based on the provided inputs, the action will determine which environment is the target environment, to which a new application version should be deployed.

The action uses the values of the production_cname and staging_cname inputs to determine which environment is the production or staging environment. Accordingly, the production CNAME should always point to the production environment, and the staging CNAME should always point to the staging environment.

If neither environment exists, the action will create a new environment with the production_cname input. If the production environment already exists, the action will target the staging environment, creating it if it doesn't exist.

After deploying, the action will swap the CNAMEs of the staging and production environments, if swap_cnames is set to true.

Example Blue/Green Deployment

See the example repo for an example of how to use this action. The example repo also contains a CloudFormation template to create the necessary IAM resources required to run the action using the GitHub OIDC provider.

name: Example Deploy Workflow
  on:
    workflow_dispatch:
    push:
      branches:
        - main
        - staging

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ vars.AWS_ROLE }}
          aws-region: ${{ vars.AWS_REGION }}
      - name: Generate source bundle
        run: zip -r bundle.zip . -x '*.git*'
      - name: Deploy
        uses: tmshkr/blue-green-beanstalk@v5
        with:
          app_name: "test-app"
          blue_env: "my-blue-env"
          deploy: true # Must be set to true to deploy
          green_env: "my-green-env"
          platform_branch_name: "Docker running on 64bit Amazon Linux 2023"
          production_cname: "blue-green-beanstalk-prod"
          source_bundle: "bundle.zip"
          staging_cname: "blue-green-beanstalk-staging"
          swap_cnames: ${{ github.ref_name == 'main' }}
          version_description: ${{ github.event.head_commit.message }}
          version_label: ${{ github.ref_name }}-${{ github.sha }}

Inputs

Required Inputs

Name Description
app_name Name of the Elastic Beanstalk application. If it doesn't exist, it will be created.

Dependent Inputs

Inputs that must be provided together.

Name Description Needs
blue_env Name of the blue environment. green_env, production_cname, staging_cname
green_env Name of the green environment. blue_env, production_cname, staging_cname
production_cname CNAME prefix for the domain that serves production traffic when performing a blue/green deployment. blue_env, green_env, staging_cname
single_env Name of a single environment to deploy. If specified, the blue/green environments and production/staging CNAMEs must not be provided. single_env_cname
single_env_cname CNAME prefix to use for a single-environment deployment. single_env
staging_cname CNAME prefix of the staging environment for a blue/green deployment. blue_env, green_env, production_cname

Optional Inputs

Name Default Description
aws_access_key_id AWS Access Key ID. May instead be specified via the AWS_ACCESS_KEY_ID environment variable.
aws_region AWS region. May instead be specified via the AWS_REGION environment variable.
aws_secret_access_key AWS Secret Access Key. May instead be specified via the AWS_SECRET_ACCESS_KEY environment variable.
aws_session_token AWS session token for using temporary credentials. May instead be specified via the AWS_SESSION_TOKEN environment variable.
create_environment true Whether to create a new environment during deployment if the target environment doesn't exist.
deploy false Whether to deploy to the target environment.
disable_termination_protection false Whether to disable termination protection for the target environment's CloudFormation stack before attempting to terminate it.
enable_termination_protection false Whether to enable termination protection for the target environment's CloudFormation stack after deployment.
minimum_health_color Green Minimum health color (Green, Yellow, Red, or Grey) required for the target environment to be considered healthy.
option_settings Path to a JSON file consisting of an array of option settings to use when updating an existing evironment or creating a new environment.
platform_branch_name Name of the platform branch to use. When creating a new environment, it will be launched with the latest version of the specified platform branch. To see the list of available platform branches, run the aws elasticbeanstalk list-platform-branches command.
send_command Command to send to the target environment after (optional) deployment. Specify a shell script or series of commands to run. Useful for running tasks related to deployment, such as swapping environment variables before promoting an environment to production.
source_bundle Path to the source bundle to deploy. If not specified, the sample application will be used.
swap_cnames false Whether to swap the CNAMEs of the blue and green environments.
template_name Name of an Elastic Beanstalk configuration template to use when creating a new environment.
terminate_unhealthy_environment false Whether to terminate an unhealthy target environment. If set to false, the action will fail if the target environment is unhealthy.
update_environment true Whether to update an existing environment during deployment.
update_listener_rules false When set to true, the action will update any elbv2 listener rules on the environments' SharedLoadBalancer tagged with a bluegreenbeanstalk:target_cname key, whose value is equal to an environment's CNAME prefix, so that it forwards to the corresponding target group.

For example, you can tag a listener rule that matches the host my-domain.com with a tag containing bluegreenbeanstalk:target_cname as its key and my-prod-cname as its value, and the action will update the listener rule to point to the same target group as the environment with the CNAME my-prod-cname.

The listener rules are updated after the CNAMEs are swapped, so this keeps the listener rules in sync with the CNAMEs.

The port of the process may be specified by another tag with the key bluegreenbeanstalk:target_port whose value is the port number. If no such tag is found, the default port of 80 will be used.

If you'd only like to update the listener rules for a certain CNAME, you can set this input to the CNAME prefix you'd like to update.
use_default_option_settings true Whether to use default option settings when creating a new environment.
version_description Description to use for the new application version.
version_label Version label to use for the new application version.
wait_for_command true Whether to wait for the send_command execution to complete.
wait_for_deployment true Whether to wait for the deployment to complete.
wait_for_environment true Whether to wait for the target environment to be ready before deployment. If set to false, the action will fail if the target environment is not ready.
wait_for_termination true Whether to wait for an environment to be terminated. If set to false, the action will fail if the target environment is terminating.

Outputs

Name Description
target_env_cname CNAME of the target environment.
target_env_endpoint_url URL of the environment's load balancer, or an Elastic IP address when using a single instance environment.
target_env_id ID of the target environment.
target_env_json JSON representation of the target environment.
target_env_name Name of the target environment.
target_env_status Status of the target environment.

About

Deploy to blue/green environments on AWS Elastic Beanstalk

Resources

License

Stars

Watchers

Forks

Packages

No packages published