跳至內容

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.

已提問 2 年前檢視次數 1448 次
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: {}
                 }
             };
    

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。