Skip to content

CloudFront-to-VPC adventure continues... How to update EC2 ARN when using ASG?

0

The goal here is to use an Amazon CloudFront distribution to serve content from a web application running on a single-instance EC2 spot instance within a private VPC. This application accepts low availability (i.e. 99% or 15 minutes a day of downtime is fine) so a single-instance is the correct architecture choice. The instance lifecycle is managed by an Auto Scaling Group (ASG). We introduced ASG after realizing that the EC2 Spot Instance API is so brain-dead it's completely unusable without an ASG. So yes, I'm using ASG to manage a single EC2 instance. A key constraint of the project is to accomplish this without using an Application or Network Load Balancer (ALB/NLB) or Public IPv4.

The Challenge:

The primary challenge is integrating the dynamic nature of an ASG with CloudFront's braindead VPC Origin feature which requires a static dependency on an EC2 instance ARN. The AWS::CloudFront::VpcOrigin resource's VpcOriginEndpointConfig.Arn property requires the ARN of an Application Load Balancer (ALB), Network Load Balancer (NLB), or a specific EC2 instance. I've ruled out ALB and NLB due to the 2,000% operating cost increase (e.g. t3.nano instance cost goes from sub-$1 to over $20 when you add an ALB).

So, to be clear, our architecture explicitly avoids ALBs/NLBs, and our EC2 instances are managed by a dynamic Auto Scaling Group (ASG) with service discovery via Cloud Map. This means there is no stable, static ALB, NLB, or individual EC2 instance ARN to reference declaratively in the CloudFormation template. This fundamental mismatch between the VpcOriginEndpointConfig.Arn requirement and our dynamic, ALB-less architecture leads to a logical impossibility in creating a correct CloudFormation template for AWS::CloudFront::VpcOrigin. Cloud Formation is a key requirement.

What's the proper solution here? I can see writing an Custom Resource, but is there a more native approach?

4 Answers
1

Hello.

I recommend creating a custom resource or creating an EventBridge rule that detects when an EC2 instance is launched by AutoScaling and then creating a state machine that rewrites the target of the VPC origin from StepFunctions.
The following document contains examples of AutoScaling events, so why not try creating an EventBridge rule based on this content?
It is possible to create this using Lambda instead of StepFunctions, but in this case, since we are only switching the VPC origin target when starting EC2, I recommend creating a StepFunction that only executes the AWS API.
https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-event-reference.html

EXPERT

answered 10 months ago

  • Cloud Map appears to be a simpler option although I don't quite have that working yet

0

CloudFront’s VPC origin requires a static ARN (ALB, NLB, or EC2 instance). With an Auto Scaling Group, EC2 instance ARNs change, so CloudFront can’t track them automatically.

There’s no native way to make this dynamic — the only practical options are:

Use a Network Load Balancer as a stable endpoint for CloudFront.

Or make the app public and restrict access to CloudFront using OAC or prefix lists.

Without an ALB/NLB, CloudFront can’t reliably reach a private ASG-managed instance.

answered 10 months ago

  • the AI answer was slightly more correct as it seems somewhat possible with Cloud Map

0

To solve your challenge of using CloudFront VPC Origins with an Auto Scaling Group (ASG) managing a single EC2 spot instance without using load balancers, you'll need to address the dynamic nature of EC2 instance ARNs. Here's how you can approach this:

The fundamental issue is that CloudFront VPC Origins requires a stable endpoint (ARN) to connect to within your private subnet, but your EC2 instance is dynamically provisioned by an ASG, resulting in changing ARNs when instances are replaced.

Since you've ruled out ALB/NLB due to cost concerns and need to use CloudFormation, here are your options:

  1. Custom Resource Approach: This is likely your best option. Create a CloudFormation Custom Resource that:
  • Monitors your ASG for instance changes
  • Updates the CloudFront VPC Origin configuration with the new EC2 instance ARN whenever the instance changes
  • Handles the lifecycle events of your CloudFormation stack
  1. Cloud Map with Lambda Integration: Since you mentioned you're already using Cloud Map for service discovery:
  • Set up a Lambda function that triggers when your ASG launches a new instance
  • Have the Lambda update your CloudFront VPC Origin configuration with the new instance ARN
  • This can be integrated into your CloudFormation template as a custom resource
  1. EC2 Instance Connect Endpoint: For SSH access to your instance in the private subnet (for deployments), you can use EC2 Instance Connect Endpoint, which doesn't require public IPs.

Prerequisites to ensure your setup works correctly:

  • Your VPC must have an internet gateway
  • Configure appropriate inbound and outbound rules in your network ACLs
  • Update security groups to allow the CloudFront managed prefix list
  • Ensure you have at least one available IPv4 address in your private subnet
  • Grant access to the AWSServiceRoleForCloudFrontVPCOrigin service-linked role

The Custom Resource approach, while requiring some additional code, provides the most native integration with CloudFormation while addressing the dynamic nature of your EC2 instances managed by ASG.
Sources
CloudFront VPC Origin with ECS-EC2 (non-Fargate) Service | AWS re:Post
Best Practices for Protecting EC2 Behind CloudFront | AWS re:Post
Use CloudFront VPC origins to secure applications | AWS re:Post

answered 10 months ago

EXPERT

reviewed 10 months ago

0

I'm pursuing an option where the EC2 self-registers into Cloud Map by invoking a Lambda over IPv6 to bypass the need for IPv4 VPC Endpoints ($$$). Will update this answer if I get it working.

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.