- Newest
- Most votes
- Most comments
Finally got it working using the VPC Origin path for the CloudFront traffic, with the IPv6 used for outbound without a NAT. Key fixes:
- InstanceId in the VPCOrigin
- PrivateDnsName in the DistributionConfig
- Allow 0.0.0.0/0
- Use correct port (nginx default is 80)
- Use an IPv6 call to Lambda for cfn.signal
Things to check,
Place your EC2 instance in a public subnet with:
- An IPv6 address assigned (either automatically from the VPC CIDR or manually).
- IPv6 connectivity enabled for the subnet.
- Attach a security group that allows inbound traffic from CloudFront’s edge locations over IPv6 on the port your application listens on (e.g., TCP 80 or 443).
Networking
- The subnet must have a route to the internet over IPv6:
- Add a default route (::/0) in the subnet’s route table pointing to an Internet Gateway (IGW)
- Ensure the Internet Gateway has IPv6 enabled.
CloudFront
- CloudFront does not let you directly target an IP address (IPv4 or IPv6). It requires a DNS hostname.
- Assign an AAAA DNS record (for IPv6) pointing to your EC2’s IPv6 address:
The 504 Gateway Timeout error you're experiencing with CloudFront and your IPv6-only EC2 instance is likely related to how CloudFront's VPC origin connects to your EC2 instance. There are several potential causes for this issue:
-
NAT Gateway and Subnet Configuration: When CloudFront attempts to connect to your EC2 instance through its VPC origin, it's likely trying to use IPv4 internally. If your EC2 instance is in a subnet with a NAT gateway attached, this could be blocking inbound connections from CloudFront. NAT gateways allow outbound connections but block inbound connections over the internet by default.
-
CloudFront to Origin Communication: CloudFront has a default timeout of around 60-90 seconds for requests to origins. If your origin doesn't respond within this timeframe, CloudFront will return a 504 Gateway Timeout error.
-
IPv6-only Configuration: While your EC2 instance is configured for IPv6, CloudFront's VPC origin functionality might be attempting to communicate with the instance using its private IPv4 address. Even though you've added an ingress rule for TCP port 80 from the private IPv4 range, there might be network path issues.
-
Security Group Configuration: Although you've added ingress rules for both IPv6 and private IPv4, there might be other network ACLs or routing issues preventing the connection.
To resolve this issue, consider the following solutions:
-
Check Subnet Configuration: Ensure your EC2 instance is in a subnet that has an Internet Gateway attached rather than a NAT Gateway. NAT Gateways block inbound connections, which could be causing the 504 errors.
-
Review Network Path: Verify that there's a valid network path from CloudFront's VPC origin to your EC2 instance. This includes checking route tables, network ACLs, and security groups.
-
Consider Dual-Stack Approach: For this specific use case, you might need to use a dual-stack approach (both IPv6 and public IPv4) temporarily until CloudFront's VPC origin fully supports IPv6-only origins.
-
Test Direct Connectivity: Try to connect directly to the EC2 instance using its IPv6 address to confirm that Nginx is accessible via IPv6.
-
CloudFront Origin Protocol: Ensure that the origin protocol policy in CloudFront is correctly set to match how your Nginx server is configured.
The most likely issue is related to the subnet configuration and how CloudFront's VPC origin is attempting to establish a connection to your EC2 instance. If your EC2 instance is in a subnet with a NAT gateway, try moving it to a subnet with an Internet Gateway instead.
Sources
Cloudfront 504 gateway Timeout | AWS re:Post
Cloudfront timeout to Application Load Balancer | AWS re:Post
HTTP 504 status code (Gateway Timeout) - Amazon CloudFront
DNS64 and NAT64 - Amazon Virtual Private Cloud
- checked, 2. checked, 3. it's already dual-stack, 4. yes the direct connection to IPv6 nginx endpoint from an outside network is successful, 5. yes:
OriginProtocolPolicy: http-only
- checked, 2. checked, 3. it's already dual-stack, 4. yes the direct connection to IPv6 nginx endpoint from an outside network is successful, 5. yes:
Relevant content
- asked 2 years ago
- asked 7 months ago
- AWS OFFICIALUpdated 8 months ago

Made some progress based on your last two points, thank you! After adding an AAAA DNS record on a spare domain I had laying around, the error changed to: ERROR Failed to contact the origin.
I see I had some incorrect port numbers too.