我使用CloudFormation创建lambda函数。lambda函数存储在一个单独的文件中,然后使用aws cloudformation package
命令重新创建。这很好,堆栈成功部署:
# Filename: auth/auth.yml
# Lambda JS file: auth/lambda-pre-signup.js
Resources:
## Other resources here
MyPreSignupLambda:
Type: AWS::Lambda::Function
Properties:
Architectures:
- arm64
Code: 'lambda-pre-signup.js'
Handler: 'lambda-pre-signup.handler'
Runtime: nodejs16.x
PackageType: Zip
Role: !GetAtt MyRole.Arn
命令:
aws cloudformation package --template-file auth.yml --s3-bucket my-bucket --output-template-file generated-auth.yml
aws cloudformation deploy --template-file generated-auth.yml --stack-name test-stack --capabilities CAPABILITY_IAM
然而,当我创建根堆栈模板并引用lambda时,我会得到一个错误:
资源处理程序返回消息:"无法解压缩上载的文件。请检查您的文件,然后尝试再次上传。(服务:Lambda,状态代码:400,请求ID:xxxxx(";
当我检查上传文件的S3桶时,源代码在那里,但它没有被压缩(我可以下载并直接查看代码,而无需解压缩(。
这是我当前的根堆栈CF模板:
# Filename: root.yml
Resources:
MyAuth:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: ./auth/auth.yml
命令:
aws cloudformation package --template-file root.yml --s3-bucket my-bucket --output-template-file generated-root.yml
aws cloudformation deploy --template-file generated-root.yml --stack-name test-root-stack --capabilities CAPABILITY_IAM
package
命令中是否有一些选项可以确保上传的lambda代码被压缩?
编辑:写了一个错误的论点
AWS CLI官方文档明确指出:
如果指定了一个文件,命令会直接将其上传到S3存储桶。如果指定了一个文件夹,该命令会压缩文件夹,然后上传.zip文件。
为了确保您的文件被压缩,您可以创建一个新目录(例如code
(并将您的lambda-pre-signup.js
文件放在那里:
mkdir code
mv lambda-pre-signup.js code/
然后,您更正的CloudFormation模板可能看起来像:
# Filename: auth/auth.yml
# Lambda JS file: auth/code/lambda-pre-signup.js
Resources:
# Other resources here
MyPreSignupLambda:
Type: AWS::Lambda::Function
Properties:
Architectures:
- arm64
Code: code/
Handler: lambda-pre-signup.handler
Runtime: nodejs16.x
Role: !GetAtt MyRole.Arn
旁注:
- 我使用
code/
来明确指示目录,code
也应该工作 - 在CCD_ 7和CCD_
- 可以省略CCD_ 9