-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please provide an example with a simple cookie manipulation #15
Comments
This could be a good example. Can you provide more details about this example? Would you like to see an example of adding/modifying a cookie on in an incoming request, which would eventually be passed to the origin? This would be a viewer request CloudFront Function setting a |
@dbrown-git I have a use case where we're still using the legacy cache settings in CloudFront and adding It would be a huge boost to our cache hit ratio if we could remove CloudFront-Viewer-Country from the cache key and, instead, inject it into the cookie response to the client. I could then modify the Javascript to pull it from there, and we could return to caching the same version of each page for every country. I just noticed CloudFront Functions yesterday in the AWS console, so I've been starting to experiment with it more to accomplish the use case above. It sounds like my use case would require a viewer response function. Will |
Not with the legacy cache settings, no. This is one of the key benefits of using cache policies and origin request policies instead of the legacy cache settings. With the legacy cache settings, the only way to add a |
Thanks for the reply, @joshbean ! I have actually been experimenting today with changing over to an Origin Request and Cache Policy on my test CloudFront distribution. I have a I have also created a CloudFront function that just attempts to copy In case it helps, here is the CloudFront function I am testing, and I'm attaching it as a
|
@jeff-crosby Thanks for all of those details! I have a couple of thoughts to offer. First, I notice that you have a couple of headers (Accept and Host) that are specified in both the cache policy and the origin request policy. There's no harm in doing that, but for simplicity's sake it's not technically necessary. Anything that's specified in a cache policy is automatically included in the origin request, so there's no need to specify these headers again in the origin request policy. The origin request policy is only for values that you want to get at the origin (or in your case, in a CloudFront function) but that you don't want to include in the cache key. Second, it's unlikely (virtually impossible) that you'll ever get a cache entry that's shared across countries, particularly geographically distant countries like the US and Ireland. CloudFront's caching infrastructure includes hundreds of edge locations and more than a dozen regional edge caches around the world. When a user in the US requests an object through CloudFront, they're routed to the edge location that provides the lowest latency, typically one that's geographically close to the user. If the object isn't cached at the edge location, CloudFront checks a regional edge cache, before finally going to the origin. A subsequent user in a similar geographic location in the US is very likely to get a cache hit. However, a user in Ireland will get an entirely different routing path through an edge location that's geographically close to Ireland, then a regional edge cache somewhere in Europe, before finally going to the origin. The user in Ireland will almost never route through the same cache hosts as the user in the US. If you want a single cache layer that all users globally are routed through, take a look at CloudFront Origin Shield. With Origin Shield, sending an initial request from the US and a subsequent request from Ireland should result in a cache hit for the user in Ireland. Origin Shield incurs an additional cost per 10,000 requests on top of the standard CloudFront per-request charges, so be aware of that if you decide to look into this feature. |
I'm trying to use Cloudfront functions to create a A/B testing setup with cookies that allow users to be served one variant consistently. Right now I'm stuck on how to set a cookie for a first-time user, and have them redirect to the function again after a cookie is set. Currently I'm working from the Viewer-request events: If I try to set the cookie attribute in the request or response object, I get this error when visiting the distribution: |
For anyone else who ends up here, this worked for me: async function handler (event) {
const response = event.response
response.cookies = {
'X-My-Cookie': {
value: 'Test',
attributes: 'Secure; HttpOnly; Path=/'
}
};
return response;
} |
@jsakas Trying this did not set a Cookie for me – are you using this in a viewer request Lambda@Edge function? |
I was not able to find any example for modifing or adding a cookie.
We need this feature to set the Client Language via Cookie.
The text was updated successfully, but these errors were encountered: