how to include multiple resource files in SAM template.yml

前端 未结 2 1367
灰色年华
灰色年华 2021-01-15 13:42

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 t

2条回答
  •  隐瞒了意图╮
    2021-01-15 14:28

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

提交回复
热议问题