Skip to content

Commit

Permalink
and Fn::Stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Sep 13, 2017
1 parent bacb578 commit adf3897
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For example, [`Fn::Include`](#fninclude) provides a convenient way to include fi
* [`Fn::Flatten`](#fnflatten)
* [`Fn::Map`](#fnmap)
* [`Fn::Merge`](#fnmerge)
* [`Fn::Stringify`](#fnstringify)

Tag-based syntax is available in YAML templates. For example,`Fn::Include` becomes `!Include`.

Expand Down Expand Up @@ -377,6 +378,29 @@ For example, this allows you to merge objects of your template that have been cr
}
```

## Fn::Stringify

`Fn::Stringify` will take the passed value and transform it to a JSON string. This is useful for parameters that require a JSON document as a string. Using this function, you can keep writing your configuration in YAML and let the function transform it into a JSON string.

Another useful application is the use of this function in a config file passed as `--cli-input-json` parameter.

```
# myconfig.yml
StackName: MyStack
TemplateBody: !Stringify { Fn::Include: mytemplate.yml }
Parameters:
- ParameterKey: Foo
ParameterValue: Bar
```

You can then simply run the following command to deploy a stack:

```
cfn-include myconfig.yml | jq 'del(.Metadata)' > myconfig.json
aws cloudformation create-stack --cli-input-json file://myconfig.json
```

## More Examples

See [/examples](https://github.com/monken/cfn-include/tree/master/examples) for templates that call an API Gateway endpoint to collect AMI IDs for all regions. There is also a good amount of [tests](https://github.com/monken/cfn-include/tree/master/t) that might be helpful.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ function recurse(base, scope, object) {
return recurse(base, scope, object["Fn::Merge"]).then(function(json) {
return _.merge.apply(_, json);
});
} else if (object["Fn::Stringify"]) {
return recurse(base, scope, object["Fn::Stringify"]).then(function(json) {
return JSON.stringify(json);
});
} else {
return Promise.props(_.mapValues(object, _.bind(recurse, this, base, scope)))
}
Expand Down
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ _ = require('lodash');
var tags = [
{ short: 'Include', full: 'Fn::Include', type: 'scalar' },
{ short: 'Include', full: 'Fn::Include', type: 'mapping' },
{ short: 'Stringify', full: 'Fn::Stringify', type: 'sequence' },
{ short: 'Stringify', full: 'Fn::Stringify', type: 'mapping' },
{ short: 'Map', full: 'Fn::Map', type: 'sequence' },
{ short: 'Flatten', full: 'Fn::Flatten', type: 'sequence' },
{ short: 'Merge', full: 'Fn::Merge', type: 'sequence' },
Expand Down
3 changes: 2 additions & 1 deletion t/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var tests = [
'merge.json',
'errors.js',
'yaml.js',
'stringify.json',
];
if(process.env['TEST_AWS']) tests.push('s3.json', 'api.js');

//var tests = ['yaml.js'];
// var tests = [];

tests.forEach(function(file) {
var tests = require('./tests/' + file);
Expand Down
1 change: 1 addition & 0 deletions t/includes/stringify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TemplateBody: !Stringify { Fn::Include: foobar.json }
33 changes: 33 additions & 0 deletions t/tests/stringify.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"stringify": [{
"name": "basic json",
"template": {
"Fn::Stringify": {
"Fn::Map": [
[1, 2, 3], "_"
]
}
},
"output": "[1,2,3]"
}, {
"name": "array",
"template": {
"Fn::Stringify": [1,2,3]
},
"output": "[1,2,3]"
}, {
"name": "nested includes json",
"template": {
"Fn::Stringify": {
"Fn::Include": "includes/foobar.json"
}
},
"output": "{\"foo\":\"bar\"}"
}, {
"name": "yaml",
"template": {
"Fn::Include": "includes/stringify.yml"
},
"output": { "TemplateBody": "{\"foo\":\"bar\"}" }
}]
}

0 comments on commit adf3897

Please sign in to comment.