A load balanced and auto-scaled api running on AWS ECS.
Init
workflow - manual trigger
[obtain current task arn]
.[if service doesn't exist]
Push a new initial image to ecr.[if service doesn't exist]
Create a new task definition is created./host
endpoint.Deploy
workflow - push on main
trigger
src/*
detected.deploy
boolean based on a new task definition (difference to current) detected.[if deploy=true]
Codedeploy deployment is created and status is monitored.Destroy
workflow - manual trigger
url
from terraform - found in github action init / setup / network outputs
curl [url]/dev/host
{
"message":"Request handled by backend at 2024-09-25T12:28:17.593Z",
"imageUri":"700011111111.dkr.ecr.eu-west-2.amazonaws.com/fargate-auto-scaled-backend@sha256:78dfc01946306dd6afea2b47b56e196788501bfa93c1b2ee1e90a54e72b56938",
"hostname":"ip-10-55-161-195.eu-west-2.compute.internal"
}
ECS will auto-scale when CPU reaching upper and lower limits. CPU is for entire ECS service.
Initially, the scale-down-alarm
cloudwatch alarm state will be In Alarm
as CPU will be < scale down threshold. This is expected.
Simulate a load on the ECS service with curl [URL]/dev/stress-cpu/75/120
. This example will run 75% CPU load for 120 seconds.
After that load has completed and the =< 1 minute cool off period. This will trigger a cloudwatch alarm which will in turn trigger the auto-scaling rule(s).
Once that load has finished - after the 120 seconds - the scale down alarm will be triggered and the tasks scaled back down.
In tf/service
the below variables are to be considered.
cpu_scale_up_threshold
: percentage CPU load to trigger a scale up of tasks.
cpu_scale_down_threshold
: percentage CPU load to trigger a scale down of tasks.
max_scaled_task_count
: maximum amount of tasks to be allowed.
docker build -t express-app .
docker run -i -e BASE_PATH=dev -p 3000:3000 express-app
Required deployment iam privileges.
[
"dynamodb:*",
"s3:*",
"ecr:*",
"iam:*",
"ecs:*",
"ec2:*",
"elasticloadbalancing:*",
"application-autoscaling:*",
"logs:*",
"cloudwatch:*",
"apigateway:*",
"codedeploy:*"
]
Required github action variables.
AWS_ACCOUNT_ID
AWS_REGION
AWS_ROLE
role with above deployment privileges