Class: CfnPP::Replacer
- Inherits:
-
Object
- Object
- CfnPP::Replacer
- Defined in:
- lib/cfnpp/replacer.rb
Overview
Implements logic for having textual templates that get turned into CloudFormation Fn::Join invocations. This makes userdata scripts and other things much easier to read.
The templates are ERB syntax, with three functions available for inserting CloudFormation references: cfn_raw
, cfn_ref
and cfn_getatt
.
Example Template
#!/bin/bash
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -s <%= cfn_ref("AWS::StackId") %> -r LaunchConfig --region <%= cfn_ref("AWS::Region") %>
/opt/aws/bin/cfn-signal -e $? <%= cfn_ref("WaitHandle") %>
# Setup correct file ownership
chown -R apache:apache /var/www/html/wordpress
Example use of CfnPP::Replacer
template_text = "..."
cfn_hash = CfnReplacer.new.process(template_text)
Instance Method Summary collapse
-
#cfn_cfnpp_ref(s) ⇒ Object
for local refs (instead of cfn_ref).
-
#cfn_getatt(k, v) ⇒ Object
shortcut for inserting a cfn “Fn::GetAtt”; just pass a two element array of the key and value to get.
-
#cfn_raw(h) ⇒ Object
called in the template to include any arbitrary CloudFormation code.
-
#cfn_ref(s) ⇒ Object
shortcut for inserting a cfn “Ref”; just pass the “Ref” value.
- #cfn_render(path) ⇒ Object
- #get_binding ⇒ Object
-
#initialize(text, vars = {}, opts = {}) ⇒ Replacer
constructor
Turns input text into a Hash appropriate for inserting into a CloudFormation structure.
- #process ⇒ Object
- #process_basic ⇒ Object
Constructor Details
#initialize(text, vars = {}, opts = {}) ⇒ Replacer
Turns input text into a Hash appropriate for inserting into a CloudFormation structure.
28 29 30 31 32 |
# File 'lib/cfnpp/replacer.rb', line 28 def initialize(text, vars = {}, opts = {}) @text = text @r_vars = vars @opts = opts end |
Instance Method Details
#cfn_cfnpp_ref(s) ⇒ Object
for local refs (instead of cfn_ref)
58 59 60 |
# File 'lib/cfnpp/replacer.rb', line 58 def cfn_cfnpp_ref(s) return @opts[s] end |
#cfn_getatt(k, v) ⇒ Object
shortcut for inserting a cfn “Fn::GetAtt”; just pass a two element array of the key and value to get
75 76 77 |
# File 'lib/cfnpp/replacer.rb', line 75 def cfn_getatt(k,v) return cfn_raw({ "Fn::GetAtt" => [k, v]}) end |
#cfn_raw(h) ⇒ Object
called in the template to include any arbitrary CloudFormation code.
63 64 65 66 |
# File 'lib/cfnpp/replacer.rb', line 63 def cfn_raw(h) txt = JSON.dump h return "@@@#{txt}@@@" end |
#cfn_ref(s) ⇒ Object
shortcut for inserting a cfn “Ref”; just pass the “Ref” value
69 70 71 |
# File 'lib/cfnpp/replacer.rb', line 69 def cfn_ref(s) return cfn_raw({ "Ref" => s }) end |
#cfn_render(path) ⇒ Object
51 52 53 54 55 |
# File 'lib/cfnpp/replacer.rb', line 51 def cfn_render(path) txt = File.read File.join(File.dirname('.'), path) tmpl = ERB.new txt return tmpl.result(self.get_binding) end |
#get_binding ⇒ Object
79 80 81 |
# File 'lib/cfnpp/replacer.rb', line 79 def get_binding binding end |
#process ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cfnpp/replacer.rb', line 34 def process() tmpl = ERB.new @text joinparts = [] tmpl.result(self.get_binding).split('@@@').each do |chunk| if chunk.match(/^\{/) chunk = JSON.parse(chunk) end joinparts.push(chunk) end return { "Fn::Join" => [ '', joinparts ] } end |
#process_basic ⇒ Object
46 47 48 49 |
# File 'lib/cfnpp/replacer.rb', line 46 def process_basic() tmpl = ERB.new @text return tmpl.result(self.get_binding) end |