gpt4 book ai didi

amazon-web-services - 如何在 SAM template.yml 中包含多个资源文件

转载 作者:行者123 更新时间:2023-12-04 02:38:26 33 4
gpt4 key购买 nike

我想在不同的文件中编写我的云形成 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

我的文件夹结构。
enter image description here

编辑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属性(property) ( https://docs.aws.amazon.com/de_de/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html )

在你的情况下应该是这样的

模板.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

** 如果有的话,不要忘记删除以前的堆栈。

谢谢!.........

关于amazon-web-services - 如何在 SAM template.yml 中包含多个资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60551046/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com