A Node.js application containerized with Docker and deployed to AWS ECS via GitHub Actions.
-
AWS Account with:
- ECR repository created
- ECS cluster and service configured
- Task definition created
- IAM roles with proper permissions
-
GitHub Secrets configured:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
aws ecs create-repository --repository-name ecr-nodejs-app --region us-east-1aws ecs create-cluster --cluster-name ecr-cluster --region us-east-1aws ecs register-task-definition --cli-input-json file://task-definition.json --region us-east-1Note: Update the task-definition.json file with:
- Your AWS account ID
- IAM role ARNs
- ECR repository URI
aws ecs create-service \
--cluster ecr-cluster \
--service-name ecr-nodejs-service \
--task-definition ecr-nodejs-task \
--desired-count 1 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-xxxxx],securityGroups=[sg-xxxxx],assignPublicIp=ENABLED}" \
--region us-east-1- ECS Task Execution Role: Allows ECS to pull images from ECR and write logs
- ECS Task Role: Permissions for the running container (if needed)
Example policy for Task Execution Role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}- Go to your repository Settings → Secrets and variables → Actions
- Add the following secrets:
AWS_ACCESS_KEY_ID: Your AWS access keyAWS_SECRET_ACCESS_KEY: Your AWS secret key
Update the following in .github/workflows/deploy.yml:
AWS_REGION: Your AWS regionECR_REPOSITORY: Your ECR repository nameECS_SERVICE: Your ECS service nameECS_CLUSTER: Your ECS cluster nameECS_TASK_DEFINITION: Your task definition family name
The pipeline will automatically trigger on:
- Push to
mainormasterbranch - Manual trigger via GitHub Actions UI
The workflow will:
- Build the Docker image
- Push to ECR
- Update the ECS task definition with the new image
- Deploy to ECS service
docker build -t ecr-nodejs-app .docker run -p 3000:3000 ecr-nodejs-appVisit http://localhost:3000 to see the application.