forked from aws-samples/cloudfront-authorization-at-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (66 loc) · 2.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import {
CloudFormationCustomResourceHandler,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
} from "aws-lambda";
import staticSiteUpload from "s3-spa-upload";
import { mkdirSync } from "fs";
import { sendCfnResponse, Status } from "./cfn-response";
interface Configuration {
BucketName: string;
}
async function uploadPages(
action: "Create" | "Update" | "Delete",
config: Configuration,
physicalResourceId?: string
) {
if (action === "Create" || action === "Update") {
await staticSiteUpload(`${__dirname}/pages`, config.BucketName);
} else {
// "Trick" to empty the bucket is to upload an empty dir
mkdirSync("/tmp/empty_directory", { recursive: true });
await staticSiteUpload("/tmp/empty_directory", config.BucketName, {
delete: true,
});
}
return physicalResourceId || "StaticSite";
}
export const handler: CloudFormationCustomResourceHandler = async (
event,
context
) => {
console.log(JSON.stringify(event, undefined, 4));
const { ResourceProperties, RequestType } = event;
const { ServiceToken, ...config } = ResourceProperties;
const { PhysicalResourceId } = event as
| CloudFormationCustomResourceDeleteEvent
| CloudFormationCustomResourceUpdateEvent;
let status = Status.SUCCESS;
let physicalResourceId: string | undefined;
let data: { [key: string]: any } | undefined;
let reason: string | undefined;
try {
physicalResourceId = await Promise.race([
uploadPages(RequestType, config as Configuration, PhysicalResourceId),
new Promise<undefined>((_, reject) =>
setTimeout(
() => reject(new Error("Task timeout")),
context.getRemainingTimeInMillis() - 500
)
),
]);
} catch (err) {
console.error(err);
status = Status.FAILED;
reason = `${err}`;
}
await sendCfnResponse({
event,
status,
data,
physicalResourceId,
reason,
});
};