how to include multiple resource files in SAM template.yml

断了今生、忘了曾经 提交于 2020-03-21 06:59:36

问题


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.


回答1:


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

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

Thx!.........



来源:https://stackoverflow.com/questions/60551046/how-to-include-multiple-resource-files-in-sam-template-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!