Use when writing or reviewing Terraform that deploys a container service to ECS Fargate: task definitions, secrets injected from Secrets Manager, image tags and ECR lifecycle, deployment rollback and alarms, a service behind a load balancer or in a VPC that a platform team owns, why a task does not start, or running more than one task. Not for a new service from a template (creating-aws-projects) or Lambda (aws-serverless).
97%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Medium
Suggest reviewing before use
This skill covers the application's Terraform root: cluster, task definition, service, ECR repository and log group. It assumes that the VPC, load balancer, target group, IAM roles and secrets may belong to a platform team, and that the application root only references them.
Start with the contract. Ask the platform team for:
Look up each item with a data source, and check it at plan time. Then a wrong ID fails the plan before anything changes, instead of failing a task at runtime.
data "aws_iam_role" "execution" { name = local.execution_role_name } # plan fails if the role is missing
# Exists only for its postcondition, which tflint cannot see.
# tflint-ignore: terraform_unused_declarations
data "aws_subnet" "private" {
for_each = toset(var.private_subnet_ids)
id = each.value
lifecycle {
postcondition {
condition = self.vpc_id == var.vpc_id
error_message = "Subnet ${each.value} is not in ${var.vpc_id}."
}
}
}load_balancer block dynamic, so the service can run before the target group exists.secrets = [for name, key in local.secret_keys : {
name = name
valueFrom = "${data.aws_secretsmanager_secret.app.arn}:${key}::" # <arn>:<json-key>:<version-stage>:<version-id>
}]::. Empty fields mean AWSCURRENT.-??????, the random suffix that Secrets Manager adds to the ARN.secrets. Anyone with ecs:DescribeTaskDefinition can read environment values.aws ecs update-service --force-new-deployment) after every rotation, and put that step in the rotation runbook.Source: ECS secrets from Secrets Manager · verified 2026-11-15
IMMUTABLE repository, and pass the tag in as a variable whose validation rejects latest.versionConsistency, on by default), and every later task in that deployment starts from that digest. A replacement task, a scale-out or a rollback therefore needs that exact image to still exist.imageCountMoreThan N with tagStatus = "any" on a repository that several environments share lets frequent dev pushes, and throwaway builds, delete the image that production runs. Expire untagged images, and expire release images by age or by a tag prefix per environment. Or give each account its own repository, and copy the image to it on promotion.Source: ECS rolling deployments · verified 2026-11-15
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200
deployment_circuit_breaker {
enable = true
rollback = true
}
alarms {
enable = true
rollback = true
alarm_names = [aws_cloudwatch_metric_alarm.target_5xx.alarm_name]
}alarms block fails the deployment and rolls it back when a named alarm goes to ALARM after the deployment.alarms block needs the alarms to exist, which needs cloudwatch:PutMetricAlarm in the deployer's grant.health_check_grace_period_seconds covers slow starts (JVM start, migrations). Set it from a measured start time.wait_for_steady_state = false, the apply succeeds as soon as ECS accepts the new task definition. If the circuit breaker then rolls back, the apply still showed success, and the old revision is running. After the apply, run aws ecs wait services-stable, then check that the service's primary deployment has rolloutState = COMPLETED on the new task definition. With true, a task that stops at once, for example because a secret key is missing, makes the apply wait until its timeout.Sources: Circuit breaker · verified 2026-11-15; Alarm-based rollback · verified 2026-11-15
/ecs/<service> in Terraform with retention_in_days, and leave out awslogs-create-group. Then the execution role needs no logs:CreateLogGroup, and the logs expire.HTTPCode_Target_5XX_Count, with both the TargetGroup and LoadBalancer dimensions. On a shared load balancer, the metric with only the LoadBalancer dimension counts other teams' errors;HealthyHostCount below the desired count for the target group. RunningTaskCount exists only with Container Insights;awslogs driver has defaulted to non-blocking mode with a 10 MB buffer since 2025-06-25. When the buffer is full, log lines are lost. Set mode on purpose.references/observability.md. Most services need no sidecar.aws ecs list-tasks --cluster <cluster> --desired-status STOPPED
aws ecs describe-tasks --cluster <cluster> --tasks <arn> --query "tasks[].[stoppedReason,containers[].reason]"| Stopped reason | Cause |
|---|---|
ResourceInitializationError naming Secrets Manager | A missing key or value, no GetSecretValue on the execution role, or no route to Secrets Manager |
CannotPullContainerError | The tag or digest is not in ECR (check the lifecycle policy), or no route to ECR |
| Running, then deregistered | The target group health check fails. Read the container log for the startup error, and check the health check path and the grace period |
desired_count goes above 1Check each item. Each one breaks quietly when a second task starts:
app_cookie on the application's session cookie, not lb_cookie, whose fixed lifetime can end while the session is still alive.With one task, the service runs in one Availability Zone at a time, and every deployment replaces it.
Report each item as PASS, FAIL or NOT APPLICABLE:
valueFrom with a JSON key, the values are set outside Terraform, and the rotation runbook includes a new deployment.latest, and the lifecycle policy cannot expire an image that any environment runs.alarms block on a user-facing alarm, and a post-apply check that the new revision completed.awslogs mode is set on purpose. Alarms use target-group dimensions and notify someone.references/observability.md (step 5 or 6, or a backend other than CloudWatch), and is non-essential with its own memory and logs. The rollback alarm stays in CloudWatch. Traces of regulated data are redacted.desired_count can be above 1, the four items above are handled.allowed_account_ids, and each environment has its own state key.terraform-aws), not from a laptop.a-pavithraa/aws-serverless-skill
1c28c17
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.