bazaarvoice / cloudformation-ruby-dsl

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

Using resource from a module #120

Open gopearls42 opened 7 years ago

gopearls42 commented 7 years ago

This issue probably has more to do with my (lack of) mastery of modules that the tool, but I'm trying to refactor a lot of my code and have written a method in a module (M) to that creates subnets:

m.rb:

require 'bundler/setup'
require 'cloudformation-ruby-dsl/cfntemplate'

module M
  M.create_subnet
    4.times do |s|
      resource s, Type: 'AWS::EC2::Subnet', ...
    end
  end
end

but when I call in from my main file, as in:

require 'm'
template do
   load_from_file('loadme')
end.exec!

loadme.rb:

require 'm'
M::create_subnets(...)

I get the error:

in 'block in create_subnets': undefined method `resource' for M:Module (NoMethodError)

How can I reference a resource outside the template code block (within a loaded file)?

zhelyan commented 7 years ago

perhaps

require 'm'
extend M
M::create_subnets(...)