Module: AWSDSL::CfnHelpers

Includes:
CfnDsl::Functions
Included in:
CfnBuilder
Defined in:
lib/awsdsl/cfn_helpers.rb

Instance Method Summary collapse

Instance Method Details

#format_block_devices(devices) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/awsdsl/cfn_helpers.rb', line 50

def format_block_devices(devices)
  devices.map do |dev|
    h = { DeviceName: dev[:name] }
    if dev[:ephemeral]
      h[:VirtualName] = "ephemeral#{dev[:ephemeral]}"
    else
      h[:Ebs] = {
        VolumeSize: dev[:size],
        VolumeType: dev[:type] || 'gp2'
      }
    end
    h
  end
end

#format_listener(listener) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/awsdsl/cfn_helpers.rb', line 15

def format_listener(listener)
  listener = listener_defaults(listener)
  hash = {
    LoadBalancerPort: listener[:loadbalancer_port],
    InstancePort: listener[:instance_port],
    Protocol: listener[:proto]
  }
  hash[:SSLCertificateId] = listener[:cert] if listener[:cert]
  hash
end

#format_policy_statement(policy_statement) ⇒ Object



46
47
48
# File 'lib/awsdsl/cfn_helpers.rb', line 46

def format_policy_statement(policy_statement)
  Hash[policy_statement.map { |k, v| [k.to_s.capitalize.to_sym, v] }]
end

#get_vpc_by_name(vpc) ⇒ Object



73
74
75
76
# File 'lib/awsdsl/cfn_helpers.rb', line 73

def get_vpc_by_name(vpc)
  ec2 = AWS::EC2.new
  ec2.vpcs.with_tag('Name', vpc).first
end

#get_zone_for_record(name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/awsdsl/cfn_helpers.rb', line 65

def get_zone_for_record(name)
  r53 = AWS::Route53.new
  zones = r53.hosted_zones.sort_by { |z| z.name.split('.').count }.reverse
  zones.find do |z|
    name.split('.').reverse.take(z.name.split('.').count) == z.name.split('.').reverse
  end
end

#health_check_defaults(health_check) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/awsdsl/cfn_helpers.rb', line 26

def health_check_defaults(health_check)
  hash = {
    Target: health_check[:target]
  }
  hash[:HealthyThreshold] = health_check[:healthy_threshold] || 3
  hash[:UnhealthyThreshold] = health_check[:unhealthy_threshold] || 5
  hash[:Interval] = health_check[:interval] || 90
  hash[:Timeout] = health_check[:timeout] || 60
  hash
end

#listener_defaults(listener) ⇒ Object



8
9
10
11
12
13
# File 'lib/awsdsl/cfn_helpers.rb', line 8

def listener_defaults(listener)
  listener[:proto] ||= 'HTTP'
  listener[:instance_port] ||= listener[:port]
  listener[:loadbalancer_port] ||= listener[:port]
  listener
end

#resolve_security_group(vpc, sg) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/awsdsl/cfn_helpers.rb', line 123

def resolve_security_group(vpc, sg)
  return [sg] if sg.start_with?('sg-')
  ec2 = AWS::EC2.new
  v = ec2.vpcs[vpc] if vpc.start_with?('vpc-')
  v ||= get_vpc_by_name(vpc)
  v.security_groups.with_tag('Name', sg).map(&:id)
end

#resolve_security_groups(vpc, security_groups) ⇒ Object



117
118
119
120
121
# File 'lib/awsdsl/cfn_helpers.rb', line 117

def resolve_security_groups(vpc, security_groups)
  security_groups.map do |sg|
    resolve_security_group(vpc, sg)
  end.flatten
end

#resolve_subnet(vpc, subnet) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/awsdsl/cfn_helpers.rb', line 108

def resolve_subnet(vpc, subnet)
  return [subnet] if subnet.start_with?('subnet-')
  return subnet_refs(vpc, subnet) if subnet_defined?(vpc, subnet)
  ec2 = AWS::EC2.new
  v = ec2.vpcs[vpc] if vpc.start_with?('vpc-')
  v ||= get_vpc_by_name(vpc)
  v.subnets.with_tag('Name', subnet).map(&:id)
end

#resolve_subnets(vpc, subnets) ⇒ Object



84
85
86
87
88
# File 'lib/awsdsl/cfn_helpers.rb', line 84

def resolve_subnets(vpc, subnets)
  subnets.map do |subnet|
    resolve_subnet(vpc, subnet)
  end.flatten(1)
end

#resolve_vpc(vpc) ⇒ Object



78
79
80
81
82
# File 'lib/awsdsl/cfn_helpers.rb', line 78

def resolve_vpc(vpc)
  return vpc if vpc.start_with?('vpc-')
  return Ref("#{vpc.capitalize}VPC") if vpc_defined?(vpc)
  get_vpc_by_name(vpc).id
end

#subnet_defined?(vpc, subnet) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/awsdsl/cfn_helpers.rb', line 100

def subnet_defined?(vpc, subnet)
  @stack.vpcs.find {|v| v.name == vpc }.subnets.map(&:name).include?(subnet)
end

#subnet_refs(vpc, subnet) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/awsdsl/cfn_helpers.rb', line 90

def subnet_refs(vpc, subnet)
  vpc = @stack.vpcs.find {|v| v.name == vpc }
  subnet = vpc.subnets.find {|s| s.name == subnet}
  subnet_name = "#{vpc.name.capitalize}#{subnet.name.capitalize}Subnet"
  azs = subnet.azs || fetch_availability_zones(vpc.region)
  azs.map do |az|
    Ref("#{subnet_name}#{az.capitalize}")
  end
end

#update_policy_defaults(role) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/awsdsl/cfn_helpers.rb', line 37

def update_policy_defaults(role)
  update_policy = role.update_policy || {}
  return nil if update_policy[:disable] == true
  hash = {}
  hash[:MaxBatchSize] = update_policy[:max_batch] || 1
  hash[:MinInstancesInService] = update_policy[:min_inservice] || role.min_size
  hash[:PauseTime] = update_policy[:pause_time] if update_policy[:pause_time]
end

#vpc_defined?(vpc) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/awsdsl/cfn_helpers.rb', line 104

def vpc_defined?(vpc)
  @stack.vpcs.map(&:name).include?(vpc)
end