Amazon web services cloudformation中的自定义函数不起作用

Amazon web services cloudformation中的自定义函数不起作用,amazon-web-services,aws-lambda,amazon-cloudformation,aws-opsworks,Amazon Web Services,Aws Lambda,Amazon Cloudformation,Aws Opsworks,我需要在云形成中调用lambda: 这是我的yaml模板: #test-custom-func AWSTemplateFormatVersion: "2010-09-09" Parameters: project: Description: project Type: String ConstraintDescription: Any string EnvironmentApp: Description: EnvironmentApp Type:

我需要在云形成中调用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堆栈时,它永远不会被创建,并保持在CREATEIN progress状态,过了很长时间它就会失败


在使用自定义函数时是否遗漏了任何内容?

您需要显式地向CloudFormation发送响应,而不是使用
回调方法

将我找到的这段代码插入您的代码:

// 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发送响应,而不是使用
callback
方法

将我找到的这段代码插入您的代码:

// 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);