首页 > 解决方案 > 无服务器框架 - 创建 Lambda 和 S3 并在 S3 中上传文件。然后,使用 Lambda 提取到 DynamoDB

问题描述

这是我第一次使用 serverless 框架,我的任务是使用 serverless 创建 lambda、s3 和 dynamodb,然后调用 lambda 从 s3 转移到 dynamo。我试图让无服务器生成的名称到我的 S3 以在我的 Lambda 中使用它,但我没有运气。这就是我的 serverless.yml 的样子:

service: fetch-file-and-store-in-s3

frameworkVersion: ">=1.1.0"

custom:
  bucket: 
    Ref: Outputs.AttachmentsBucketName

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
        - s3:PutObjectAcl
      Resource: "arn:aws:s3:::${self:custom.bucket.Ref}/*"

functions:
  save:
    handler: handler.save
    environment:
      BUCKET: ${self:custom.bucket.Ref}

resources:
  # S3
  AttachmentsBucket:
  Type: AWS::S3::Bucket
  Properties:
    # Set the CORS policy
    CorsConfiguration:
      CorsRules:
        -
          AllowedOrigins:
            - '*'
          AllowedHeaders:
            - '*'
          AllowedMethods:
            - GET
            - PUT
            - POST
            - DELETE
            - HEAD
          MaxAge: 3000

  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket

这是它创建 s3 存储桶的部分

Resources:
    # S3
    AttachmentsBucket:
      Type: AWS::S3::Bucket
      Properties:
        # Set the CORS policy
        CorsConfiguration:
          CorsRules:
            - AllowedOrigins:
                - '*'
            - AllowedHeaders:
                - '*'
            - AllowedMethods:
                - GET
                - PUT
                - POST
                - DELETE
                - HEAD
            - MaxAge: 3000
  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket

这是我目前得到的错误:

λ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service fetch-file-and-store-in-s3.zip file to S3 (7.32 MB)...
Serverless: Validating template...

  Error --------------------------------------------------

  Error: The CloudFormation template is invalid: Invalid template property or properties [AttachmentsBucket, Type, Properties]

标签: amazon-s3aws-lambdaserverless-framework

解决方案


你有一些缩进问题:

resources:
  Resources:
    # S3
    AttachmentsBucket:
      Type: AWS::S3::Bucket
      Properties:
        # Set the CORS policy
        CorsConfiguration:
          CorsRules:
            - AllowedOrigins:
                - '*'
            - AllowedHeaders:
                - '*'
            - AllowedMethods:
                - GET
                - PUT
                - POST
                - DELETE
                - HEAD
            - MaxAge: 3000
  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket

推荐阅读