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.

Sven
asked 7 months ago486 views
1 Answer
0
Accepted Answer

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
EXPERT
Paul_L
answered 7 months ago
profile picture
EXPERT
reviewed 7 months ago
  • 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: {}
                 }
             };
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions