bazaarvoice / cloudformation-ruby-dsl

Ruby DSL for creating Cloudformation templates
Apache License 2.0
210 stars 76 forks source link

Adding support for Fn::Sub. #107

Closed Noirbot closed 7 years ago

Noirbot commented 7 years ago

Description

Cloudformation has the Fn:Sub function now, which provides functionality similar to Ruby's built-in string interpolation. We should add support for it.

Steps to Test or Reproduce

There are two forms of Fn::Sub, both of which I've added to the DSL, using the same function to produce each, depending on parameters provided.

There's the verbose version where you declare your own variables:

sub("${var1}::AWS::${var2}", {:var1 => 'Yes', :var2 => 'Working'})

This produces this CFN output:

{
  "Fn::Sub": [
    "${var1}::AWS::${var2}",
    {
      "var1": "Yes",
      "var2": "Working"
    }
  ]
}

There's also a shorthand version that you can use when you're only referencing template params or logical ids that only requires passing in the string:

sub("arn:aws:s3:::${ParamName}/*")

That produces this output:

{
  "Fn::Sub": "arn:aws:s3:::${ParamName}/*"
}

Environment

This should work for all modern templates.

Deploy Notes

I'm guessing this will require a release, since it's an added function, but probably not a minor/major version.

Todos

@jonaf/@temujin9

Adding support for both forms of Fn::Sub.

jonaf commented 7 years ago

Thanks @Noirbot ! I have two additional requests for this PR:

  1. Would you mind also adding a simple example to the test template and update the test to validate the output, such that we can prevent any regressions with the syntax in the future?
  2. Would you update the README.md to include the sub function in the list of supported AWS intrinsic functions?
Noirbot commented 7 years ago

@jonaf Updates have been made, should be better now!