/lambda-edge-remove-trailing-slash

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

Primary LanguageJavaScriptMIT LicenseMIT

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 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-how-it-works.html
  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

License

MIT License (https://github.com/sinovi/lambda-edge-remove-trailing-slash/blob/master/LICENSE)