How to Deploy a Container to AWS ECS from GitHub Actions
An ECS deploy is build, push to ECR, render a task definition, then update the service.
Authenticate to AWS via OIDC, push the image to ECR, inject the new image into the task definition, and deploy it to the service.
Steps
- Configure AWS credentials via
aws-actions/configure-aws-credentialsand OIDC. - Log in to ECR, build and push the tagged image.
- Render the task definition with the new image and deploy to the ECS service.
Workflow
.github/workflows/deploy.yml
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: us-east-1
- id: ecr
uses: aws-actions/amazon-ecr-login@v2
- run: |
docker build -t ${{ steps.ecr.outputs.registry }}/app:${{ github.sha }} .
docker push ${{ steps.ecr.outputs.registry }}/app:${{ github.sha }}
- id: task
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-def.json
container-name: app
image: ${{ steps.ecr.outputs.registry }}/app:${{ github.sha }}
- uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.task.outputs.task-definition }}
service: app-service
cluster: prod
wait-for-service-stability: trueGotchas
- Tag images with the commit SHA so rollbacks point at a real image.
- Latchkey runs these deploys on cheaper managed runners that self-heal transient ECR or AWS errors.
Related guides
How to Authenticate to AWS With OIDC in GitHub ActionsGet short-lived AWS credentials in GitHub Actions via OIDC and aws-actions/configure-aws-credentials, removin…
How to Publish a Docker Image to Docker Hub in GitHub ActionsBuild and push a Docker image to Docker Hub from GitHub Actions with docker/login-action and build-push-actio…