如何在SAM template.yml中包含多个资源文件 [英] how to include multiple resource files in SAM template.yml

查看:220
本文介绍了如何在SAM template.yml中包含多个资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将云形成yml文件写入另一个文件中,然后分别加载它们。在无服务器框架中很容易做到这一点,但是我无法弄清楚如何使用SAM来实现。
您能帮我怎么做吗?



我在下面提供了该项目的副本:





Edit4:

  AWSTemplateFormatVersion:'2010-09-09'
转换:AWS :: Serverless-2016-10-31
说明:>
示例SAM模板

#使用单独的CloudFormation模板资源创建资源:
资源:
yourApplicationAliasName:
类型:AWS :: Serverless :: Application
属性:
#Lambda函数
位置:./resources/lambda-functions.yml

lambda-functions.yml内容:

  AWSTemplateFormatVersion:'2010-09-09'
转换:'AWS :: Serverless-2016-10-31'
说明:AWS Lambda函数。
资源:
HelloWorldFunction:
类型:AWS :: Serverless :: Function
属性:
CodeUri:../hello-world/
处理程序: app.lambdaHandler
运行时:nodejs12.x
事件:
HelloWorld:
类型:Api
属性:
路径:/ helloworld
方法:获取

我的buildspec.yml文件:

 版本:0.2 
阶段:
安装:
运行时版本:
nodejs:12
pre_build:
命令:
-echo安装源NPM依赖项...
-npm安装
构建:
命令:
-通过使用cloudformation回显打包文件...
-导出BUCKET = sls-simple
-aws cloudformation软件包--template-file template.yml --s3-bucket $ BUCKET --output-template-file outputtemplate.yml
最后:
-echo即使安装程序总是运行mmand使
工件失败:
类型:zip
文件:
-template.yml
-outputtemplate.yml

构建中的错误1(已解决):


转换AWS :: Serverless-2016-10-31失败,原因是:无效的Serverless
应用程序规范文档。发现的错误数量:1.
SAM模板的结构无效。 资源部分是
必需的创建时间


部署中的错误2(执行更改集)


以下资源创建失败:
[yourApplicationAliasName]。 。用户请求的回滚。 2020-03-06
13:37:38 UTC + 0800 yourApplicationAliasName CREATE_FAILED模板
格式错误:必须定义至少一个资源成员。


构建部分中的错误3


[容器] 2020/03/07 15:24:43运行命令aws cloudformation
软件包--template-file template.yml --s3-bucket $ BUCKET
--output-template-file outputtemplate.yml



无法上载由yourApplicationAliasName资源的Location参数引用的
的工件./resources/lambda-functions.yml。
HelloWorldFunction资源的CodeUri参数引用的
上传工件hello-world /无法使用。资源
HelloWorldFunction的参数CodeUri指向不存在的文件或文件夹
/ codebuild / output / src606023065 / src / resources / hello-world


错误4:
代码构建现在已成功,并且在部署过程中遇到错误。


模板格式错误:必须至少定义一个Resources成员。



以下资源创建失败:
[yourApplicationAliasName]。 。用户请求的回滚。



解决方案

您可以使用 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'
转换:AWS :: Serverless-2016-10-31
说明:>
示例SAM模板

#使用单独的CloudFormation模板资源创建资源:
资源:
yourApplicationAliasName:
类型:AWS :: Serverless :: Application
属性:
#Lambda函数
位置:./resources/lambda-functions.yml

和lambda-functions.yml文件

  AWSTemplateFormatVersion:'2010-09-09'
转换:'AWS :: Serverless-2016-10-31'
说明:AWS Lambda函数。
资源:
HelloWorldFunction:
类型:AWS :: Serverless :: Function
属性:
CodeUri:hello-world /
处理程序:app.lambdaHandler
运行时:nodejs12.x
事件:
HelloWorld:
类型:Api
属性:
路径:/ helloworld
方法:获取

尝试使用be sam命令进行如下包装:

  sam包--template template.yml --output-template-file outputtemplate.yml --s3-bucket您的存储桶名称

然后您需要部署它:

  sam deploy --template-file outputtemplate.yml --stack-name your-bucket-name --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND 

**不要忘记删除以前的堆栈。



Thx!.........


I want to write my cloud formation yml file in a different file and load them separately. It is easy to do it in the serverless framework but I couldn't figure it out how to do it with SAM. Would you mind help me how to do it?

I provided a copy of the project below:

https://github.com/day2daychallenge/nest_application.git

my template.yml file:

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)}

My resource file(lambda-functions.yml) is as below:

  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            Path: /helloworld
            Method: get

my folder structure.

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.yml

lambda-functions.yml content:

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

my buildspec.yml file:

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 in build(solved):

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

Error2 in deployment( execute changeset)

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 in the build section

[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

Error4: Code build is successful now, and I get below error during deployment.

Template format error: At least one Resources member must be defined.

The following resource(s) failed to create: [yourApplicationAliasName]. . Rollback requested by user.

解决方案

you can use the Location property ( https://docs.aws.amazon.com/de_de/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html)

In your case should be something like

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

and the lambda-functions.yml file

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

try to use be sam command for packaging as below:

sam package --template template.yml --output-template-file outputtemplate.yml --s3-bucket your-bucket-name

then you need to deploy it:

sam deploy --template-file outputtemplate.yml --stack-name your-bucket-name --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND

** do not forget to remove your previous stack if there is any.

Thx!.........

这篇关于如何在SAM template.yml中包含多个资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆