This function allows writing items to a DynamoDB. Also include better documentation in the README.
58 lines
No EOL
2.3 KiB
JavaScript
58 lines
No EOL
2.3 KiB
JavaScript
/* Copyright 2015 Amazon Web Services, Inc. or its affiliates. All Rights Reserved.
|
|
This file is licensed to you under the AWS Customer Agreement (the "License").
|
|
You may not use this file except in compliance with the License.
|
|
A copy of the License is located at http://aws.amazon.com/agreement/.
|
|
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
|
|
See the License for the specific language governing permissions and limitations under the License. */
|
|
|
|
exports.SUCCESS = "SUCCESS";
|
|
exports.FAILED = "FAILED";
|
|
|
|
exports.send = function(event, context, responseStatus, responseData, physicalResourceId) {
|
|
|
|
var responseBody = JSON.stringify({
|
|
Status: responseStatus,
|
|
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
|
|
PhysicalResourceId: physicalResourceId || context.logStreamName,
|
|
StackId: event.StackId,
|
|
RequestId: event.RequestId,
|
|
LogicalResourceId: event.LogicalResourceId,
|
|
Data: responseData
|
|
});
|
|
|
|
console.log("Response body:\n", responseBody);
|
|
|
|
var https = require("https");
|
|
var url = require("url");
|
|
|
|
// This script comes from http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#cfn-lambda-function-code-cfnresponsemodule
|
|
// The only change is this 'if' statement around this block of code, so it doesn't
|
|
// fail when the ResonseURL is missing (i.e. during manual testing).
|
|
if (event.ResponseURL) {
|
|
var parsedUrl = url.parse(event.ResponseURL);
|
|
var options = {
|
|
hostname: parsedUrl.hostname,
|
|
port: 443,
|
|
path: parsedUrl.path,
|
|
method: "PUT",
|
|
headers: {
|
|
"content-type": "",
|
|
"content-length": responseBody.length
|
|
}
|
|
};
|
|
|
|
var request = https.request(options, function(response) {
|
|
console.log("Status code: " + response.statusCode);
|
|
console.log("Status message: " + response.statusMessage);
|
|
context.done();
|
|
});
|
|
|
|
request.on("error", function(error) {
|
|
console.log("send(..) failed executing https.request(..): " + error);
|
|
context.done();
|
|
});
|
|
|
|
request.write(responseBody);
|
|
request.end();
|
|
}
|
|
} |