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: productionGotchas
force-new-deploymentreuses the current task definition; register a new revision first if the image tag is pinned in it.wait services-stablepolls for up to 10 minutes, then errors; size your health checks accordingly.
Related guides
How to Deploy to AWS via OIDC With GitLab CI/CDDeploy to AWS from GitLab CI/CD without long-lived keys by minting an OIDC id_token and exchanging it for sho…
How to Deploy to Google Cloud Run With GitLab CI/CDDeploy a container to Google Cloud Run from GitLab CI/CD with gcloud run deploy, pointing the service at your…