Merge and Done: Safe and Speedy Docker Deploy on Amazon ECS

Docker promises easy container management. Amazon EC2 Container Service (ECS) promises quick deployments. Solano CI takes advantage of both to provide safe automatic deployments whenever you merge a pull request. One action and your cloud is running your latest code.

When a Solano Labs engineer brought up the idea of updating a docker example repository to demonstrate a Solano CI+Amazon ECS integration, I have to admit my first thoughts bordered on dread. I envisioned writing an extensive list of arguably obscure aws commands and explaining every one of them in too much detail.

After re-visiting AWS’s excellent documentation on ECS, my thoughts turned much sunnier. Not only was most of the documentation already done, almost all of it could be done in the AWS console. Updating the CI memes example repository to integrate with Amazon ECS was so simple, I procrastinated by updating the container’s webserver output (the earlier web 0.1 look of it kind of bugged me).

These are the steps I took starting from a fresh AWS account:

  1. Read the Setting Up with Amazon ECS documentation and followed the steps outlined therein.
  2. Instead of using the Amazon ECS sample, I created a custom task definition on the Amazon ECS console first run wizard.
  3. For the Task Definition Name I entered “ci_memes”, and then used the Configure via JSON button to add the JSON, although for the initial task revision I set some static values:
  4. On the Schedule Tasks step I selected “Create a Service”, set the desired number of tasks to 1, and the service name to “ci_memes-service”.
  5. To configure the cluster, I selected 1 instance, “t2.micro” as the instance type, the key pair I created in step 1 above, secured non-HTTP access to my network’s IP address range, and set the ECS instance role to the IAM role also created in step 1.
  6. I updated the solano.yml file in the repo to include non-sensitive environment variables.
  7. I used the solano config:add command to set environment variables that shouldn’t be checked into the repo:
  8. Solano CI was already building the container in the pre_setup hook script , and running the defined tests, so all I had to do was deploy in the post_build hook script. The script is fairly well commented, but the TL;DR version:
    1. Only deploy if the tests pass.
    2. Deploy to docker hub.
    3. Write a new JSON task definition file, replacing the place holders with environment and build defined values.
    4. Determine the existing revision number of the task in Amazon ECS.
    5. Update the Amazon ECS task.

As silly as the CI memes and quotes are, it is nice to know that any time I merge a pull request, Solano CI will automatically build, test, and deploy to both Docker Hub and Amazon ECS. It is so easy, I’m looking forward to integrating Amazon EC2 Container Registry (ECR) once it is released.

Post a Comment