使用 CloudFormation 在 S3 上添加事件 - “存储桶已存在”错误

问题描述

我正在尝试使用 S3 触发器创建 lambda 函数。在执行模板时,我收到 S3 存储桶已存在错误。我的 S3 中没有任何同名的存储桶,即使在此代码中,我也只创建了一次存储桶,但不知何故,它似​​乎创建了两次存储桶。

以下是我的 cloudformation 模板。

    '''python
    
AWstemplateFormatVersion : 2010-09-09
Parameters:
  BucketName:
    Type: String
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    DependsOn:
      - ProcessingLambdaPermission
    Properties:
      BucketName: !Ref BucketName
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: s3:PutObject:*
            Function: !GetAtt ProcessingLambdaFunction.Arn
            Filter:
              S3Key:
                Rules:
                - Name: suffix
                  Value: .txt

  ProcessingLambdaPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref ProcessingLambdaFunction
      Principal: s3.amazonaws.com
      SourceArn: 'arn:aws:s3:::hope'
      SourceAccount: !Ref AWS::AccountId

  ProcessingLambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Policies:
        - PolicyName: allowLogging
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Action:
              - logs:*
              Resource: arn:aws:logs:*:*:*
        - PolicyName: getAndDeleteObjects
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Action:
              - s3:Getobject
              - s3:DeleteObject
              Resource:  !Sub 'arn:aws:s3:::${BucketName}/*'

  ProcessingLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: !Sub |
            import json
            import boto3
            s3 = boto3.client("s3")
            def lambda_handler(event,context):
              print("hello")
      Handler: index.handler
      Role: !GetAtt ProcessingLambdaExecutionRole.Arn
      Runtime: python2.7
      MemorySize: 512
      Timeout: 120

    '''

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)