Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 402
John David Avatar asked Aug 29 '18 21:08

John David


2 Answers

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.

like image 198
Eugene Manuilov Avatar answered Nov 12 '22 22:11

Eugene Manuilov


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.

like image 1
mnwk Avatar answered Nov 12 '22 21:11

mnwk