eolas/zk/Restructure_URLs.md

41 lines
1.3 KiB
Markdown
Raw Normal View History

2023-04-08 18:47:34 +01:00
---
tags: [AWS, aws-lambda, backend]
---
# AWS Lambda examples
## Reconstruct CloudFront URLs
```js
exports.handler = (event, context, callback) => {
// Extract the request from the CloudFront event that is sent to Lambda@Edge
var request = event.Records[0].cf.request;
// Extract the URI from the request
var olduri = request.uri;
// Match any '/' that occurs at the end of a URI. Replace it with a default index
var newuri = olduri.replace(/\/$/, "/index.html");
// Replace the received URI with the URI that includes the index page
request.uri = newuri;
// Return to CloudFront
return callback(null, request);
};
```
2023-04-10 19:36:55 +01:00
Let's walk through the above example making reference to the general structure
of the
2024-02-17 11:57:44 +00:00
[AWS Lambda handler function](Lambda_handler_function.md):
1. The `event` parameter is invoked to gain access to the event that triggered
the function. This is a CloudFront request event which is targetted with
`event.Records[0].cf.request`
2. We classify this request as the `olduri` since it doesn't have the form we
want.
2023-04-10 19:36:55 +01:00
3. We mutate the original request by replacing `/` with `/index.html`
4. We then re-assign the `request.uri` value to the value of the new URI.
5. Finally we invoke the `callback` parameter to signal the completion of the
transformation and return the new request as the return value.