ecs-fargate-service
ECS Fargate service behind an Application Load Balancer: ECR repository (scan on push, lifecycle policy),
cluster, task definition, service with circuit breaker, ALB with HTTP listener (HTTPS + HTTP redirect when
certificate_arn is set), security groups, CloudWatch logs and least-privilege IAM roles. Environment
variables go in environment; secrets are Secrets Manager / SSM ARNs in secrets.
Usage
module "service" {
source = "github.com/rafaelcarvajal05/terraform-stacks//modules/ecs-fargate-service?ref=v0.1.0"
name = "my-app"
vpc_id = "vpc-0123456789abcdef0"
private_subnet_ids = ["subnet-aaaa", "subnet-bbbb"] # tasks run here
public_subnet_ids = ["subnet-cccc", "subnet-dddd"] # ALB lives here
container_image = "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest"
container_port = 8080
desired_count = 2
environment = {
NODE_ENV = "production"
}
secrets = {
DATABASE_URL = "arn:aws:secretsmanager:us-east-1:123456789012:secret:db-url"
}
tags = {
Environment = "prod"
}
}
See examples/ecs-fargate-basic for a runnable configuration (note: it
creates NAT gateways, which are billed per hour).
Requirements
Providers
Modules
No modules.
Resources
| Name |
Description |
Type |
Default |
Required |
| container_image |
Container image to run, for example the ECR repository URL (output ecr_repository_url) plus a tag, or a public image for the first deploy. |
string |
n/a |
yes |
| container_port |
Port the container listens on. The ALB forwards traffic to it. |
number |
n/a |
yes |
| name |
Name of the service. Used as prefix for the ECR repository, cluster, ALB, IAM roles and log group. |
string |
n/a |
yes |
| private_subnet_ids |
Private subnet IDs (at least two AZs) where the Fargate tasks run. The tasks get no public IP, so these subnets need a NAT gateway or VPC endpoints to pull the image and reach AWS APIs. |
list(string) |
n/a |
yes |
| public_subnet_ids |
Subnet IDs (at least two AZs) for the ALB. Use public subnets for an internet-facing ALB or private ones when internal is true. |
list(string) |
n/a |
yes |
| vpc_id |
ID of the VPC where the ALB, the target group and the service run. |
string |
n/a |
yes |
| alb_ingress_cidr_blocks |
CIDR blocks allowed to reach the ALB listeners. |
list(string) |
[ "0.0.0.0/0" ] |
no |
| certificate_arn |
ARN of an ACM certificate. When set, an HTTPS listener (443) is created and HTTP (80) redirects to it; when null only an HTTP listener is created. |
string |
null |
no |
| container_name |
Name of the container in the task definition. Defaults to name. |
string |
null |
no |
| cpu |
Task CPU units (256, 512, 1024, 2048, 4096, 8192 or 16384). |
number |
256 |
no |
| desired_count |
Number of tasks to run. |
number |
2 |
no |
| ecr_force_delete |
Whether the ECR repository can be deleted even if it still contains images. |
bool |
false |
no |
| ecr_image_tag_mutability |
Tag mutability of the ECR repository (MUTABLE or IMMUTABLE). |
string |
"IMMUTABLE" |
no |
| ecr_max_image_count |
Number of most recent images kept by the ECR lifecycle policy. |
number |
30 |
no |
| enable_deletion_protection |
Whether to enable deletion protection on the ALB. |
bool |
false |
no |
| environment |
Plain-text environment variables for the container (name => value). Do not put secrets here; use secrets. |
map(string) |
{} |
no |
| health_check_path |
HTTP path the ALB uses to check target health. |
string |
"/" |
no |
| internal |
Whether the ALB is internal (not internet-facing). |
bool |
false |
no |
| log_retention_days |
Retention in days of the container CloudWatch log group. |
number |
30 |
no |
| memory |
Task memory in MiB. Must be a valid Fargate combination for cpu. |
number |
512 |
no |
| secrets |
Secrets injected into the container as environment variables (variable name => ARN of a Secrets Manager secret or SSM Parameter Store parameter). The task execution role is granted read access to exactly these ARNs. |
map(string) |
{} |
no |
| ssl_policy |
TLS security policy of the HTTPS listener. Only used when certificate_arn is set. |
string |
"ELBSecurityPolicy-TLS13-1-2-2021-06" |
no |
| tags |
Tags applied to all resources. A Name tag is added per resource. |
map(string) |
{} |
no |
Outputs