我想将我的云格式yml文件写到不同的文件中,并分别加载它们。在无服务器框架中很容易做到这一点,但是我想不出如何用SAM来实现它。你介意帮我怎么做吗?
我提供了以下项目的副本:
我的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 s.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我的文件夹结构。

Edit4:
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.ymllambda-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构建中的Error1 (已解决):
转换AWS:: Serverless -2016-10-31失败:无效的Serverless应用程序规范文档。发现错误数: 1. SAM模板的结构无效。“资源”部分需要创建时间
部署中的Error2 (执行变更集)
以下资源未能创建: yourApplicationAliasName。。用户请求回滚。2020-03-06 13:37:38 UTC+0800 yourApplicationAliasName CREATE_FAILED模板格式错误:必须至少定义一个参考资料成员。
构建部分中的Error3
容器2020/03/07 15:24:43运行命令aws cloudformation包-模板-文件template.yml -S3-桶$BUCKET -输出-模板-文件outputtemplate.yml 无法上载yourApplicationAliasName资源的位置参数引用的工件./resources/lambda-functions.yml。无法上传工件hello-world/由CodeUri参数引用的HelloWorldFunction资源。资源/codebuild/output/src606023065/src/resources/hello-world的参数CodeUri是指不存在HelloWorldFunction的文件或文件夹。
Error4:代码构建现在成功了,我在部署过程中遇到了以下错误。
模板格式错误:必须至少定义一个参考资料成员。 以下资源未能创建: yourApplicationAliasName。。用户请求回滚。
发布于 2020-03-05 17:42:59
您可以使用Location属性( 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 s.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**如果有,请不要忘记移除先前的堆栈。
呃.
发布于 2021-01-06 21:24:19
编辑:现在应该可以了:https://github.com/aws/aws-sam-cli/issues/1213#issuecomment-789975452
埃米尔很想听听你的意见,看看你是否解决了这个问题。对于那些搜索这个的人,据我所见,这是行不通的,因为嵌套堆栈似乎不支持CloudFormation中的转换。
这个问题提到嵌套堆栈不能使用像SAM这样的转换,而在这个封闭的GitHub问题中,AWS的维护人员之一注意到“关闭,因为我们还不支持should或除了包之外的任何其他命令中的嵌套模板。我们应该为这种支持创建一个通用问题。”
请竖起拇指对CF路线图,以要求AWS支持这一点。
https://stackoverflow.com/questions/60551046
复制相似问题