スキップしてコンテンツを表示

Changing Host in Lambda@Edge for an Origin-Request is forbidden

0

I have a very simple approach for a PoC:

export const handler = async (event, context, callback) => {
    const request = event.Records[0].cf.request;
    console.log(JSON.stringify(request))
    // change Origin --> forbidden
    request.headers.host[0].value = 'picsum.photos';
    
    // uri change only works
    request.uri = '/200/300';
    // query change works too
    request.querystring = 'test=querystring';
    
    callback(null, request);
};

I need to change the host to a dynamic target. Like A/B-Testing.

The documentation Lambda@Edge - Restrictions does not state, that the host is a read-only for Origin-Requests. But it always results in a {"message":"Forbidden"} as soon as I touch the host.

How can I solve this?

Changing only the URI and/or Query works fine and results in the correct origin request.

1回答
0
承認された回答

Based on your code sample, it looks like you're just trying to update the host header - your origin remains unchanged - is this correct?

Normally in this scenario, customers want to send traffic to a different origin, as well as updating the host header.

I suspect the problem might be down the way you are trying to update the header - there is an example here which shows how you can update the origin as well as the host header. I'd recommend using the syntax from there.

AWS
エキスパート
回答済み 2年前
エキスパート
レビュー済み 2年前
  • Awesome. Thank you for this hint! The solution was to also edit the origin object.

             /* Set custom origin fields*/
             request.origin = {
                 custom: {
                     domainName: 'www.example.com',
                     port: 443,
                     protocol: 'https',
                     path: '',
                     sslProtocols: ['TLSv1', 'TLSv1.1'],
                     readTimeout: 5,
                     keepaliveTimeout: 5,
                     customHeaders: {}
                 }
             };
    

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

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

関連するコンテンツ