handsome
handsome

Reputation: 2412

pass query params from cloudfront to api gateway

I created a lambda function with a API gateway and Cloudfront distribution in the front

in the cloudfront behaviors I disabled caching

enter image description here

this is the lambda function:

exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify('rawQueryString is: ' + event.rawQueryString),
    };
    return response;
};

calling the api gateway I see the querystring in the lambda response https://xxx.execute-api.us-east-1.amazonaws.com/api?name=john

rawQueryString is: '?name=john'

calling the cloudfront distribution i can't see the querystring in the lambda response https://xxx.cloudfront.net/api?name=john

rawQueryString is: ''

I tried with "Origin Request Policy"

enter image description here

but now when i call https://xxx.cloudfront.net/api?name=john

I get

{
    "message": "Forbidden"
}

Upvotes: 3

Views: 4087

Answers (3)

dead_webdev
dead_webdev

Reputation: 256

https://kuchbhilearning.blogspot.com/2022/10/pass-query-params-from-cloudfront-to.html

We need to enable the cloudfront origin, this can also be done through CDK through addBehaviour or default ones.

Upvotes: 0

acidjazz
acidjazz

Reputation: 1314

This has been answered properly here

You cannot send Host - so make an Origin Policy passing as much as you want - but do not pass Host ! That's what produces the Forbidden.

Upvotes: 0

Marcin
Marcin

Reputation: 238299

You should setup origin request policies for your cache behavior. You can try with AWS managed Managed-AllViewer policy or create new one just to forward the query strings:

enter image description here

Upvotes: 7

Related Questions