【问题标题】:Resource with id [ApiGatewayLambdaS3Event] is invalid. property BucketName not defined for resource of type S3ID 为 [ApiGatewayLambdaS3Event] 的资源无效。未为 S3 类型的资源定义属性 BucketName
【发布时间】:2021-04-18 17:28:00
【问题描述】:

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

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

但是,我收到以下错误

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

这是我使用我尝试创建的所有 lambda 函数和触发器创建的 template.yaml 文件。

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 事件触发器。

【问题讨论】:

    标签: python-3.x amazon-web-services amazon-s3 aws-sam


    【解决方案1】:

    来自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'
    

    【讨论】:

    • 您好,非常感谢。我可以使用现有的存储桶还是必须提及新的存储桶名称?
    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    相关资源
    最近更新 更多