eolas/DevOps/AWS/AWS_Lambda/Lambda_examples.md

29 lines
670 B
Markdown
Raw Normal View History

2023-04-08 18:47:34 +01:00
---
categories:
- DevOps
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);
};
```