cloudformation中的自定义功能不起作用
Posted
技术标签:
【中文标题】cloudformation中的自定义功能不起作用【英文标题】:Custom function in cloudformation does not work 【发布时间】:2018-04-04 14:36:27 【问题描述】:我需要在云形成中调用 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 堆栈时,它永远不会被创建并保持在创建进行中状态,并且在很长一段时间后它会失败。
我在使用自定义函数时有什么遗漏吗?
【问题讨论】:
【参考方案1】:您需要明确向 CloudFormation 发送响应,而不是使用 callback
方法。
将我找到的这个 sn-p 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);
【讨论】:
以上是关于cloudformation中的自定义功能不起作用的主要内容,如果未能解决你的问题,请参考以下文章
importValue 功能在 cloudformation 中不起作用