9

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. enter image description here

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.

Amir
  • 1,919
  • 8
  • 53
  • 105
  • use nested stacks. use Type: AWS::CloudFormation::Stack as your resource with TemplateURL as a property pointing to the location. SAM helps here https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html – Neil McGuigan Mar 05 '20 at 17:43

2 Answers2

11

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

Amir
  • 1,919
  • 8
  • 53
  • 105
pepo
  • 1,374
  • 11
  • 14
  • @pepe: tqvm for provided solution, now my application can build but it fail in deployment stage. Any idea? 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 – Amir Mar 05 '20 at 19:44
  • Hi @amir, I just edited the the answer to include the Application. Can you try please? – pepo Mar 05 '20 at 23:06
  • Thanks for your help. now deployment is successful but it is not able to execute the 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. my indentation is correct for lambda-function.yml? – Amir Mar 06 '20 at 05:41
  • Hi @amir, I think the problem was in the lambda-functions.yml file. I edited the answer to fix that. Can you try? – pepo Mar 06 '20 at 11:30
  • there was an indentation issue in your code, I fix that but I got a new error during the build. I edited my question with the latest details. now the error is quite funny to me, 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 – Amir Mar 07 '20 at 16:08
  • since I put the lambda-functions.yml in resource folder, do I need to change CodeUri from hello-world/ to CodeUri: ../hello-world/ ? – Amir Mar 07 '20 at 16:11
  • 1
    Hi @amir, I don't think so . – pepo Mar 07 '20 at 16:22
  • Any idea about the error3? deploy build also not building application properly. I mean it just copies template to .aws-sam folder. – Amir Mar 07 '20 at 16:27
  • 1
    Hi @amir, can you post the directory structure? because I think you will need to use the --base-dir flag in this case. – pepo Mar 07 '20 at 16:41
  • I added my folder structure as well. where do I need to add --base-dir? – Amir Mar 07 '20 at 17:04
  • 1
    great, yes... in that case you need to change the CodeUri to `../hello-world` :) – pepo Mar 07 '20 at 17:06
  • now build is successfully by code pipeline but it cannot be deployed. :( Template format error: At least one Resources member must be defined. The following resource(s) failed to create: [yourApplicationAliasName]. . Rollback requested by user. – Amir Mar 07 '20 at 17:14
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209222/discussion-between-pepo-and-amir). – pepo Mar 07 '20 at 20:40
  • I added a sample of the project URL for your reference. https://github.com/day2daychallenge/nest_application.git – Amir Mar 11 '20 at 02:35
  • Hi @amir, did you try using `sam` instead of `aws cloudformation` for build/deploy? – pepo Mar 11 '20 at 12:02
  • No I didn't try sam, do you recommend using any specific command? is this correct link to follow? https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html – Amir Mar 11 '20 at 20:32
1

EDIT: This should work now: https://github.com/aws/aws-sam-cli/issues/1213#issuecomment-789975452

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

arg0nik_
  • 56
  • 4