John David
John David

Reputation: 131

AWS Cloud Formation; Breaking up template into several files and passing in variables with cfn-include

I'm running into some issues with my aws cloud formation template.

I have a general staging.yaml file where I define all my lambdas and apis. Problem is, that file has gotten WAY too big. So I decided to use this package: https://www.npmjs.com/package/cfn-include. So I can break the file up into several templates. As such:

Fn::Merge:
 - !Include ./templates/api-lambdas/accounts.yaml
 - !Include ./templates/api-lambdas/officers.yaml
 - !Include ./templates/api-lambdas/branches.yaml

My question is, is there any way of passing variables into these includes? I.E:

- !include ./templates/api-lambdas/accounts/yaml, variables: {database: databaseName, environment: staging}

And the accounts.yaml would look like this:

Environment:
 Variables:
  ENV: ${environment}
  DB_NAME: ${databaseName}

Thanks!

Upvotes: 12

Views: 14777

Answers (2)

Eugene Manuilov
Eugene Manuilov

Reputation: 4361

There is one more alternative: cfpack.js tool. It allows you to split your gigantic template into smaller templates that will be combined into one and deployed to your CloudFormation stack.

Upvotes: 4

mnwk
mnwk

Reputation: 21

Maybe you want to give nested stacks a try (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-nested-stacks.html) Usually when files got too big to be readable, they might reach the max template size restriction soon after. There you can pass all the parameters you have in your root file to the substacks pretty easy and in a typed manner.

Upvotes: 2

Related Questions