-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
68 lines (56 loc) · 1.89 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
import https from 'https';
import { parse, UrlWithStringQuery } from 'url';
import { CloudFormationCustomResourceEvent, Context, CloudFormationCustomResourceResponse } from 'aws-lambda';
import { retry } from './retry';
export const SUCCESS = "SUCCESS";
export const FAILED = "FAILED";
type ResponseStatus = typeof SUCCESS | typeof FAILED;
export const send = async (
event: CloudFormationCustomResourceEvent,
context: Context,
status: ResponseStatus,
response?: any,
physicalResourceId?: string,
retries = 3
) => {
const cfnResponse: CloudFormationCustomResourceResponse = {
Status: status,
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
PhysicalResourceId: physicalResourceId || context.logStreamName,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: response
};
const responseBody = JSON.stringify(cfnResponse);
console.log("Response body:\n", responseBody);
const parsedUrl = parse(event.ResponseURL);
const boundRequest = sendRequest.bind(null, parsedUrl, responseBody);
await retry(boundRequest, { retries });
};
const sendRequest = async (parsedUrl: UrlWithStringQuery, responseBody: string) => {
const options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: "PUT",
headers: {
"content-type": "",
"content-length": responseBody.length
}
};
return new Promise((resolve, reject) => {
var request = https.request(options, (response) => {
console.log("Status code: " + response.statusCode);
console.log("Status message: " + response.statusMessage);
resolve();
});
request.on("error", (error) => {
console.log("send(..) failed executing https.request(..): " + error);
reject(error);
});
request.write(responseBody);
request.end();
});
};
export default send;