Skip to content

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

Name Version
terraform >= 1.6.0
aws >= 5.0

Providers

Name Version
aws >= 5.0

Modules

No modules.

Resources

Name Type
aws_cloudwatch_log_group.this resource
aws_ecr_lifecycle_policy.this resource
aws_ecr_repository.this resource
aws_ecs_cluster.this resource
aws_ecs_service.this resource
aws_ecs_task_definition.this resource
aws_iam_role.execution resource
aws_iam_role.task resource
aws_iam_role_policy.secrets resource
aws_iam_role_policy_attachment.execution resource
aws_lb.this resource
aws_lb_listener.http resource
aws_lb_listener.https resource
aws_lb_target_group.this resource
aws_security_group.alb resource
aws_security_group.service resource
aws_vpc_security_group_egress_rule.alb_to_service resource
aws_vpc_security_group_egress_rule.service_all resource
aws_vpc_security_group_ingress_rule.alb_http resource
aws_vpc_security_group_ingress_rule.alb_https resource
aws_vpc_security_group_ingress_rule.service_from_alb resource
aws_iam_policy_document.ecs_tasks_assume data source
aws_iam_policy_document.secrets data source
aws_partition.current data source
aws_region.current data source

Inputs

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

Name Description
alb_arn ARN of the Application Load Balancer.
alb_dns_name DNS name of the Application Load Balancer. Point your domain's CNAME/alias record at it.
alb_security_group_id ID of the security group attached to the load balancer.
alb_zone_id Route 53 hosted zone ID of the Application Load Balancer (for alias records).
cluster_arn ARN of the ECS cluster.
cluster_name Name of the ECS cluster.
ecr_repository_arn ARN of the ECR repository.
ecr_repository_url URL of the ECR repository to push the application image to.
execution_role_arn ARN of the task execution role (image pull, logs, secrets).
log_group_name Name of the CloudWatch log group receiving the container logs.
service_name Name of the ECS service.
service_security_group_id ID of the security group attached to the Fargate tasks. Use it to allow access to databases or other resources.
target_group_arn ARN of the ALB target group.
task_definition_arn ARN of the task definition revision currently used by the service.
task_role_arn ARN of the task role assumed by the application. Starts with no permissions.
task_role_name Name of the task role, to attach application policies to.