bazaarvoice / cloudformation-ruby-dsl

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

Is there any way to set the stack name in the ruby template? #77

Closed z0mbix closed 7 years ago

z0mbix commented 8 years ago

Hi,

Is there any way to set the stack name in the ruby template instead of having to use --stack-name each time?

Thanks

jonaf commented 8 years ago

Hi @z0mbix , there's currently not any way to do that. What's the motivation? Would you like to keep the stack name in source control?

troyready commented 8 years ago

I'm interesting in doing this as well, for exactly the reason you mentioned (alongside chosen parameter values for the stack) -- it'd be nice to have a representation of the deployed infrastructure in revision control.

Would you be open to a pull request along those lines? Thus far I've experimented with a quick hack, and have the ARGV options (e.g. stack name) check for global variables as well, e.g.,

$stack_name = "myfancy_stack"

template do

  value AWSTemplateFormatVersion: '2010-09-09'
  value Description: 'blah'
 ...
end.exec!

I'd obviously be open to doing it more correctly though (variables passed around the methods? moving the standalone methods to a class that could share the variables?).

SaltwaterC commented 8 years ago

Tried the ARGV way for a while, but now I'm trying to make all the stacks available as library calls which makes ARGV a non thread-safe option. So far, I ended up monkey-patching TemplateDSL:

class TemplateDSL
  def name(stack_name)
    @stack_name = stack_name
  end
end

Adding stack_name as attr_accessor (it's already defined as attr_reader) doesn't make instance_eval happy, hence the explicit setter with a different name. The templates now read something like:

template do
  name 'foo'
  value AWSTemplateFormatVersion: '2010-09-09'
  # ...
end
jonaf commented 8 years ago

I like the format of the resulting template from your example, @SaltwaterC . I think I'd rather see if we can swap to an attr_accessor in the actual implementation, but I like where this is headed.

SaltwaterC commented 8 years ago

I went the setter way because attr_* doesn't play nice with instance_eval.

temujin9 commented 8 years ago

@SaltwaterC I like this, and it doesn't feel like it would break anything too hard. Can I encourage you to turn this monkey-patch into a pull request, instead?

temujin9 commented 7 years ago

Turns out this entirely unnecessary: you can just manually set using:

template do
  @stack_name = 'foo'
  [...]
end

and it works fine without any code changes.