Amazon s3 无服务器框架-创建Lambda和S3并在S3中上载文件。然后,用Lambda提取至DynamoDB
这是我第一次使用serverless框架,我的任务是使用serverless创建lambda、s3和dynamodb,然后调用lambda从s3传输到dynamo。 我正在尝试将serverless生成的名称添加到我的S3中,以便在Lambda中使用它,但我没有成功。 这就是我的servless.yml的样子:Amazon s3 无服务器框架-创建Lambda和S3并在S3中上载文件。然后,用Lambda提取至DynamoDB,amazon-s3,aws-lambda,serverless-framework,Amazon S3,Aws Lambda,Serverless Framework,这是我第一次使用serverless框架,我的任务是使用serverless创建lambda、s3和dynamodb,然后调用lambda从s3传输到dynamo。 我正在尝试将serverless生成的名称添加到我的S3中,以便在Lambda中使用它,但我没有成功。 这就是我的servless.yml的样子: service: fetch-file-and-store-in-s3 frameworkVersion: ">=1.1.0" custom: bucket: R
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 bucket的部分
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]
缩进有一些问题:
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
缩进对于serverless.yml文件很重要。在这种情况下,
AttachmentsBucket
是一个资源,它应该是Resources
下的一个子部分,有一个选项卡空间,然后Type
和Properties
应该有一个来自资源名称的选项卡空间:AttachmentsBucket
,而在提供的示例中它实际上有两个选项卡空间。CloudFormation将无法处理此特定资源,因为它无法使用正确的名称和属性标识资源
请参阅更新的示例:
Resources:
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
您可以使用aws cli工具验证cloudformation模板
但是您的问题是如何使lambda和dynamodb负载工作,在您的描述中,您询问的是部署部分。你能更新你的问题和标签吗?我找到了解决办法。因为我是一个新手,这是我的第一个项目,所以一开始我对这些术语不是很熟悉。我所做的就是在这里给我的桶命名:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.bucket} # Getting the name of table I defined under custom in serverless.yml
# Make Bucket publicly accessable
MyBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*' # public access to access the bucket files
Action: s3:GetObject
Resource: 'arn:aws:s3:::${self:custom.bucket}/*'
然后,为了上传一个带有deploy的文件,我找到了一个名为serverless-s3bucket-sync的插件
并添加到“自定义”属性和文件夹下的“我的文件”位置:
custom:
bucket: mybucketuniquename #unique global name it will create for the bucket
s3-sync:
- folder: images
bucket: ${self:custom.bucket}
并添加了以下角色:
iamRoleStatements:
#S3 Permissions
- Effect: Allow
Action:
- s3:*
Resource: "arn:aws:s3:::${self:custom.bucket}"
非常感谢。我的缩进实际上是这样的。。我想我在这里贴的时候把它搞砸了。我还是有同样的错误。谢谢!我的缩进是正确的。。很抱歉,是我在这里粘贴的时候把它弄糟了。我得到的错误不是来自这部分。谢谢。我能解决这个问题。我会把它寄出去