Module: CloudFormationDSL::Helpers

Included in:
Template
Defined in:
lib/cloudformation-dsl/helpers.rb

Instance Method Summary collapse

Instance Method Details

#aws_account_idObject



77
# File 'lib/cloudformation-dsl/helpers.rb', line 77

def () ref("AWS::AccountId") end

#aws_no_valueObject



81
# File 'lib/cloudformation-dsl/helpers.rb', line 81

def aws_no_value() ref("AWS::NoValue") end

#aws_notification_arnsObject



79
# File 'lib/cloudformation-dsl/helpers.rb', line 79

def aws_notification_arns() ref("AWS::NotificationARNs") end

#aws_stack_idObject



83
# File 'lib/cloudformation-dsl/helpers.rb', line 83

def aws_stack_id() ref("AWS::StackId") end

#aws_stack_nameObject



85
# File 'lib/cloudformation-dsl/helpers.rb', line 85

def aws_stack_name() ref("AWS::StackName") end

#base64(value) ⇒ Object

Formation helpers



30
# File 'lib/cloudformation-dsl/helpers.rb', line 30

def base64(value) { :'Fn::Base64' => value } end

#equal(one, two) ⇒ Object



49
# File 'lib/cloudformation-dsl/helpers.rb', line 49

def equal(one, two) { :'Fn::Equals' => [one, two] } end

#file(filename) ⇒ Object

Read the specified file and return its value as a string literal



94
# File 'lib/cloudformation-dsl/helpers.rb', line 94

def file(filename) File.read(File.absolute_path(filename, File.dirname($PROGRAM_NAME))) end

#find_in_map(map, key, name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/cloudformation-dsl/helpers.rb', line 17

def find_in_map(map, key, name)
  # Eagerly evaluate mappings when all keys are known at template expansion time
  if map.is_a?(String) && key.is_a?(String) && name.is_a?(String)
    # We don't know whether the map was built with string keys or symbol keys.  Try both.
    def get(map, key) map[key] || map.fetch(key.to_sym) end
    get(get(@dict.fetch(:Mappings).fetch(map), key), name)
  else
    { :'Fn::FindInMap' => [ map, key, name ] }
  end
end

#fn_and(*condition_list) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/cloudformation-dsl/helpers.rb', line 61

def fn_and(*condition_list)
  case condition_list.length
  when 0..1 then raise "fn_and needs at least 2 items."
  when 2..10 then  { :'Fn::And' => condition_list }
  else raise "fn_and needs a list of 2-10 items that evaluate to true/false."
  end
end

#fn_if(cond, if_true, if_false) ⇒ Object



69
# File 'lib/cloudformation-dsl/helpers.rb', line 69

def fn_if(cond, if_true, if_false) { :'Fn::If' => [cond, if_true, if_false] } end

#fn_not(condition) ⇒ Object



51
# File 'lib/cloudformation-dsl/helpers.rb', line 51

def fn_not(condition) { :'Fn::Not' => [condition] } end

#fn_or(*condition_list) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cloudformation-dsl/helpers.rb', line 53

def fn_or(*condition_list)
  case condition_list.length
  when 0..1 then raise "fn_or needs at least 2 items."
  when 2..10 then  { :'Fn::Or' => condition_list }
  else raise "fn_or needs a list of 2-10 items that evaluate to true/false."
  end
end

#get_att(resource, attribute) ⇒ Object



34
# File 'lib/cloudformation-dsl/helpers.rb', line 34

def get_att(resource, attribute) { :'Fn::GetAtt' => [ resource, attribute ] } end

#get_azs(region = '') ⇒ Object



36
# File 'lib/cloudformation-dsl/helpers.rb', line 36

def get_azs(region = '') { :'Fn::GetAZs' => region } end

#interpolate(string, locals = {}) ⇒ Object

Interpolates a string like “NAME={ref(‘Service’)}” and returns a CloudFormation “Fn::Join” operation to collect the results. Anything between and } is interpreted as a Ruby expression and eval’d. This is especially useful with Ruby “here” documents. Local variables may also be exposed to the string via the locals hash.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cloudformation-dsl/helpers.rb', line 100

def interpolate(string, locals={})
  list = []
  while string.length > 0
    head, match, string = string.partition(/\{\{.*?\}\}/)
    list << head if head.length > 0
    list << eval(match[2..-3], nil, 'interpolated string') if match.length > 0
  end

  # Split out strings in an array by newline, for visibility
  list = list.flat_map {|value| value.is_a?(String) ? value.lines.to_a : value }
  join('', *list)
end

#join(delim, *list) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cloudformation-dsl/helpers.rb', line 38

def join(delim, *list)
  case list.length
  when 0 then ''
  when 1 then list[0]
  else join_list(delim,list)
  end
end

#join_interpolate(delim, string) ⇒ Object



113
114
115
116
# File 'lib/cloudformation-dsl/helpers.rb', line 113

def join_interpolate(delim, string)
  $stderr.puts "join_interpolate(delim,string) has been deprecated; use interpolate(string) instead"
  interpolate(string)
end

#join_list(delim, list) ⇒ Object

Variant of join that matches the native CFN syntax.



47
# File 'lib/cloudformation-dsl/helpers.rb', line 47

def join_list(delim, list) { :'Fn::Join' => [ delim, list ] } end

#load_from_file(filename) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cloudformation-dsl/helpers.rb', line 3

def load_from_file(filename)
  file = File.open(filename)
  # Figure out what the file extension is and process accordingly.
  case File.extname(filename)
  when ".rb"; eval(file.read, nil, filename)
  when ".json"; JSON.load(file)
  when ".yaml"; YAML::load(file)
  else
    raise("Do not recognize extension of #{filename}.")
  end
ensure
  file.close
end

#no_valueObject

deprecated, for backward compatibility



88
89
90
91
# File 'lib/cloudformation-dsl/helpers.rb', line 88

def no_value()
  warn_deprecated('no_value()', 'aws_no_value()')
  aws_no_value()
end

#not_equal(one, two) ⇒ Object



71
# File 'lib/cloudformation-dsl/helpers.rb', line 71

def not_equal(one, two) fn_not(equal(one,two)) end

#ref(name) ⇒ Object



75
# File 'lib/cloudformation-dsl/helpers.rb', line 75

def ref(name) { :Ref => name } end

#select(index, list) ⇒ Object



73
# File 'lib/cloudformation-dsl/helpers.rb', line 73

def select(index, list) { :'Fn::Select' => [ index, list ] } end