Skip to content

Instantly share code, notes, and snippets.

@ottokruse
Last active June 28, 2020 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ottokruse/f08099eea65412f7376525d5d1cd968b to your computer and use it in GitHub Desktop.
Save ottokruse/f08099eea65412f7376525d5d1cd968b to your computer and use it in GitHub Desktop.
A better cfn-response for NodeJS, only depends on stdlib
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 || "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((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);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment