无服务器框架从CloudFormation模板为S3存储桶创建额外的字符

问题描述

所以,我想可能对此有一个直接的答案(希望),但是我试图创建一个AWS Lambda函数来处理csv文件并将处理后的文件放在不同的s3存储桶中(具体取决于正在转换和生成)。

这样做时,我使用的是Serverless Framework和CloudFormation,但要注意的是,在创建存储桶时,它们具有附加的值以及服务名称。例如:

桶: new-process-2-dev-companyprocessedsalestotal08252-jdgsd2ljyqpx

的确意味着: companyprocessedsalestotal08252

Yaml文件位于下面,我使用CloudFormation资源生成了其他存储桶。我该如何解决这种命名对流?

service: new-process-2

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

resources:
  Resources:
    companyincoming08252020:
      Type: 'AWS::S3::Bucket'
      Properties: {}
    companyprocessedsalestotal08252020:
      Type: 'AWS::S3::Bucket'
      Properties: {}
    compnayprocessedwinloss08252020:
      Type: 'AWS::S3::Bucket'
      Properties: {}
    companyemployeestargetotal08252020:
      Type: 'AWS::S3::Bucket'
      Properties: {}
    companyemployeesalespivot08252020:
      Type: 'AWS::S3::Bucket'
      Properties: {}



provider:
  name: aws
  runtime: python3.8
  region: us-east-1
  profile: serverless-admin
  timeout: 120
  memorySize: 128
  iamRoleStatements:
   - Effect: "Allow"
     Action:
       - "s3:*"
     Resource: "*"


functions:
  csv-processor:
    handler: handler.featureengineering
    events:
      - s3:
          bucket: companyincoming08252020
          event: s3:ObjectCreated:*
          rules:
            - suffix: .csv

custom:
  pythonRequirements:
  dockerizePip: true




plugins:
  - serverless-python-requirements
  - serverless-s3-deploy

解决方法

需要为正在创建的存储桶定义name属性。如果省略此选项,则CloudFormation将为存储桶生成一个名称:

如果您未指定名称,则AWS CloudFormation会生成一个唯一ID,并将该ID用于存储桶名称

在CFT中,属性为BucketName,或者如果在无服务器文件中定义,则属性仅为name

此处的CFT信息:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html#cfn-s3-bucket-name

此处无服务器信息:https://www.serverless.com/framework/docs/providers/aws/events/s3#custom-bucket-configuration