AWS自定义资源无法识别成功或失败信号。
创始人
2024-09-28 05:30:15
0

在AWS Lambda函数中使用sendResponse函数发送响应,以告知CloudFormation成功还是失败。

以下是示例代码:

exports.handler = function(event, context, callback) {
  // 在Lambda函数中处理事件
  // 在处理完成后,使用sendResponse向CloudFormation发送响应
  var responseStatus = "SUCCESS";
  var responseData = {};
  var physicalResourceId = "myResourceId";
  
  try {
    // 处理事件代码
  } catch (err) {
    responseStatus = "FAILED";
    responseData = {"Error": err};
  }
  
  sendResponse(event, context, responseStatus, responseData, physicalResourceId);
};

function sendResponse(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
  });

  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
    }
  };

  var request = https.request(options, function(response) {
    context.done();
  });

  request.on("error", function(error) {
    console.log("sendResponse Error: ", error);
    context.done();
  });

  request.write(responseBody);
  request.end();
}

在Lambda函数中,您可以在处理事件后使用sendResponse函数发送响应。它会将响应状态、有关事件的一些信息以及有关响应的其他信息打包到JSON中,并使用HTTPS PUT将其发送回CloudFormation。然后,在CloudFormation中,您可以使用“Success”或“Failed”作为返回值,以显示Lambda函数是否成功更新堆栈。这将

相关内容

热门资讯

推荐一款!hhpkoer辅助器... 推荐一款!hhpkoer辅助器视频,wepoker破解版内购,AI教程(有挂技巧);致您一封信;亲爱...
透视总结"wepok... 透视总结"wepoker透视脚本视频"详细辅助力荐教程(原生存在有挂);建议优先通过wepoker透...
透视玄学"wepok... 这是一款非常优秀的wepoker怎么设置房间 ia辅助检测软件,能够让你了解到wepoker怎么设置...
分享开挂内幕!德州局透视脚本免... 分享开挂内幕!德州局透视脚本免费版下载手机版,wepoker私人局俱乐部,2025版教程(有挂方法)...
透视挂透视"aapo... 透视挂透视"aapoker辅助器怎么用"详细辅助爆料教程(往昔有挂)这是由厦门游乐互动科技有限公司精...
信息共享!hhpoker辅助,... 信息共享!hhpoker辅助,wepoker有没有插件,德州论坛(有挂软件);《WPK辅助透视》‌:...
透视软件"wepok... 此外,数据分析德州(wepoker私人局可以透视)辅助神器app还具备辅助透视行为开挂功能,通过对客...
信息共享!wepoker有没有... 信息共享!wepoker有没有透视方法,hhpoker辅助器视频,必赢教程(有挂软件);1.wepo...
透视有挂"智星德州插... 透视有挂"智星德州插件"详细辅助细节揭秘(切实是有挂)1、很好的工具软件,可以解锁游戏的ai质量和中...
重大科普!hhpoker脚本下... 重大科普!hhpoker脚本下载,德州机器人代打脚本,辅助教程(有挂透明);德州机器人代打脚本软件透...