Last active
April 21, 2021 04:44
-
-
Save blackbing/e0a913af1b376016ed003b97f2d240f5 to your computer and use it in GitHub Desktop.
Host Wildcard Subdomain in One S3 Bucket https://blackbing.medium.com/host-wildcard-subdomain-in-one-s3-bucket-with-lambda-edge-979c7cb282dc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const http = require('http'); | |
exports.handler = (event, context, callback) => { | |
const request = event.Records[0].cf.request; | |
const response = event.Records[0].cf.response; | |
if (response.status == 404) { | |
const domainHash = request.headers['x-domain-hash'] && request.headers['x-domain-hash'][0].value; | |
if (domainHash) { | |
const host = request.headers['host'][0].value; | |
const url = `http://${host}/${domainHash}/index.html`; | |
console.log(url); | |
downloadContent(url, function(body, error) { | |
if (error) { | |
console.log(error) | |
callback(null, response); | |
} else { | |
const newResponse = { | |
status: 200, | |
statusDescription: 'OK', | |
headers: response.headers, | |
body | |
}; | |
newResponse.headers['content-type'] = [{ | |
key: 'Content-Type', | |
value: 'text/html' | |
}]; | |
callback(null, newResponse); | |
} | |
}) | |
} | |
} else { | |
callback(null, response); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment