gpt4 book ai didi

javascript - 从 Lambda 函数内更新 CloudFormation 堆栈的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 07:14:10 25 4
gpt4 key购买 nike

到目前为止,我的代码如下所示:

const AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-west-1' });
const CF = new AWS.CloudFormation({ apiVersion: '2010-05-15' });

const updateParams = {
StackName: myStackName,
UsePreviousTemplate: true,
Parameters: [
{
ParameterKey: "StackOffline",
ParameterValue: "Online"
}
],
Capabilities: [
"CAPABILITY_IAM",
"CAPABILITY_AUTO_EXPAND"
]
};

exports.handler = async (event) => {
CF.updateStack(updateParams, (err, data) => {
if (err)
console.log(err, err.stack);
else
console.log(data);
});

// ...
};

问题是......它没有做任何事情。当它运行时,它实际上不会记录任何内容;不是数据,也不是错误消息。

我尝试同时使用堆栈名称和唯一堆栈 ID,但都不起作用。

最后,我保存了 updateStack() 的返回值(我认为是 Request),这是它的 response 属性:

response: Response {
request: [Circular *1],
data: null,
error: null,
retryCount: 0,
redirectCount: 0,
httpResponse: HttpResponse {
statusCode: undefined,
headers: {},
body: undefined,
streaming: false,
stream: null,
_abortCallback: [Function: callNextListener]
},
maxRetries: 3,
maxRedirects: 10
}

(如果需要的话,我还可以发布整个Request。我没有发布,因为它太大了,而且我不知道它是否包含任何敏感信息。)

我认为这些 null 值是有问题的。我哪里出错了?

最佳答案

您遇到了古老的async/awaitcallback 问题。太长了;不要将它们组合起来。

AWS Lambda function handler in Node.js

For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the invoker until all event loop tasks are finished. If the function times out, an error is returned instead. You can configure the runtime to send the response immediately by setting context.callbackWaitsForEmptyEventLoop to false.

将您的代码更改为如下所示:

const AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-west-1' });
const CF = new AWS.CloudFormation({ apiVersion: '2010-05-15' });

const updateParams = {
StackName: myStackName,
UsePreviousTemplate: true,
Parameters: [
{
ParameterKey: "StackOffline",
ParameterValue: "Online"
}
],
Capabilities: [
"CAPABILITY_IAM",
"CAPABILITY_AUTO_EXPAND"
]
};

exports.handler = async (event) => {
try {
const data = await CF.updateStack(updateParams).promise();
console.log(data);

// ...

}
catch (err) {
console.log(err, err.stack);
}
};

关于javascript - 从 Lambda 函数内更新 CloudFormation 堆栈的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67167995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com