根据 AWS 文档 here (请参阅:RequestType)自定义 Lambda 函数可以由以下类型的 CloudFormation 操作触发:create-stack
, update-stack
,或delete-stack
操作。
我的 lambda 配置为仅在 create-stack
上执行其任务,但是当我将此自定义资源和 Lambda 添加到我的(预先存在的)CloudFormation 堆栈并更新它时,Lambda 的运行就像堆栈已创建一样,即使它已更新。
它不应该只在第一次创建时运行吗?为什么它会在第一次更新时起作用?
注意:后续更新不会运行
相关代码片段
CloudFormation 资源:
LambdaUpdateNodeStatusFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Ref LambdaUpdateNodeStatusFunctionName
CodeUri: ../../../bin/handlers/espnode/lambdaupdatenodesstatus_c
Handler: lambdaupdatenodesstatus
Role: !GetAtt LambdaUpdateNodeStatusExecutionRole.Arn
MemorySize: 512 #MB
Timeout: 900
LambdaUpdateNodeStatusFunctionLog:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref LambdaUpdateNodeStatusFunctionName]]
LambdaUpdateNodeStatusCustomResource:
Type: Custom::LambdaUpdateNodeStatusCustomResource
Properties:
ServiceToken: !GetAtt LambdaUpdateNodeStatusFunction.Arn
Version: !Ref BuildVersion
Lambda代码:
type CustomLambdaRequest struct {
RequestType string `json:"RequestType"`
ResponseURL string `json:"ResponseURL"`
StackId string `json:"StackId"`
RequestId string `json:"RequestId"`
ResourceType string `json:"ResourceType"`
LogicalResourceId string `json:"LogicalResourceId"`
PhysicalResourceId string `json:"PhysicalResourceId"`
}
func Handler(customLambdaRequest CustomLambdaRequest) {
if customLambdaRequest.RequestType == "Create" {
// Do some processing here
} else {
// Do Nothing here
}
}
最佳答案
正如您提到的,Lambda 支持的自定义资源在 create-stack
、update-stack
和 delete-stack
中触发> 操作。
但它们会针对模板中的每个自定义资源触发。
这意味着当您向模板添加新的 Custom::
资源时,它将使用 RequestType == Create
调用 lambda 并保存 LogicalResourceId
处于云形成状态。稍后当您更新堆栈时,它将触发具有更新请求类型等的函数。
关于amazon-web-services - 自定义资源 Lambda 应在创建时运行,但在第一次更新时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72855518/