cloudformation 中的自定义函数不起作用
Custom function in cloudformation does not work
我需要在云形成中调用 lambda:
这是我的 yaml 模板:
#test-custom-func
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
project:
Description: project
Type: String
ConstraintDescription: Any string
EnvironmentApp:
Description: EnvironmentApp
Type: String
ConstraintDescription: Any string
Description: ddddd
Resources:
removeBucket:
Type: Custom::Myfunction
Properties:
ServiceToken: arn:aws:lambda:us-east-1:xxxxxxxxx:function:test1
这里是测试 lambda 函数:
exports.handler = (event, context, callback) => {
// TODO implement
console.log("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
callback(null, 'Hello from Lambda');
};
如您所见,一切都非常基础。当我 运行 yaml 堆栈时,它永远不会被创建并保持在创建中状态,并且在很长一段时间后它会失败。
我在使用自定义函数时是否遗漏了什么?
您需要明确向 CloudFormation 发送响应,而不是使用 callback
方法。
将我找到的这个片段 in the doc 插入您的代码中:
// Send response to the pre-signed S3 URL
function sendResponse(event, context, responseStatus, responseData) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
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");
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
}
};
console.log("SENDING RESPONSE...\n");
var request = https.request(options, function(response) {
console.log("STATUS: " + response.statusCode);
console.log("HEADERS: " + JSON.stringify(response.headers));
// Tell AWS Lambda that the function execution is done
context.done();
});
request.on("error", function(error) {
console.log("sendResponse Error:" + error);
// Tell AWS Lambda that the function execution is done
context.done();
});
// write data to request body
request.write(responseBody);
request.end();
}
并在逻辑完成后调用 sendResponse
,如下所示:
var responseStatus = "SUCCESS";
var responseData = {};
sendResponse(event, context, responseStatus, responseData);
我需要在云形成中调用 lambda:
这是我的 yaml 模板:
#test-custom-func
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
project:
Description: project
Type: String
ConstraintDescription: Any string
EnvironmentApp:
Description: EnvironmentApp
Type: String
ConstraintDescription: Any string
Description: ddddd
Resources:
removeBucket:
Type: Custom::Myfunction
Properties:
ServiceToken: arn:aws:lambda:us-east-1:xxxxxxxxx:function:test1
这里是测试 lambda 函数:
exports.handler = (event, context, callback) => {
// TODO implement
console.log("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
callback(null, 'Hello from Lambda');
};
如您所见,一切都非常基础。当我 运行 yaml 堆栈时,它永远不会被创建并保持在创建中状态,并且在很长一段时间后它会失败。
我在使用自定义函数时是否遗漏了什么?
您需要明确向 CloudFormation 发送响应,而不是使用 callback
方法。
将我找到的这个片段 in the doc 插入您的代码中:
// Send response to the pre-signed S3 URL
function sendResponse(event, context, responseStatus, responseData) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
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");
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
}
};
console.log("SENDING RESPONSE...\n");
var request = https.request(options, function(response) {
console.log("STATUS: " + response.statusCode);
console.log("HEADERS: " + JSON.stringify(response.headers));
// Tell AWS Lambda that the function execution is done
context.done();
});
request.on("error", function(error) {
console.log("sendResponse Error:" + error);
// Tell AWS Lambda that the function execution is done
context.done();
});
// write data to request body
request.write(responseBody);
request.end();
}
并在逻辑完成后调用 sendResponse
,如下所示:
var responseStatus = "SUCCESS";
var responseData = {};
sendResponse(event, context, responseStatus, responseData);