./redirects.json
contains an array of redirects like so:
[
{
"source": "/path/to/old/resource",
"to": "/path/to/new/resource"
}
]
Where source
is a string that'll be parsed as a RegExp object. Add in querystrings if you like, they'll be matched.
See the event.json
file for an edited/pared down real request. Edit event.json
to see if your requests and redirects work as they should together.
Run npm test
to test
A positive match/redirect will output an object like this:
{
"status": "301",
"statusDescription": "Moved Permanently",
"headers": {
"content-type": [{
"key": "Content-Type",
"value": "text/plain; charset=UTF-8"
}],
"location": [{
"key": "Location",
"value": "The place you're redirecting to"
}]
}
}
A negative match/no redirect will output the original response or request object
Make sure you're watching your dependencies closely, keep devDependencies in devDependencies as the Lambda@Egde functions have even tighter restrictions on weight and execution time. For more information see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html#limits-lambda-at-edge
When you're done testing: 0. Modify your existing Lambda user or create a new one that has sufficient privileges - do it now cause it's a big pain to do it later
- Run
npm run package
- Create a new Lambda function
- Upload the zip in
./build
- Make sure your settings are as specified in the Lambda@Edge limits, eg: the function timeout is max 5 seconds
- Add some tests if you like (eg: copy the
event.json
variations you were using) - Publish and name a version - this is what you'll be linking to CloudFront
- Copy the full function version ARN, it should have a
:n
at the end, where then
is the published version number, eg:arn:aws:lambda:us-east-1:123456789012:function:my-awesome-redirect-function:1
- In the CloudFront distribution, open a behaviour and at the bottom under Lambda Function Associations set Event Type to Origin Request and paste the full function version ARN from the previous point into the Lambda Function ARN box
- Hit Yes, edit and wait for changes to take over
- Test the paths you wanted redirecting, they should now initially respond with a 301