🚀 Automating AWS ECS Deployments with GitHub Actions
Here's a comprehensive GitHub Actions workflow that automates the deployment of your application to AWS ECS. This workflow builds a Docker image, pushes it to AWS ECR, and updates the ECS service.
Prerequisites
- ✅ AWS credentials configured in GitHub Secrets (
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION).
- 🐳 Dockerfile in your repository.
- ☁️ AWS ECS cluster and service already set up.
- 📦 AWS ECR repository created.
Workflow YAML File (.github/workflows/deploy.yml)
name: Deploy to AWS ECS
on:
push:
branches: [main]
env:
AWS_REGION: your-aws-region # e.g., us-east-1
ECR_REPOSITORY: your-ecr-repo # e.g., my-app
ECS_SERVICE: your-ecs-service # e.g., my-app-service
ECS_CLUSTER: your-ecs-cluster # e.g., my-ecs-cluster
TASK_DEFINITION: task-definition.json # Task definition file
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.TASK_DEFINITION }}
container-name: your-container-name # e.g., web
image: ${{ steps.build-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
Explanation
- Name: Defines the name of the workflow.
- On: Triggers the workflow on pushes to the
main branch.
- Env: Sets environment variables for AWS region, ECR repository, ECS service, ECS cluster, and Task Definition file.
- Jobs: Defines the deploy job.
- Checkout: Checks out the repository.
- Configure AWS Credentials: Configures AWS credentials using secrets.
- Login to Amazon ECR: Logs in to Amazon ECR.
- Build, tag, and push image to Amazon ECR: Builds the Docker image, tags it with the Git SHA, and pushes it to ECR.
- Fill in the new image ID in the Amazon ECS task definition: Updates the task definition with the new image ID.
- Deploy Amazon ECS task definition: Deploys the updated task definition to the ECS service.
Task Definition (task-definition.json)
{
"family": "your-task-family",
"containerDefinitions": [
{
"name": "your-container-name",
"image": "your-ecr-repo:latest",
"cpu": 256,
"memory": 512,
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
],
"essential": true
}
],
"requiresCompatibilities": [
"FARGATE"
],
"networkMode": "awsvpc",
"memory": "512",
"cpu": "256"
}
Important Notes
- ✏️ Replace placeholder values (
your-aws-region, your-ecr-repo, your-ecs-service, your-ecs-cluster, your-container-name, your-task-family) with your actual values.
- 🔑 Ensure your AWS credentials have the necessary permissions to interact with ECR and ECS.
- ⚠️ Adjust CPU and memory settings in the task definition based on your application's requirements.