Skip to content
Latchkey

How to Deploy to Amazon ECS With CircleCI

Register a new task definition revision with the built image, then run aws ecs update-service --force-new-deployment to roll it out.

Update the task definition to point at the new image tag, register the revision, and trigger a service update so ECS replaces the running tasks.

Steps

  • Render a task definition JSON with the new image tag.
  • Register it with aws ecs register-task-definition.
  • Call aws ecs update-service referencing the new revision.
  • Optionally wait with aws ecs wait services-stable.

Config

.circleci/config.yml
version: 2.1
jobs:
  deploy:
    docker:
      - image: cimg/aws:2024.03
    steps:
      - checkout
      - run:
          name: Deploy to ECS
          command: |
            sed "s|IMAGE_TAG|$CIRCLE_SHA1|" taskdef.json > out.json
            REV=$(aws ecs register-task-definition \
              --cli-input-json file://out.json \
              --query 'taskDefinition.taskDefinitionArn' --output text)
            aws ecs update-service \
              --cluster "$ECS_CLUSTER" --service api \
              --task-definition "$REV" --force-new-deployment
            aws ecs wait services-stable --cluster "$ECS_CLUSTER" --services api
workflows:
  ship:
    jobs:
      - deploy:
          context: aws-prod

Gotchas

  • services-stable can wait up to ten minutes; raise the job no_output_timeout if the rollout is slow.
  • Each register-task-definition call creates a new revision, so prune old revisions periodically.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →