@collaborne/custom-cloudformation-resources
Version:
Custom CloudFormation resources
63 lines • 2.88 kB
JavaScript
;
// Based on the cfn-response sources published by AWS at
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = exports.FAILED = exports.SUCCESS = void 0;
const https_1 = __importDefault(require("https"));
exports.SUCCESS = 'SUCCESS';
exports.FAILED = 'FAILED';
function send(request, responseStatus, responseReason, physicalResourceId, responseData = {}, noEcho = false) {
const responseBody = JSON.stringify({
Status: responseStatus,
Reason: responseReason ||
(responseStatus === 'FAILED' ? 'Unknown error' : undefined),
PhysicalResourceId: physicalResourceId || request.PhysicalResourceId,
StackId: request.StackId,
RequestId: request.RequestId,
LogicalResourceId: request.LogicalResourceId,
NoEcho: noEcho,
Data: responseData,
});
if (responseBody.length >= 4096) {
// Warn, but proceed. The problem is that at this point we cannot do anything anymore -- CF will fail, and will do
// rollbacks as needed.
// We do log the complete body here, so that the operator/developer hopefully sees what can be shortened.
// See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html#crpg-ref-responses-fields
console.warn(`Response body length of ${responseBody.length} bytes exceeds CloudFormation limit of 4KiB: ${responseBody}`);
}
const options = {
method: 'PUT',
headers: {
'content-type': 'application/json',
'content-length': responseBody.length,
},
};
return new Promise((resolve, reject) => {
function logProblem(message) {
// Provide enough detail here so that a human could possibly fix the situation using curl.
console.error(`Failed to report status '${responseBody}' to '${request.ResponseURL}': ${message}`);
}
const req = https_1.default.request(request.ResponseURL, options, res => {
if (!res.statusCode || res.statusCode >= 400) {
const message = `Unexpected status code ${res.statusCode}`;
logProblem(message);
reject(new Error(message));
return;
}
resolve();
});
req.on('error', err => {
logProblem(err.message);
reject(err);
});
req.write(responseBody);
req.end();
});
}
exports.send = send;
//# sourceMappingURL=cfn-response.js.map