How do I restrict direct traffic to an ALB and allow traffic through only CloudFront using AWS WAF?
I want to restrict users from directly accessing an Application Load Balancer (ALB) and allow access through only Amazon CloudFront using AWS WAF. How do I do this using AWS WAF?
"I want to restrict users from directly accessing an Application Load Balancer (ALB) and allow access through only Amazon CloudFront using AWS WAF. How do I do this using AWS WAF?ResolutionTo restrict direct traffic to an ALB and allow traffic through only CloudFront using AWS WAF, do the following:Configure CloudFront to add a custom HTTP header with a secret value in the requests that CloudFront sends to the ALB.Create a rule in your AWS WAF web ACL associated with the ALB to block requests that don't contain the custom HTTP header secret value.Configure CloudFront to add a custom HTTP header with a secret value in the requests that it sends to the ALBOpen the Amazon CloudFront console.From the navigation pane, choose Distributions.Select the ID of your distribution.Choose Origins.Select your ALB origin, and then choose Edit.Note: If you don't have an ALB origin, see Creating a distribution or Updating a distribution.For Add custom header, add the Header name and Value. An example Header name is Custom-header and Value is Random-12345.Important: Consider the Header name and Value as secure credentials, like a user name and password.Choose Save changes.Create a rule in your AWS WAF web ACL associated with the ALB to block the requests that don't contain the custom HTTP header secret valueNote: If you don't have an existing web ACL associated with your ALB, then follow the steps in Create an AWS WAF web ACL. If you have an existing web ACL associated to your ALB, then proceed to Create a rule.Create an AWS WAF web ACLOpen the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, select the AWS Region where your ALB is located.Choose Create web ACL, and then enter a Name.For Associated AWS resources, associate your ALB, 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.Review the web ACL configuration. If it matches your specifications, choose Create web ACL.Create a ruleOpen the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, select the AWS Region where your ALB is located.Select the web ACL you have associated your ALB to.Choose Rules, then choose Add Rules, Add my own rules and rule groups.For Name, enter a rule name, and then choose Regular Rule.For If a request, choose doesn't match the statement (NOT).On Statement 1:For Inspect, choose Single Header.For Header field name, enter the name of the Header name created in CloudFront. For example, Custom-header.For Match type, choose Exactly matches string.For String to match, enter the Value created in CloudFront. For example, Random-12345.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.Related informationAdding custom headers to origin requestsAWS WAF RulesFollow"