const { DocumentClient } = require ( "aws-sdk/clients/dynamodb" ) ; const DB_TABLE_NAME = "{DB_TABLE_NAME}" ; const DB_TABLE_REGION = "{DB_TABLE_REGION}" ; const documentClient = new DocumentClient ( { convertEmptyValues : true , region : DB_TABLE_REGION } ) ; exports . handler = async ( event ) => { const request = event . Records [ 0 ] . cf . request ; const requestedDomain = request . headers . host [ 0 ] . value ; const originDomain = request . origin . custom . domainName ; const params = { TableName : DB_TABLE_NAME , Key : { PK : `DOMAIN#${ requestedDomain }` , SK : "A" } } ; const { Item } = await documentClient . get ( params ) . promise ( ) ; if ( Item ) { const from = request . uri ; request . uri = `/${ Item . tenant }${ from }` ; console . log ( `Rewriting request from "${ from }" to "${ request . uri }"` ) ; } else { console . log ( `Failed to find a tenant for domain "${ requestedDomain }"` ) ; } request . headers . host [ 0 ] . value = originDomain ; return request ; } ;