如何在 SAM 中包含多个资源文件 template.yml
how to include multiple resource files in SAM template.yml
我想把我的云形成yml文件写在不同的文件中,然后分别加载它们。在无服务器框架中很容易做到这一点,但我无法弄清楚如何使用 SAM 来做到这一点。
你介意帮我怎么做吗?
我提供了以下项目的副本:
https://github.com/day2daychallenge/nest_application.git
我的template.yml文件:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates
resources:
Resources:
# Lambda function
- ${file(resources/lambda-functions.yml)}
我的资源文件(lambda-functions.yml)如下:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
我的文件夹结构。
编辑4:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates resources:
Resources:
yourApplicationAliasName:
Type: AWS::Serverless::Application
Properties:
# Lambda function
Location: ./resources/lambda-functions.yml
lambda-functions.yml 内容:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: AWS Lambda function.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
我的buildspec.yml文件:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
pre_build:
commands:
- echo Install source NPM dependencies...
- npm install
build:
commands:
- echo packaging files by using cloudformation...
- export BUCKET=sls-simple
- aws cloudformation package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
finally:
- echo This always runs even if the install command fails
artifacts:
type: zip
files:
- template.yml
- outputtemplate.yml
构建中的错误 1(已解决):
Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless
Application Specification document. Number of errors found: 1.
Structure of the SAM template is invalid. 'Resources' section is
required Created time
部署错误 2(执行变更集)
The following resource(s) failed to create:
[yourApplicationAliasName]. . Rollback requested by user. 2020-03-06
13:37:38 UTC+0800 yourApplicationAliasName CREATE_FAILED Template
format error: At least one Resources member must be defined.
构建部分中的 Error3
[Container] 2020/03/07 15:24:43 Running command aws cloudformation
package --template-file template.yml --s3-bucket $BUCKET
--output-template-file outputtemplate.yml
Unable to upload artifact ./resources/lambda-functions.yml referenced
by Location parameter of yourApplicationAliasName resource. Unable to
upload artifact hello-world/ referenced by CodeUri parameter of
HelloWorldFunction resource. Parameter CodeUri of resource
HelloWorldFunction refers to a file or folder that does not exist
/codebuild/output/src606023065/src/resources/hello-world
错误4:
现在代码构建成功,我在部署过程中遇到以下错误。
Template format error: At least one Resources member must be defined.
The following resource(s) failed to create:
[yourApplicationAliasName]. . Rollback requested by user.
您可以使用 Location
属性 ( https://docs.aws.amazon.com/de_de/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html)
在你的情况下应该是这样的
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates resources:
Resources:
yourApplicationAliasName:
Type: AWS::Serverless::Application
Properties:
# Lambda function
Location: ./resources/lambda-functions.yml
和 lambda-functions.yml 文件
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: AWS Lambda function.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
尝试使用be sam命令打包如下:
sam package --template template.yml --output-template-file outputtemplate.yml --s3-bucket your-bucket-name
然后你需要部署它:
sam deploy --template-file outputtemplate.yml --stack-name your-bucket-name --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
** 如果有,请不要忘记删除之前的堆栈。
谢谢!.........
编辑:现在应该可以工作了:https://github.com/aws/aws-sam-cli/issues/1213#issuecomment-789975452
@amir 很想听听您是否解决了这个问题。对于那些搜索这个的人,据我所知,这是行不通的,因为嵌套堆栈似乎还不支持 CloudFormation 中的转换。
This issue mentions that nested stacks can't use transforms like SAM, and in this closed GitHub issue,AWS SAM CLI 的维护者之一指出“关闭,因为我们还不支持 SAM 构建中的嵌套模板或除包之外的任何其他命令。我们应该为此创建一个一般问题支持。
CF路线图请点赞,希望AWS支持
我想把我的云形成yml文件写在不同的文件中,然后分别加载它们。在无服务器框架中很容易做到这一点,但我无法弄清楚如何使用 SAM 来做到这一点。 你介意帮我怎么做吗?
我提供了以下项目的副本:
https://github.com/day2daychallenge/nest_application.git
我的template.yml文件:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates
resources:
Resources:
# Lambda function
- ${file(resources/lambda-functions.yml)}
我的资源文件(lambda-functions.yml)如下:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
我的文件夹结构。
编辑4:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates resources:
Resources:
yourApplicationAliasName:
Type: AWS::Serverless::Application
Properties:
# Lambda function
Location: ./resources/lambda-functions.yml
lambda-functions.yml 内容:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: AWS Lambda function.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
我的buildspec.yml文件:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
pre_build:
commands:
- echo Install source NPM dependencies...
- npm install
build:
commands:
- echo packaging files by using cloudformation...
- export BUCKET=sls-simple
- aws cloudformation package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
finally:
- echo This always runs even if the install command fails
artifacts:
type: zip
files:
- template.yml
- outputtemplate.yml
构建中的错误 1(已解决):
Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. 'Resources' section is required Created time
部署错误 2(执行变更集)
The following resource(s) failed to create: [yourApplicationAliasName]. . Rollback requested by user. 2020-03-06 13:37:38 UTC+0800 yourApplicationAliasName CREATE_FAILED Template format error: At least one Resources member must be defined.
构建部分中的 Error3
[Container] 2020/03/07 15:24:43 Running command aws cloudformation package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
Unable to upload artifact ./resources/lambda-functions.yml referenced by Location parameter of yourApplicationAliasName resource. Unable to upload artifact hello-world/ referenced by CodeUri parameter of HelloWorldFunction resource. Parameter CodeUri of resource HelloWorldFunction refers to a file or folder that does not exist /codebuild/output/src606023065/src/resources/hello-world
错误4: 现在代码构建成功,我在部署过程中遇到以下错误。
Template format error: At least one Resources member must be defined.
The following resource(s) failed to create: [yourApplicationAliasName]. . Rollback requested by user.
您可以使用 Location
属性 ( https://docs.aws.amazon.com/de_de/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html)
在你的情况下应该是这样的
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template
# Create our resources with separate CloudFormation templates resources:
Resources:
yourApplicationAliasName:
Type: AWS::Serverless::Application
Properties:
# Lambda function
Location: ./resources/lambda-functions.yml
和 lambda-functions.yml 文件
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: AWS Lambda function.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /helloworld
Method: get
尝试使用be sam命令打包如下:
sam package --template template.yml --output-template-file outputtemplate.yml --s3-bucket your-bucket-name
然后你需要部署它:
sam deploy --template-file outputtemplate.yml --stack-name your-bucket-name --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
** 如果有,请不要忘记删除之前的堆栈。
谢谢!.........
编辑:现在应该可以工作了:https://github.com/aws/aws-sam-cli/issues/1213#issuecomment-789975452
@amir 很想听听您是否解决了这个问题。对于那些搜索这个的人,据我所知,这是行不通的,因为嵌套堆栈似乎还不支持 CloudFormation 中的转换。
This issue mentions that nested stacks can't use transforms like SAM, and in this closed GitHub issue,AWS SAM CLI 的维护者之一指出“关闭,因为我们还不支持 SAM 构建中的嵌套模板或除包之外的任何其他命令。我们应该为此创建一个一般问题支持。
CF路线图请点赞,希望AWS支持