Sub topic 9
Terminating TLS with ACM certificates
This chapter assumes you can already create an Application Load Balancer (ALB) with a listener and a target group, as covered in the earlier chapters. You should also know what a certificate and a DNS record are, and have a domain whose DNS you can edit. Route 53 is convenient but not required.
What TLS termination means
TLS (Transport Layer Security) encrypts traffic between a client and a server. When a load balancer terminates TLS, it does the following:
- It holds the certificate and private key.
- It completes the handshake with the client.
- It decrypts the request.
- It forwards the request to a target.
Your instances no longer carry certificates, and renewals happen in one place.
AWS Certificate Manager (ACM) is the service that issues and stores those certificates. A public certificate from ACM costs nothing when used with integrated services such as ALB, NLB and CloudFront. ACM renews it automatically, and the load balancer picks up the renewed certificate without a listener change.
Request a certificate
You must request the certificate in the same Region as the load balancer. A certificate in us-east-1 cannot be attached to an ALB in eu-west-1.
aws acm request-certificate \
--domain-name www.example.com \
--subject-alternative-names example.com \
--validation-method DNSThe command prints a CertificateArn. Keep it in a variable, such as $CERT_ARN, for the next steps.
ACM must confirm that you control the domain. With DNS validation, you add a CNAME record that ACM gives you:
aws acm describe-certificate \
--certificate-arn "$CERT_ARN" \
--query "Certificate.DomainValidationOptions[].ResourceRecord"Create the returned name and value as a CNAME record in your DNS zone. Within minutes the certificate status changes from PENDING_VALIDATION to ISSUED.
Email validation also exists, but it needs a person to respond to an email at each renewal, so prefer DNS validation.
Attach the certificate to an HTTPS listener
A listener is where the certificate attaches. Create an HTTPS listener on port 443 that forwards to the target group:
aws elbv2 create-listener \
--load-balancer-arn "$ALB_ARN" \
--protocol HTTPS \
--port 443 \
--certificates CertificateArn="$CERT_ARN" \
--ssl-policy ELBSecurityPolicy-TLS13-1-2-2021-06 \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"Two settings matter here:
--certificatesnames the default certificate, which the ALB presents when no other certificate matches the client's requested host name.--ssl-policyis the security policy. It controls which TLS versions and cipher suites the load balancer accepts. The policy above allows TLS 1.2 and 1.3. Choose the newest policy your oldest clients can support.
The ALB's security group must allow inbound TCP 443 from your clients. Your instances' security group only needs to allow the port the ALB forwards to, normally 80, from the ALB's security group.
Redirect HTTP to HTTPS
Keep the port 80 listener, but change its default action to a redirect so that plain HTTP requests move to HTTPS:
aws elbv2 modify-listener \
--listener-arn "$HTTP_LISTENER_ARN" \
--default-actions 'Type=redirect,RedirectConfig={Protocol=HTTPS,Port=443,StatusCode=HTTP_301}'The ALB answers directly with the 301 response, so redirected requests never reach your targets.
Serve several domains with SNI
One HTTPS listener can hold many certificates. The ALB uses Server Name Indication (SNI), a TLS extension where the client sends the host name it wants during the handshake. The ALB then picks the matching certificate from the listener's certificate list.
aws elbv2 add-listener-certificates \
--listener-arn "$HTTPS_LISTENER_ARN" \
--certificates CertificateArn="$SECOND_CERT_ARN"A wildcard certificate such as *.example.com covers one level of subdomain (api.example.com) but not example.com itself or a.b.example.com. Add the bare domain as a subject alternative name.
What happens behind the load balancer
Once TLS ends at the ALB, you choose how to reach the targets:
| Option | Target group protocol | Trade-off |
|---|---|---|
| Terminate only | HTTP | Simplest. Traffic is unencrypted inside the VPC. |
| Re-encrypt | HTTPS | Traffic stays encrypted. Targets need their own certificate. |
With re-encryption, the ALB does not validate the target's certificate, so a self-signed certificate on the instance works. Also, because the application only sees HTTP in the first option, it cannot tell whether the original request was secure. The ALB adds the X-Forwarded-Proto header to every request. Have your application read it when it builds redirect URLs or sets secure cookies.
NLB differences
A Network Load Balancer can also terminate TLS. Create a listener with protocol TLS, attach an ACM certificate, and set a policy. The NLB then forwards to targets over TCP or TLS. If you use a TCP listener instead, the NLB passes the encrypted bytes straight through, and the targets must hold the certificate.
Common problems
- Certificate not selectable: it is in a different Region or is still
PENDING_VALIDATION. - Browser name mismatch warning: the requested host name is not in the certificate's domain name or subject alternative names.
- Connection times out on 443: the ALB security group lacks an inbound rule for 443.
- Redirect loop: the app redirects to HTTPS because it sees HTTP from the ALB. Make it trust
X-Forwarded-Proto. - Certificate expiry warning from ACM: the validation CNAME was removed or never created.
Exercise
Your ALB in eu-west-1 has an HTTPS listener, but a new ACM certificate does not appear when you try to attach it. DNS validation shows ISSUED. What is the most likely cause, and why should you not delete the validation CNAME afterward?
Show answer
The certificate was most likely requested in a different Region, because ACM certificates are Regional and must be in the same Region as the load balancer. Request the certificate again in eu-west-1. Keep the validation CNAME because ACM re-checks it at every renewal. If it is missing, automatic renewal fails and the certificate eventually expires.