-
Notifications
You must be signed in to change notification settings - Fork 160
/
cfn-response.ts
46 lines (42 loc) · 1.12 KB
/
cfn-response.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
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import { request } from "https";
export enum Status {
"SUCCESS" = "SUCCESS",
"FAILED" = "FAILED",
}
export async function sendCfnResponse(props: {
event: {
StackId: string;
RequestId: string;
LogicalResourceId: string;
ResponseURL: string;
};
status: Status;
reason?: string;
data?: {
[key: string]: string;
};
physicalResourceId?: string;
}) {
const response = {
Status: props.status,
Reason: props.reason?.toString() || "See CloudWatch logs",
PhysicalResourceId: props.physicalResourceId || "no-explicit-id",
StackId: props.event.StackId,
RequestId: props.event.RequestId,
LogicalResourceId: props.event.LogicalResourceId,
Data: props.data || {},
};
await new Promise<void>((resolve, reject) => {
const options = {
method: "PUT",
headers: { "content-type": "" },
};
request(props.event.ResponseURL, options)
.on("error", (err) => {
reject(err);
})
.end(JSON.stringify(response), "utf8", resolve);
});
}