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-servicereferencing 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-prodGotchas
services-stablecan wait up to ten minutes; raise the jobno_output_timeoutif the rollout is slow.- Each register-task-definition call creates a new revision, so prune old revisions periodically.
Related guides
How to Build and Push a Docker Image With setup_remote_docker in CircleCIBuild a Docker image inside a CircleCI Docker executor using setup_remote_docker, then log in and push the ta…
How to Deploy to EKS With the aws-eks Orb in CircleCIDeploy a Kubernetes manifest to Amazon EKS from CircleCI with the aws-eks orb, which configures kubectl and a…