Skip to content

amaysim-au/docker-ecs-utils

Repository files navigation

ECS Utils

A collection of scripts for deploying AWS ECS Services.

Features

  • Fully automated deployments
  • Blue/green (or even rainbow) deployments
  • Uses AWS Application Load Balancer routing rules for blue/green, not DNS
  • Uses 3 Musketeers

Prerequisites

Requires Make, Docker and docker-compose to run this utility. To deploy, you will need a functional ECS Cluster, as well as an application stack that contains the load balancer.

Targets

Deploy

Running make deploy deploys a version of your application to ECS. The script can derive the version from your CI tool (e.g. GO_PIPELINE_LABEL) or you can statically set it for in-place deployments.

The script does the following:

  • The script queries the application's ALB to determine the next available priority/order
  • Parses .env to generate a list of environment variable keys, and then grabs the values from the running environment (i.e. os.environ.get('MY_VAR')).
  • The script generates the task definition from the file at deployment/ecs.json as well as the environment variables gathered in the previous step and uploads it to ECS.
  • Create a CloudFormation stack using the template at scripts/ecs-cluster-application-version.yml.
  • The script will then poll until this stack is succesfully created. Succesful creation involves the ECS succesfully starting the containers and registering them to the target group.
  • The script polls the Target Group to ensure that all healthchecks are passing.
  • A URL for this specific version is output.

Cutover

Once you are ready for the version you've deployed to start receiving live traffic, you can do a cutover by running make cutover.

The cutover script changes the ALB's default rule to point to the version you deployed. Changing ALB rules is atomic and instantly takes effect.

After cutting over, the old versions are not automatically removed. This is so you can instantly cut back by running the cutover stage in the old pipeline.

Auto Cleanup

At some point you will want to clean up old deployments. Running make autocleanup will remove any versions that are not live by deleting the CloudFormation stacks.

Cookiecutter Template

You can use this repo to create your own ECS project using cookiecutter.

Install the latest version of Cookiecutter:

pip install -U cookiecutter

Generate your ECS project:

cookiecutter https://github.com/amaysim-au/docker-ecs-utils

You can find an example of a generated project in example/.

Updating ecs-utils image

This project is auto-deployed to Docker Hub upon GitHub Master/Tag changes.

latest

Before committing anything to master, make sure everything works locally: $ make dockerBuild lint test. Once a commit is done to master, Docker Hub builds it and tags the image to latest.

Semantic version

Once you are happy with your changes, it is time to version the image. First, make sure your Makefile VERSION is updated and committed to master. Lastly, run $ make gitTag which sets a tag in GitHub and triggers Docker Hub.