AWSTemplateFormatVersion: '2010-09-09' Description: Deploys a Lambda Function to rewrite CloudFront url to include index.html of subdirectories Resources: LambdaRewrite: Type: AWS::Lambda::Function Properties: Runtime: nodejs12.x Role: !GetAtt LambdaExecutionRole.Arn Handler: index.handler Code: ZipFile: | 'use strict'; 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'); // Log the URI as received by CloudFront and the new URI to be used to fetch from origin console.log("Old URI: " + olduri); console.log("New URI: " + newuri); // Replace the received URI with the URI that includes the index page request.uri = newuri; // Return to CloudFront return callback(null, request); }; Description: Created by CloudFormation - Handler to rewrite url ending in / to /index.html LambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com - edgelambda.amazonaws.com Action: - sts:AssumeRole Path: "/" Policies: - PolicyName: root PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - logs:* Resource: arn:aws:logs:*:*:* LambdaVersion: Type: AWS::Lambda::Version Properties: FunctionName: !Ref LambdaRewrite Description: v1