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

Amazon web services 如何在SAM template.yml中包含多个资源文件,amazon-web-services,aws-lambda,aws-sam,Amazon Web Services,Aws Lambda,Aws Sam,我想将我的云形成yml文件写入另一个文件,并分别加载它们。在无服务器框架中很容易做到这一点,但我不知道如何使用SAM做到这一点。 你介意帮我怎么做吗 我提供了以下项目的副本: my template.yml文件: AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > Sample SAM Template # Create our resources w

我想将我的云形成yml文件写入另一个文件,并分别加载它们。在无服务器框架中很容易做到这一点,但我不知道如何使用SAM做到这一点。 你介意帮我怎么做吗

我提供了以下项目的副本:

my 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)}
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
我的资源文件(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
sam package --template template.yml --output-template-file outputtemplate.yml --s3-bucket your-bucket-name
我的文件夹结构。

编辑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文件:

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)}
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(已解决):

转换AWS::Serverless-2016-10-31失败,错误为:无效的Serverless 应用规范文档。发现的错误数:1。 SAM模板的结构无效。'资源组 所需创建时间

部署中出现错误2(执行变更集)

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

构建部分中的错误3

[容器]2020/03/07 15:24:43运行命令aws cloudformation 包--模板文件template.yml--s3 bucket$bucket --输出模板文件outputtemplate.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
无法上载工件。/resources/lambda-functions.yml被引用 按yourApplicationAliasName资源的位置参数。不能 上载工件hello world/由的CodeUri参数引用 HelloWorldFunction资源。资源的参数CodeUri HelloWorldFunction引用不存在的文件或文件夹 /代码构建/输出/src606023065/src/resources/hello world

错误4: 代码构建现在成功了,在部署过程中我发现了以下错误

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

无法创建以下资源: [您的应用程序名称]。用户请求的回滚


您可以使用
位置
属性()

你的情况应该是

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命令进行打包,如下所示:

  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            Path: /helloworld
            Method: get
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
**不要忘记删除以前的堆栈(如果有)


Thx

@amir很想知道你是否解决了这个问题。就我所见,对于那些搜索这个的人来说,这是行不通的,因为嵌套堆栈似乎还不支持CloudFormation中的转换

提到嵌套堆栈不能使用SAM之类的转换,在中,AWS SAM CLI的一名维护人员指出“关闭,因为我们还不支持SAM build中的嵌套模板或包之外的任何其他命令。我们应该为这种支持创建一个通用问题。”


请对CF路线图竖起大拇指,要求AWS支持这一点。

使用嵌套堆栈。使用Type:AWS::CloudFormation::Stack作为资源,TemplateURL作为指向该位置的属性。SAM在这里帮助@pepe:tqvm获得提供的解决方案,现在我的应用程序可以构建,但在部署阶段失败。有什么想法吗?转换AWS::Serverless-2016-10-31失败,原因是:无服务器应用程序规范文档无效。发现的错误数:1。SAM模板的结构无效。'“资源”部分是必需的Hi@amir,我不这么认为。Hi@amir,你能发布目录结构吗?因为我认为在这种情况下需要使用--base dir标志。太好了,是的。。。在这种情况下,您需要将CodeUri更改为
。/hello world
:)让我们来看看。