Connecting Cloudfront to Lambda Function Url Always Result in forbidden

3

I have setup a lambda function url and cloudfront system

  1. Lambda Function Url is straight forward, a function that will return an image or a json value
  2. Cloudfront using this setting:
    • Origins:
      • Origin Domain: {LAMBDA FUNCTION URL}
      • Protocol: HTTPS only - TLSv1
      • Enable Origin Shield: No
    • Behavior:
      • Viewer: Redirect HTTP to HTTPS
      • Allowed HTTP Method: GET, HEAD
      • Restrict Viewer Access: No
      • Cache Policy: Managed-CachingDisabled
      • Origin request policy: AllViewer

The result however always return 403 Forbidden with this body

{ "Message": null }

And this header

X-cache: Error from cloudfront
x-amzn-ErrorType: AccessDeniedException

Is there any setting that I missed that cause this error? I already test direct hit using postman and browser to the function url an it works fine

ndk-fj
質問済み 2年前3038ビュー
2回答
3
承認された回答

Thanks for the detailed description. You are getting 403 Forbidden due to the origin request policy AllViewer being used. In this case, when you access the website via CloudFront URL, the Host header similar to d12345678.cloudfront.net will be forwarded to the Lambda which will not be recognized, resulting in 403.

The solution is to create your custom origin request policy and only forward the necessary values (but not the Host header.)

AWS
weidi
回答済み 2年前
  • Thank you, it works so well, for my case, I also need to also forward origin and user-agent but it is because of my lambda function code.

-2
profile pictureAWS
エキスパート
回答済み 2年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ