Skip to content

Commit

Permalink
Inline Lambda code into CloudFormation template.
Browse files Browse the repository at this point in the history
read x-distribution-name header in TileJSON response if PUBLIC_HOSTNAME is not set.
  • Loading branch information
bdon committed Aug 28, 2024
1 parent 61020e6 commit 4fbbb8b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
7 changes: 7 additions & 0 deletions serverless/aws/inline_lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { yamlParse, yamlDump } from 'yaml-cfn';
import fs from 'fs';

const template = yamlParse(fs.readFileSync('protomaps-template.yaml'));
const code = fs.readFileSync('dist/index.js','utf8');
template.Resources.LambdaFunction.Properties.Code = {ZipFile:code};
fs.writeFileSync('out.yaml', yamlDump(template));
60 changes: 59 additions & 1 deletion serverless/aws/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions serverless/aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"@types/node": "^18.11.2",
"esbuild": "^0.20.0",
"tsx": "^4.7.0",
"typescript": "^4.8.4"
"typescript": "^4.8.4",
"yaml-cfn": "^0.3.2"
},
"private": true,
"scripts": {
"tsc": "tsc --noEmit --watch",
"build": "esbuild src/index.ts --target=es2020 --outfile=dist/index.mjs --format=esm --bundle --platform=node --target=node18 --external:@aws-sdk/client-s3 --external:@aws-sdk/node-http-handler --banner:js=//$(git describe --always) && cd dist && zip lambda_function.zip index.mjs",
"build": "esbuild src/index.ts --target=es2020 --minify --outfile=dist/index.js --format=cjs --bundle --platform=node --target=node20 --external:@aws-sdk/client-s3 --external:@aws-sdk/node-http-handler --banner:js=//sha:$(git describe --always)",
"test": "tsx ../shared/index.test.ts",
"biome": "biome check --config-path=../../js/ src --apply",
"biome-check": "biome check --config-path=../../js src"
Expand Down
4 changes: 2 additions & 2 deletions serverless/aws/protomaps-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Resources:
return request;
}
FunctionConfig:
Comment: 'Add x-distribution-id header.'
Comment: 'Add x-distribution-domain header.'
Runtime: cloudfront-js-2.0

CloudFrontDistribution:
Expand Down Expand Up @@ -181,4 +181,4 @@ Resources:
PUBLIC_HOSTNAME: !Ref PublicHostname
Code:
S3Bucket: !Ref BucketName
S3Key: lambda_function3.zip
S3Key: lambda_function.zip
10 changes: 1 addition & 9 deletions serverless/aws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,12 @@ export const handlerRaw = async (
const header = await p.getHeader();

if (!tile) {
if (!process.env.PUBLIC_HOSTNAME) {
return apiResp(
501,
"PUBLIC_HOSTNAME must be set for TileJSON",
false,
headers
);
}
headers["Content-Type"] = "application/json";

const t = tileJSON(
header,
await p.getMetadata(),
process.env.PUBLIC_HOSTNAME,
process.env.PUBLIC_HOSTNAME || event.headers['x-distribution-domain-name'] || "",
name
);

Expand Down

0 comments on commit 4fbbb8b

Please sign in to comment.