how to include multiple resource files in SAM template.yml

前端 未结 2 1368
灰色年华
灰色年华 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!.........

    0 讨论(0)
  • 2021-01-15 14:36

    @amir would be curious to hear from you as to whether you sorted this out. For those searching for this, as far as I've seen, this won't work because nested stacks don't appear to support transforms (yet) in CloudFormation.

    This issue mentions that nested stacks can't use transforms like SAM, and in this closed GitHub issue, one of the maintainers of the AWS SAM CLI noted "Closing as we don't yet support Nested Templates in SAM build or any other commands except package. We should create a general issue for this support."

    Please give thumbs up to the CF roadmap to ask AWS to support this.

    0 讨论(0)
提交回复
热议问题