Coder Social home page Coder Social logo

sinovi / lambda-edge-remove-trailing-slash Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 12.0 67 KB

A Lambda@Edge function to remove the trailing slash from requests

Home Page: https://sinovi.uk/articles/static-website-url-optimisation-with-aws-serverless

License: MIT License

JavaScript 100.00%

lambda-edge-remove-trailing-slash's Introduction

What's new

  • version 1.2.1: Updated readme.
  • version 1.2.0: Updated Node runtime to 16.x
  • version 1.3.0: Updated Node runtime to 20.x

LambdaEdgeRemoveTrailingSlash

A Lambda@Edge function to remove trailing slashes from requests received by your CloudFront distribution.

Designed to assist hosting static websites via S3, ensuring requests to /some/object-identifer/ are resolved to /some/object-identifier correctly

Available on the AWS Serverless Application Repository

How to find this function in the AWS Serverless Repository

  1. Visit AWS Lambda Console
  2. Choose 'Create Function'
  3. Choose 'Browse serverless app repository'
  4. Search for 'LambdaEdgeRemoveTrailingSlash'
  5. Tick the checkbox to 'Show apps that create custom IAM roles or resource policies'
  6. This application will appear in the search results
  7. Click the application box to view the application

Installation Steps (AWS Console)

Installation of this function comprises two stages; Installing the application via the Serverless Repository and CloudFront, then configuring and deploying the Lambda@Edge function.

Building the CloudFormation Stack and Lambda Application

  1. Ensure your console is currently viewing US-East-1 (N. Virginia) - All Lambda@Edge functions must be deployed in this region, see lambda at edge how it works
  2. Visit this application on the AWS Serverless Repository (as above)
  3. Review the SAM Template, Permissions and License as appropriate
  4. In the 'Application settings' box, choose a CloudFormation Stack name for this application
  5. Tick the box to 'acknowledge that this app creates custom IAM roles' (see below for details of the IAM Role)
  6. Click 'Deploy'

AWS will now build a CloudFormation Stack, a Lambda Application and create a new Lambda Function.

Deploying to CloudFront

  1. Visit your Lambda functions list in US-East-1 (N. Virginia)
  2. You'll see the new function that was created by CloudFormation
  3. Click into the function to edit it
  4. Choose 'Actions' from the buttons at the top of the console
  5. Choose 'Deploy to Lambda@Edge'
  6. Now choose the CloudFront distribution that you wish to apply the function to. Commonly in Static S3/CloudFront website deployments you would have a www and non-www distribution with one redirecting to the other. Choose the distribution which is the final source of your website.
  7. Change the 'CloudFront Event' setting to 'Viewer request'
  8. All other settings remain as default
  9. Tick the 'Confirm deploy' acknowledgement
  10. Click 'Deploy'

AWS will now deploy the Lambda@Edge function to all Edge locations for your chosen CloudFront distribution. This may take a few minutes.

Once deployed, visit your website. You will find that https://{your-website.com}/path/ redirects as HTTP 301 to https://{your-website.com}/path (without the trailing slash)

Notes on IAM role creation

This application creates an IAM Role for the Lambda function. The IAM Role uses only standard AWS resources, namely:

  • arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole - to give the Lambda function basic execution permissions
  • A Trust Relationship with edgelambda.amazonaws.com - to allow publishing of the Lambda function as Lambda@Edge

Questions / support

If you'd like to reach out to us with any questions you can find our details at sinovi.uk/contact

License

MIT License

lambda-edge-remove-trailing-slash's People

Contributors

dependabot[bot] avatar felipebarbosa29 avatar hicksy avatar jgalley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lambda-edge-remove-trailing-slash's Issues

Update runtime to nodejs18.x

Hello, since Node.js 16 End-Of-Life (EOL) reached on September 11, 2023, this code must be update.
Are there any plans to do it?

Thanks in advanced!

Too many redirect Error

I rewrote this in nodejs12.x and deployed in my AWS account to test it. When hitting the URL with / (eg: cloudfrontdl.net/page1/) it redirects and then throws too many redirect error.
Can you please let me know how you have resolved it?

Open Redirect issue

Hello,
I want to suggest to update the code adding this block due to solve a Open Redirest issue.

[...]
    var request = event.Records[0].cf.request;

    if (/^\/\/[^\/]+/.test(request.uri)) {
        const response = {
            status: '404',
            statusDescription: 'Not Found'
        };
        return callback(null, response);
    }

    if(request.uri !== '/' && request.uri.substr(-1) === '/') {
[...]

Whitout this if block if you try to navigate an url like https://www.example.com//acme.it/ (apy attention to double slashes and trailing slash) yuo will redirect to https://acme.it site.

Hoping I was helpfull I want to thank you for your code.

Best,
Matteo

Update to node 16 needed

Hello, since Node.js 12 End-Of-Life (EOL) reached on April 30, 2022, this code must be update.
Are there any plans to do it?

Thank you very much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.