I am doing something probably unusual and ill-advised to overcome a limitation with request and response behavior.
I am having an Origin Request Lambda call back to the initial URL via https.get
, with a parameter passed in the header. This will cause a secondary behavior for the same URL request, allowing me to mutate the response in the original Origin Request Lambda before returning a custom response.
The long version:
Function 1 of Viewer Request Lambda fires when there is not the custom property
my-uuid
in the header. It will create the UUID, set that UUID in themy-uuid
property on the header, and then fire the callback with the updated header.Function 1 of the Origin Request Lambda fires where
my-uuid
header is present. Cloudfront is configured to cache based on this header alone, so that the generated UUID will always trigger Function 1 of the Origin Request Lambda. Function 1 makes anhttps.get
call to the URL called in the original request, but passed along themy-uuid
header.Function 2 of the Viewer Request Lambda fires based on the presence of the
my-uuid
header in this second run. This simply strips themy-uuid
header and fires the callback sansmy-uuid
header property.This page has been called before and is in the Cloudfront cache. As the request does not have the
my-uuid
header property, there is no cache-busting, and the cached page is returned to Function 1 of the Origin Request Lambda. OR:This page has not yet been cached, so Function 2 of the Origin Request Lambda is invoked. In the absence of the
my-uuid
header property, it simply fires the callback with the request as-is.
Either way, Function 1 of the Origin Request Lambda receives the HTML from the
https.get
call, and uses this to create a custom response object with the body of the desired page but also the set-cookie header containing the UUID I generated in the initial Viewer Request Lambda. This custom response object is passed into the callback.
While on that path, the solution I crafted brought me to another issue:
Steps 3 and 4.2 (the Function 2 of either Request Lambda) are not logging at all when I call my endpoint via Postman. I have a plethora of console logs to track what's happening internally. However, the response has any headers I try to set in the final response (except, annoyingly, the set-cookie
header which appears to simply disappear and is why I need the logging to work).
If I set the my-uuid
header on my Postman request to trigger the Function 2 behavior, I do see those in the log.