
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/cdkRun <code>npm install</code>, then <code>cdk deploy</code> in the pattern directory (bootstrap the account and Region first if needed).The stack creates the versioned code bucket, uploads the application, and defines the Web Function, its revision and <code>IamAuth</code> endpoint, the OAC, a cache policy that forwards the viewer's Authorization header, the CloudFront distribution and the resource policy.cdk destroy. The stack disables and deletes the distribution and the cache policy, empties and deletes the code bucket and removes the log group.