forked from jetbridge/cdk-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve bundling of various lambda functions
- Loading branch information
Showing
17 changed files
with
311 additions
and
175 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { CdkCustomResourceEvent, CdkCustomResourceHandler } from 'aws-lambda'; | ||
import AWS from 'aws-sdk'; | ||
|
||
async function tryGetObject(bucket, key, tries) { | ||
const s3 = new AWS.S3(); | ||
try { | ||
return await s3.getObject({ Bucket: bucket, Key: key }).promise(); | ||
} catch (err) { | ||
console.error('Failed to retrieve object', key, err); | ||
// for now.. skip it. might be a rollback and the file is no longer available. | ||
// // if access denied - wait a few seconds and try again | ||
// if (err.code === 'AccessDenied' && tries < 10) { | ||
// console.info('Retrying for object', key); | ||
// await new Promise((res) => setTimeout(res, 5000)); | ||
// return tryGetObject(bucket, key, ++tries); | ||
// } else { | ||
// throw err; | ||
// } | ||
} | ||
} | ||
|
||
const doRewrites = async (event: CdkCustomResourceEvent) => { | ||
// rewrite static files | ||
const s3 = new AWS.S3(); | ||
const { s3keys, bucket, replacements } = event.ResourceProperties; | ||
if (!s3keys || !bucket || !replacements) { | ||
console.error('Missing required properties'); | ||
return; | ||
} | ||
const promises = s3keys.map(async (key) => { | ||
// get file | ||
const params = { Bucket: bucket, Key: key }; | ||
console.info('Rewriting', key, 'in bucket', bucket); | ||
const res = await tryGetObject(bucket, key, 0); | ||
if (!res) return; | ||
|
||
// get body | ||
const bodyPre = res.Body?.toString('utf-8'); | ||
if (!bodyPre) return; | ||
let bodyPost = bodyPre; | ||
|
||
// do replacements of tokens | ||
Object.entries(replacements as Record<string, string>).forEach(([token, value]) => { | ||
bodyPost = bodyPost.replace(token, value); | ||
}); | ||
|
||
// didn't change? | ||
if (bodyPost === bodyPre) return; | ||
|
||
// upload | ||
console.info('Rewrote', key, 'in bucket', bucket); | ||
const putParams = { | ||
...params, | ||
Body: bodyPost, | ||
ContentType: res.ContentType, | ||
ContentEncoding: res.ContentEncoding, | ||
CacheControl: res.CacheControl, | ||
}; | ||
await s3.putObject(putParams).promise(); | ||
}); | ||
await Promise.all(promises); | ||
}; | ||
|
||
// search and replace tokenized values of designated objects in s3 | ||
export const handler: CdkCustomResourceHandler = async (event) => { | ||
const requestType = event.RequestType; | ||
if (requestType === 'Create' || requestType === 'Update') { | ||
await doRewrites(event); | ||
} | ||
|
||
return event; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import url from 'url'; | ||
import type { CloudFrontRequestHandler } from 'aws-lambda'; | ||
|
||
/** | ||
* This fixes the "host" header to be the host of the origin. | ||
* The origin is the lambda server function URL. | ||
* If we don't provide its expected "host", it will not know how to route the request. | ||
*/ | ||
export const handler: CloudFrontRequestHandler = (event, _context, callback) => { | ||
const request = event.Records[0].cf.request; | ||
// console.log(JSON.stringify(request, null, 2)) | ||
|
||
// get origin url from header | ||
const originUrlHeader = request.origin?.custom?.customHeaders['x-origin-url']; | ||
if (!originUrlHeader || !originUrlHeader[0]) { | ||
console.error('Origin header wasn"t set correctly, cannot get origin url'); | ||
return callback(null, request); | ||
} | ||
const urlHeader = originUrlHeader[0].value; | ||
const originUrl = url.parse(urlHeader, true); | ||
if (!originUrl.host) throw new Error('Origin url host is missing'); | ||
|
||
request.headers['x-forwarded-host'] = [{ key: 'x-forwarded-host', value: request.headers.host[0].value }]; | ||
request.headers.host = [{ key: 'host', value: originUrl.host }]; | ||
callback(null, request); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.