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函数是否成功更新堆栈。这将

相关内容

热门资讯

透视透视!wepoker免费透... 透视透视!wepoker免费透视,微信小程序可以开挂吗,解迷透视(果真有挂)-哔哩哔哩1、下载好we...
透视揭露!We poker辅助... 透视揭露!We poker辅助器下载,WePOker有没有透视方法(透视)一直真的有脚本(哔哩哔哩)...
备受关注的!wepoker有没... 备受关注的!wepoker有没有挂,微乐小程序游戏辅助器脚本官网,教你脚本(证实有挂)-哔哩哔哩we...
透视必备!hhpoker软件可... 透视必备!hhpoker软件可以玩吗,hh poker透视器下载(透视)一贯是有工具(哔哩哔哩)1、...
记者获悉!we-poker辅助... 记者获悉!we-poker辅助,微乐小程序免费黑科技5个玩家常用方法,详情脚本(有挂细节)-哔哩哔哩...
透视详细!德普之星有透视辅助吗... 透视详细!德普之星有透视辅助吗,德普之星辅助软件(透视)切实存在有教程(哔哩哔哩)在进入软件靠谱后,...
透视app!wepoker有机... 透视app!wepoker有机器人吗,微信小程序微乐辅助免费版,科普透视(有挂技巧)-哔哩哔哩1)w...
透视了解!wpk辅助器是真的吗... 透视了解!wpk辅助器是真的吗,wpk透视是真的假的(透视)总是有插件(哔哩哔哩)1、免费辅助多个强...
辅助透视!hhpoker德州机... 辅助透视!hhpoker德州机器人,微信微乐辅助,揭幕脚本(有挂攻略)-哔哩哔哩1、用户打开应用后不...
透视解谜!wepoker辅助软... 透视解谜!wepoker辅助软件视频,wepoker智能辅助插件(透视)其实是真的方法(哔哩哔哩)1...