Skip to content
Latchkey

How to Deploy to Amazon ECS With GitLab CI/CD

aws ecs update-service --force-new-deployment pulls the latest image tag and ecs wait services-stable blocks until rollout is healthy.

Push the image, then call aws ecs update-service --force-new-deployment so ECS launches new tasks, and wait with aws ecs wait services-stable.

Steps

  • Build and push the image to ECR (or your registry).
  • Run aws ecs update-service --force-new-deployment.
  • Block with aws ecs wait services-stable.
  • Run the deploy only on the default branch.

.gitlab-ci.yml

.gitlab-ci.yml
deploy:
  stage: deploy
  image: amazon/aws-cli:latest
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
  script:
    - aws ecs update-service --cluster prod --service myapp --force-new-deployment
    - aws ecs wait services-stable --cluster prod --services myapp
  environment:
    name: production

Gotchas

  • force-new-deployment reuses the current task definition; register a new revision first if the image tag is pinned in it.
  • wait services-stable polls for up to 10 minutes, then errors; size your health checks accordingly.

Related guides

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