amazon-web-services - 自定义资源 Lambda 应在创建时运行,但在第一次更新时运行

标签 amazon-web-services aws-cloudformation aws-cloudformation-custom-resource

根据 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-stackupdate-stackdelete-stack 中触发> 操作。

但它们会针对模板中的每个自定义资源触发。

这意味着当您向模板添加新的 Custom:: 资源时,它将使用 RequestType == Create 调用 lambda 并保存 LogicalResourceId 处于云形成状态。稍后当您更新堆栈时,它将触发具有更新请求类型等的函数。

关于amazon-web-services - 自定义资源 Lambda 应在创建时运行,但在第一次更新时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72855518/

相关文章:

json - 无法在 Amazon ECS 优化的 AMI 上通过 cloudformation 安装 cloudwatch 代理

java - AWS SWF 在特定条件下重新启动工作流程

amazon-web-services - 有没有办法导出和导入 Amazon Connect - 联系流

java vm 选项和 AWS::ECS::TaskDefinition

amazon-web-services - AWS CloudFormation VPC CIDR 分配给安全组

amazon-web-services - 如何通过 ARN 删除 AWS 资源?

amazon-s3 - S3 TopicConfiguration - 无法验证以下目标配置

aws-cloudformation - 适用于 NAT 实例的 AWS CloudFormation - 在 LaunchConfig 中禁用 SourceDestinationCheck

amazon-web-services - CloudFormation 模板不选择 key 对

aws-cloudformation - 如何在堆栈更新期间强制完全重新创建自定义资源?