Nov 18, 20267 min read

HTTPS and a Custom Domain for ECS Fargate With ALB and ACM

How to serve an ECS Fargate service on api.example.com over HTTPS: an ACM certificate with DNS validation, an Application Load Balancer with HTTPS and redirect listeners, an ip target group, security groups and a Route 53 alias, all in Terraform.

Illustration for HTTPS and a Custom Domain for ECS Fargate With ALB and ACM

An ECS Fargate service has no public address of its own. Tasks get private IPs that change on every deploy, so a stable https://api.example.com needs an Application Load Balancer, an ACM certificate, a target group and a DNS record, plus security groups that keep the tasks reachable only through the load balancer.

This guide builds that setup in Terraform, piece by piece, and then shows how the same result looks when Encore deploys the service to Fargate in your own AWS account.

The pieces at a glance

PieceWhat it doesCommon mistake
ACM certificateTLS certificate for api.example.com, renewed by AWSRequested in us-east-1 while the ALB is in another region
DNS validation recordProves domain ownership to ACMNever created, so the certificate stays pending
Application Load BalancerPublic entry point in public subnetsPlaced in private subnets
HTTPS listener (443)Terminates TLS and forwards to the target groupMissing certificate_arn or an old TLS policy
HTTP listener (80)Redirects to HTTPS with a 301Forwards plain HTTP to the app
Target groupHolds the task IPs and runs health checkstarget_type = "instance" on Fargate
Security groupsALB open on 80 and 443, tasks open only to the ALBTasks open to 0.0.0.0/0
Route 53 aliasPoints the domain at the ALBA CNAME at the zone apex

The certificate region rule is in the ACM docs for Elastic Load Balancing, and the listener settings are in the ALB HTTPS listener docs.

Step 1: Request and validate the certificate

ACM issues and renews public certificates for integrated services such as Elastic Load Balancing, and DNS validation lets Terraform create the proof record in Route 53 and wait until the certificate is issued. create_before_destroy keeps the listener working when the certificate is replaced.

variable "vpc_id" { type = string } variable "public_subnet_ids" { type = list(string) } variable "private_subnet_ids" { type = list(string) } data "aws_route53_zone" "main" { name = "example.com" } resource "aws_acm_certificate" "api" { domain_name = "api.example.com" validation_method = "DNS" lifecycle { create_before_destroy = true } } resource "aws_route53_record" "api_validation" { for_each = { for dvo in aws_acm_certificate.api.domain_validation_options : dvo.domain_name => { name = dvo.resource_record_name record = dvo.resource_record_value type = dvo.resource_record_type } } zone_id = data.aws_route53_zone.main.zone_id name = each.value.name type = each.value.type records = [each.value.record] ttl = 60 allow_overwrite = true } resource "aws_acm_certificate_validation" "api" { certificate_arn = aws_acm_certificate.api.arn validation_record_fqdns = [for r in aws_route53_record.api_validation : r.fqdn] }

If the domain lives at another DNS provider, create the CNAME from domain_validation_options there by hand and keep the aws_acm_certificate_validation resource, which waits for issuance either way.

Step 2: Load balancer, security groups and target group

The ALB sits in public subnets and accepts 80 and 443 from anywhere. The task security group accepts the container port only from the ALB security group, so nothing reaches a task without going through the listener rules. Fargate tasks use the awsvpc network mode, which is why the target group registers IPs.

resource "aws_security_group" "alb" { name = "api-alb" vpc_id = var.vpc_id ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_security_group" "task" { name = "api-task" vpc_id = var.vpc_id ingress { from_port = 8080 to_port = 8080 protocol = "tcp" security_groups = [aws_security_group.alb.id] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_lb" "api" { name = "api" load_balancer_type = "application" subnets = var.public_subnet_ids security_groups = [aws_security_group.alb.id] } resource "aws_lb_target_group" "api" { name = "api" port = 8080 protocol = "HTTP" target_type = "ip" vpc_id = var.vpc_id health_check { path = "/health" matcher = "200" } }

Step 3: Listeners and the DNS record

The HTTPS listener uses the validated certificate ARN, which makes Terraform wait for validation before it creates the listener. The HTTP listener only redirects. An alias A record is the right record type here, since it works at the zone apex too and follows the ALB's changing IPs.

resource "aws_lb_listener" "https" { load_balancer_arn = aws_lb.api.arn port = 443 protocol = "HTTPS" ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" certificate_arn = aws_acm_certificate_validation.api.certificate_arn default_action { type = "forward" target_group_arn = aws_lb_target_group.api.arn } } resource "aws_lb_listener" "http" { load_balancer_arn = aws_lb.api.arn port = 80 protocol = "HTTP" default_action { type = "redirect" redirect { port = "443" protocol = "HTTPS" status_code = "HTTP_301" } } } resource "aws_route53_record" "api" { zone_id = data.aws_route53_zone.main.zone_id name = "api.example.com" type = "A" alias { name = aws_lb.api.dns_name zone_id = aws_lb.api.zone_id evaluate_target_health = true } }

Step 4: Attach the ECS service

The ECS service joins the target group through a load_balancer block, runs in private subnets with the task security group, and gets a health check grace period so slow-starting containers are not killed before they answer /health.

resource "aws_ecs_service" "api" { name = "api" cluster = aws_ecs_cluster.main.id task_definition = aws_ecs_task_definition.api.arn desired_count = 2 launch_type = "FARGATE" health_check_grace_period_seconds = 30 network_configuration { subnets = var.private_subnet_ids security_groups = [aws_security_group.task.id] } load_balancer { target_group_arn = aws_lb_target_group.api.arn container_name = "api" container_port = 8080 } depends_on = [aws_lb_listener.https] }

TLS ends at the load balancer, so the app sees plain HTTP on port 8080. Code that builds absolute URLs or enforces HTTPS should read the X-Forwarded-Proto header that the ALB adds instead of looking at the connection. The ECS Fargate with Terraform guide covers the cluster, task definition and IAM roles that this block refers to.

How does Encore handle HTTPS on Fargate?

Encore

Encore brings infrastructure from code, and on AWS the infrastructure docs describe the result: a VPC across two availability zones, an Application Load Balancer in the public subnet as the entry point, Fargate tasks in a private compute subnet that only accept traffic from the ALB, and target groups with health checks and grace periods for each service. The endpoints come from the Go or TypeScript code, so the routes the load balancer serves are the routes in the application.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

// api/api.go package api import "context" type Health struct { Status string `json:"status"` } //encore:api public method=GET path=/health func Check(ctx context.Context) (*Health, error) { return &Health{Status: "ok"}, nil }

Each environment is reachable on an encr.app subdomain from the first deploy. A custom domain is one CNAME record pointing at custom-domain.encr.app and an entry in Settings > Custom Domains, after which Encore issues the SSL certificate and serves the environment on that domain, as the custom domains docs describe. The certificate, the listener and the redirect are not resources in your repository.

Coding agents work on the endpoints with encore run locally and push the change, and the domain, certificate and load balancer setup stay outside the code they edit.

Best fit: Go or TypeScript backends on AWS that want each service on Fargate behind a load balancer and a custom HTTPS domain per environment from one DNS record.

HTTPS on Fargate checklist

  • Request the ACM certificate in the same region as the ALB and validate it through DNS.
  • Use an ip target group with a health check path that returns 200 quickly.
  • Let the task security group accept traffic only from the ALB security group.
  • Redirect port 80 to 443 instead of forwarding it.
  • Use an alias A record to the ALB, not a CNAME at the apex.
  • Read X-Forwarded-Proto in the app when it needs to know the original scheme.

Frequently asked questions

How do I add HTTPS to an ECS Fargate service?

Put an Application Load Balancer in front of the service, request an ACM certificate for the domain with DNS validation, and attach it to an HTTPS listener on port 443 that forwards to an ip target group. Register the ECS service with that target group and add a listener on port 80 that redirects to HTTPS.

Does the ACM certificate have to be in the same region as the ALB?

Yes. An Application Load Balancer can only use ACM certificates from its own region. The us-east-1 requirement applies to CloudFront, not to an ALB in front of Fargate.

Why does a Fargate target group need target_type ip?

Fargate tasks use the awsvpc network mode, so each task has its own elastic network interface and private IP. The load balancer registers those IPs directly, which requires an ip target group rather than the instance type.

How do I get HTTPS and a custom domain with Encore on AWS?

Encore runs the services on Fargate behind an Application Load Balancer in your AWS account. For your own domain, add a CNAME record pointing at custom-domain.encr.app and add the domain in the app's Custom Domains settings, and Encore issues the SSL certificate.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders