How do I restrict direct traffic to an Application Load Balancer and allow traffic through only CloudFront?
I want to restrict direct access to an Application Load Balancer and allow access through only Amazon CloudFront.
Short description
To restrict direct traffic to an Application Load Balancer and allow access only through CloudFront, use one or both of the following solutions:
Note: It's a best practice to combine the two following solutions.
Resolution
AWS WAF
To use AWS WAF to restrict direct traffic to an Application Load Balancer and allow traffic through only CloudFront, do the following:
- Configure CloudFront to add a custom HTTP header with a secret value in the requests that CloudFront sends to the Application Load Balancer.
- Create a rule in the AWS WAF web access control list (web ACL) associated with the Application Load Balancer. Use this rule to block requests that don't contain the custom HTTP header secret value.
Configure CloudFront to add a custom HTTP header
Complete the following:
- Open the Amazon CloudFront console.
- From the navigation pane, choose Distributions.
- Select your distribution ID.
- Choose Origins.
- Select your Application Load Balancer, and then choose Edit.
Note: If your Application Load Balancer isn't an origin, then update your distribution and set the Application Load Balancer as an origin.
- For Add custom header, enter the Header name and Value.
Important: The Header name and Value act as secure credentials, such as a username and password. Copy these values to a text file to use later in this procedure.
- Choose Save changes.
Create a rule in your web ACL to block requests without the header
Complete the following:
- Open the AWS WAF console.
- In the navigation pane, under AWS WAF, choose Web ACLs.
- For Region, select the AWS Region where your Application Load Balancer is located.
Note: If you already have an existing web ACL associated to your Application Load Balancer, then skip ahead to step 9.
- Choose Create web ACL, and then enter a Name.
- For Associated AWS resources, select your Application Load Balancer, and then choose Next.
- For Default web ACL action for requests that don't match any rules, choose Allow, and then choose Next.
- For Request sampling options, choose Enable sampled requests, and then choose Next.
- Choose Create web ACL.
- Open the AWS WAF console.
- In the navigation pane, under AWS WAF, choose Web ACLs.
- For Region, select the Region where your Application Load Balancer is located.
- Select the web ACL associated to your Application Load Balancer.
- Under Rules, choose Add Rules, and then choose Add my own rules and rule groups.
- For Name, enter your rule name.
- For Type, choose Regular Rule.
- For If a request, choose doesn't match the statement (NOT).
- Configure the following settings under Statement 1:
For Inspect, choose Single Header.
For Header field name, enter the Header name that you created in CloudFront.
For Match type, choose Exactly matches string.
For String to match, enter the Value that you created in CloudFront.
For Text transformation, choose None.
- For Action, choose Block.
- Choose Save rule.
- For Set rule priority, if you have multiple rules, then set this rule to the highest priority.
- Choose Save.
Security groups
You can use security groups to restrict direct traffic to an Application Load Balancer and allow traffic through only CloudFront. To do this, use an AWS managed prefix list on security groups in the Application Load Balancer.
To update an existing security group, follow the steps in Update the associated security groups. To associate your Application Load Balancer with a security group, complete the following:
- Open the Amazon EC2 console.
- Select Load balancers, and then select the Application Load Balancer that you want to restrict direct access to.
- Choose Security.
- Select the security group that you want to associate with your Application Load Balancer.
- To modify the inbound rules, select Edit inbound rules, and then update the configurations to your use case.
Note: If you have a rule that allows 0.0.0.0/0, then you must add a new rule before you delete the existing rule.
- To allow specific protocols, select the protocol and then choose Custom.
- For Source type, choose CloudFront, and then select your prefixes from the AWS managed prefix list.
- Choose Save.
To configure more restrictive settings, it's a best practice to allow only ports used by your Application Load Balancer.
Note: The CloudFront managed prefix list can only be added once per security group under the default settings due to the prefix list weight. To add another rule with CloudFront as the Source type in the same security group, request a quota increase. Or, use two security groups that both reference the CloudFront managed prefix list.