ID 为 [ApiGatewayLambdaS3Event] 的资源无效未为 S3 类型的资源定义属性 BucketName

问题描述

我正在我的 sam.yaml 文件添加一个 S3 事件,以在将文件添加到存储桶中时触发 lambda 函数

我将要创建的这些函数的触发器将用于现有的 S3 存储桶。我是否可以使用 SAM 为现有存储桶创建触发器,还是需要手动创建触发器?

但是,我收到以下错误

Resource with id [ApiGatewayLambdaS3Event] is invalid. property BucketName not defined for resource of type S3

这是我创建的 template.yaml 文件,其中包含我尝试创建的所有 lambda 函数和触发器。

AWstemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: CD Demo Lambda
Resources:
  CDDemoLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./FunctionOne
      FunctionName: CDDemoLambda
      Description: 'Lambda function for CD Demo Test'
      MemorySize: 128
      Timeout: 30
      Events:
        getAZsAPI:
          Type: Api
          Properties:
            Path: /getazs
            Method: get
            
  HelloWorld:
    Type: 'AWS::Serverless::Function'
    Properties:
      AutopublishAlias: qaTest
      Handler: qa-hello-world.lambda_handler
      Runtime: python3.6
      CodeUri: ./FunctionTwo
      FunctionName: HelloWorld
      Description: 'Hello WOrld'
      
  ApiGatewayLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      AutopublishAlias: apigateway
      Handler: lambdafunctionthree.lambda_handler
      Runtime: python3.6
      FunctionName: ApiGatewayLambda
      CodeUri: ./FunctionThree
      Description: 'Hello WOrld'

添加要与我的 lambda 函数一起创建的 S3 事件触发器,我需要进行哪些更改。

解决方法

来自documentation

您可以进一步混合和匹配您希望调用 lambda 的事件类型。

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: 'AWS::Serverless-2016-10-31'
    Description: CD Demo Lambda
    Resources:
    CDDemoLambda:
        Type: 'AWS::Serverless::Function'
        Properties:
        Handler: lambda_function.lambda_handler
        Runtime: python3.6
        CodeUri: ./FunctionOne
        FunctionName: CDDemoLambda
        Description: 'Lambda function for CD Demo Test'
        MemorySize: 128
        Timeout: 30
        Events:
            getAZsAPI:
            Type: Api
            Properties:
                Path: /getazs
                Method: get
    SrcBucket:
        Type: 'AWS::S3::Bucket'

    HelloWorld:
        Type: 'AWS::Serverless::Function'
        Properties:
        AutoPublishAlias: qaTest
        Handler: qa-hello-world.lambda_handler
        Runtime: python3.6
        CodeUri: ./FunctionTwo
        FunctionName: HelloWorld
        Description: 'Hello WOrld'
        Policies:
            S3ReadPolicy:
            BucketName: !Ref SrcBucket
            Events:
                CreateThumbnailEvent:
                Type: S3
                Properties:
                    Bucket: !Ref SrcBucket
                    Events: s3:ObjectCreated:*

    LambdaInvokePermission:
        Type: 'AWS::Lambda::Permission'
        Properties:
        FunctionName: !GetAtt HelloWorld.Arn
        Action: 'lambda:InvokeFunction'
        Principal: 's3.amazonaws.com'
        SourceAccount: !Sub ${AWS::AccountId}
        SourceArn: !GetAtt SrcBucket.Arn

    ApiGatewayLambda:
        Type: 'AWS::Serverless::Function'
        Properties:
        AutoPublishAlias: apigateway
        Handler: lambdafunctionthree.lambda_handler
        Runtime: python3.6
        FunctionName: ApiGatewayLambda
        CodeUri: ./FunctionThree
        Description: 'Hello WOrld'