
Serve a Lambda Web Function only through a CloudFront distribution: an IamAuth endpoint, an OAC that signs origin requests, and a resource policy that trusts one distribution.
IamAuth authentication type, so Lambda accepts only requests signed with AWS Signature Version 4 and returns 403 to everything else before your code runs.lambda with SigningBehavior: always-amz-auth. A resource policy on the function allows lambda:InvokeWebFunctionEndpoint for the CloudFront service principal only when AWS:SourceArn matches your distribution, so a different distribution using the same origin is also denied.Cache-Control headers that CloudFront honours, which the pattern uses to show edge caching.This pattern is available in the following frameworks/languages:
git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-web-cloudfront-oac/nodejs/awscliSet your Region, <code>export AWS_REGION=us-east-1</code>, then run <code>./deploy.sh</code>.The script deploys the Web Function with <code>aws lambda-web deploy --auth-type IamAuth</code>, creates the OAC, a cache policy that forwards the viewer's Authorization header, and the distribution, attaches the resource policy and waits for the distribution to deploy.aws lambda-web delete-web-function --function-name lambda-web-cloudfront-oac.