Module: Enscalator::Core::CfResources

Included in:
RichTemplateDSL
Defined in:
lib/enscalator/core/cf_resources.rb

Overview

Resources for cloudformation template dsl

Instance Method Summary collapse

Instance Method Details

#iam_instance_profile_with_full_access(role_name, *services) ⇒ String

IAM instance profile with full access policies to passed services

Parameters:

  • role_name (String)

    iam role name

  • services (Array<String>)

    a list of aws service name

Returns:

  • (String)

    iam instance profile name



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/enscalator/core/cf_resources.rb', line 130

def iam_instance_profile_with_full_access(role_name, *services)
  resource "#{role_name}Role",
           Type: 'AWS::IAM::Role',
           Properties: {
             AssumeRolePolicyDocument: {
               Statement: [
                 {
                   Effect: 'Allow',
                   Principal: {
                     Service: ['ec2.amazonaws.com']
                   },
                   Action: ['sts:AssumeRole']
                 }
               ]
             },
             Path: '/',
             Policies: [
               {
                 PolicyName: "#{role_name}Policy",
                 PolicyDocument: {
                   Statement: services.map do |s|
                     {
                       Effect: 'Allow',
                       Action: "#{s}:*",
                       Resource: '*'
                     }
                   end
                 }
               }
             ]
           }

  resource "#{role_name}InstanceProfile",
           Type: 'AWS::IAM::InstanceProfile',
           Properties: {
             Path: '/',
             Roles: [ref("#{role_name}Role")]
           }

  ref("#{role_name}InstanceProfile")
end

#instance_vpc(name, image_id, subnet, security_groups, depends_on: [], properties: {}) ⇒ Object

Create ec2 instance in given vpc

Parameters:

  • name (String)

    instance name

  • image_id (String)

    instance ami_id

  • subnet (String)

    instance subnet id

  • security_groups (String)

    instance security_groups (string of Security Groups IDs)

  • depends_on (Array) (defaults to: [])

    resources necessary to be create prior to this instance

  • properties (Hash) (defaults to: {})

    other properties



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/enscalator/core/cf_resources.rb', line 180

def instance_vpc(name, image_id, subnet, security_groups, depends_on: [], properties: {})
  fail "VPC instance #{name} can not contain non VPC SecurityGroups" if properties.include?(:SecurityGroups)
  if properties.include?(:NetworkInterfaces)
    fail "VPC instance #{name} can not contain NetworkInterfaces and subnet or security_groups"
  end
  properties[:ImageId] = image_id
  properties[:SubnetId] = subnet
  properties[:SecurityGroupIds] = security_groups
  if properties[:Tags] && !properties[:Tags].any? { |x| x[:Key] == 'Name' }
    properties[:Tags] << { Key: 'Name', Value: join('-', aws_stack_name, name) }
  end
  options = {
    Type: 'AWS::EC2::Instance',
    Properties: properties
  }

  options[:DependsOn] = depends_on unless depends_on.empty?
  resource name, options
  name
end

#instance_with_network(name, image_id, network_interfaces, properties: {}) ⇒ Object

Create ec2 instance with attached to it network interface

Parameters:

  • name (String)

    instance name

  • image_id (String)

    instance ami_id

  • network_interfaces (String)

    network interfaces

  • properties (Hash) (defaults to: {})

    other properties



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/enscalator/core/cf_resources.rb', line 207

def instance_with_network(name, image_id, network_interfaces, properties: {})
  if ([:SubnetId, :SecurityGroups, :SecurityGroupIds] & properties).any?
    fail "Instance with NetworkInterfaces #{name} can not contain instance subnet or security_groups"
  end
  properties[:ImageId] = image_id
  properties[:NetworkInterfaces] = network_interfaces
  if properties[:Tags] && !properties[:Tags].any? { |x| x[:Key] == 'Name' }
    properties[:Tags] << { Key: 'Name', Value: join('-', aws_stack_name, name) }
  end
  options = {
    Type: 'AWS::EC2::Instance',
    Properties: properties
  }
  resource name, options
  name
end

#security_group(name, description, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object

Security group

Parameters:

  • name (String)

    of the security group

  • description (String)

    of security group

  • security_group_egress (Array) (defaults to: [])

    list of outbound rules

  • security_group_ingress (Array) (defaults to: [])

    list of inbound rules

  • depends_on (Array) (defaults to: [])

    list of resources this vpc needs

  • tags (Hash) (defaults to: {})

    tags



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/enscalator/core/cf_resources.rb', line 67

def security_group(name,
                   description,
                   security_group_egress: [],
                   security_group_ingress: [],
                   depends_on: [],
                   tags: {})
  properties = {
    GroupDescription: description
  }
  properties[:SecurityGroupEgress] = security_group_egress unless security_group_egress.empty?
  properties[:SecurityGroupIngress] = security_group_ingress unless security_group_ingress.empty?
  unless tags.include?('Name')
    tags['Name'] = join('-', aws_stack_name, name)
  end
  properties[:Tags] = tags_to_properties(tags)
  options = {
    Type: 'AWS::EC2::SecurityGroup',
    Properties: properties
  }
  options[:DependsOn] = depends_on unless depends_on.empty?
  resource name, options
  name
end

#security_group_vpc(name, description, vpc, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object

VPC Security group

Parameters:

  • name (String)

    of the security group

  • description (String)

    of security group

  • security_group_egress (Array) (defaults to: [])

    list of outbound rules

  • security_group_ingress (Array) (defaults to: [])

    list of inbound rules

  • depends_on (Array) (defaults to: [])

    list of resources this vpc needs

  • tags (Hash) (defaults to: {})

    tags



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/enscalator/core/cf_resources.rb', line 99

def security_group_vpc(name,
                       description,
                       vpc,
                       security_group_egress: [],
                       security_group_ingress: [],
                       depends_on: [],
                       tags: {})
  properties = {
    VpcId: vpc,
    GroupDescription: description
  }
  properties[:SecurityGroupEgress] = security_group_egress unless security_group_egress.empty?
  properties[:SecurityGroupIngress] = security_group_ingress unless security_group_ingress.empty?
  unless tags.include?('Name')
    tags['Name'] = join('-', aws_stack_name, name)
  end
  properties[:Tags] = tags_to_properties(tags)
  options = {
    Type: 'AWS::EC2::SecurityGroup',
    Properties: properties
  }
  options[:DependsOn] = depends_on unless depends_on.empty?
  resource name, options
  name
end

#subnet(name, vpc, cidr, availability_zone: '', depends_on: [], tags: {}) ⇒ Object

Subnet resource

Parameters:

  • name (String)

    of the vpc name

  • cidr (String)

    ip address block in CIDR notation (Classless Inter-Domain Routing)

  • availability_zone (String) (defaults to: '')

    where subnet gets created

  • depends_on (Array) (defaults to: [])

    list of resources this vpc needs

  • tags (Hash) (defaults to: {})

    tags



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/enscalator/core/cf_resources.rb', line 39

def subnet(name, vpc, cidr, availability_zone: '', depends_on: [], tags: {})
  properties = {
    VpcId: vpc,
    CidrBlock: cidr
  }
  properties[:AvailabilityZone] = availability_zone unless availability_zone.empty?
  unless tags.include?('Name')
    tags['Name'] = join('-', aws_stack_name, name)
  end
  properties[:Tags] = tags_to_properties(tags)

  options = {
    Type: 'AWS::EC2::Subnet',
    Properties: properties
  }
  options[:DependsOn] = depends_on unless depends_on.empty?
  resource name, options
  name
end

#vpc(name, cidr, enable_dns_support: nil, enable_dns_hostnames: nil, depends_on: [], tags: {}) ⇒ Object

VPC resource

Parameters:

  • name (String)

    of the vpc name

  • cidr (String)

    ip address block in CIDR notation (Classless Inter-Domain Routing)

  • enable_dns_support (String) (defaults to: nil)

    enable dns support

  • enable_dns_hostnames (String) (defaults to: nil)

    enable dns hostname

  • depends_on (Array) (defaults to: [])

    list of resources this vpc needs

  • tags (Hash) (defaults to: {})

    tags



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/enscalator/core/cf_resources.rb', line 13

def vpc(name, cidr, enable_dns_support: nil, enable_dns_hostnames: nil, depends_on: [], tags: {})
  properties = {
    CidrBlock: cidr
  }
  properties[:EnableDnsSupport] = enable_dns_support unless enable_dns_support.nil?
  properties[:EnableDnsHostnames] = enable_dns_hostnames unless enable_dns_hostnames.nil?
  unless tags.include?('Name')
    tags['Name'] = join('-', aws_stack_name, name)
  end
  properties[:Tags] = tags_to_properties(tags)
  options = {
    Type: 'AWS::EC2::VPC',
    Properties: properties
  }
  options[:DependsOn] = depends_on unless depends_on.empty?
  resource name, options
  name
end