Skip to content

danielecammarata/edge-redirects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lambda Edge Redirect Generator

This project generates an AWS Lambda function that handles URL redirects. The Lambda function redirects requests based on the request path to different predefined URLs.

Prerequisites

  • Bun - Make sure you have Bun installed on your system.

Usage

To generate the AWS Lambda function code, run the Bun program:

bun run exec --file a-valid-json.json

This will generate a lambda_function.js file in your project directory containing the AWS Lambda function code for all the redirects.

The possible parameters are:

--file

expects a valid JSON file containing an array of objects with the keys, from, and to, for example:

[{
  "from": "/old",
  "to": "/new"
}, {
  "from": "/another-old",
  "to": "/another-new"
}]

--from --to

both parameters to generate a single redirect rule. for example:

bun run exec --from /old --to /new 

Generated Lambda Function

The generated lambda_function.js will look like this:

exports.handler = async (event, context, callback) => {
  let request = event.Records[0].cf.request;
  const uri = request.uri;
  console.log(uri);

  switch (true) {
    case uri === "/some-url": {
      const redirectResponse = {
        status: '301',
        statusDescription: 'Moved Permanently',
        headers: {
          'location': [{ key: 'Location', value: "/the-new-url/" }],
          'cache-control': [{ key: 'Cache-Control', value: "max-age=3600" }],
        },
      };
      callback(null, redirectResponse);
      break;
    }
    default: {
      callback(null, request);
    }
  }
};

In this setup, accessing /some-url will redirect to /the-new-url/.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published