Module: Formatron::CloudFormation::Resources::EC2

Defined in:
lib/formatron/cloud_formation/resources/ec2.rb

Overview

Generates CloudFormation template EC2 resources rubocop:disable Metrics/ModuleLength

Constant Summary collapse

BLOCK_DEVICE_MAPPINGS =
:BlockDeviceMappings

Class Method Summary collapse

Class Method Details

.block_device_mapping(device:, size:, type:, iops:) ⇒ Object

rubocop:enable Metrics/MethodLength



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 238

def self.block_device_mapping(device:, size:, type:, iops:)
  mapping = {
    DeviceName: device,
    Ebs: {
      VolumeSize: size
    }
  }
  mapping[:Ebs][:VolumeType] = type unless type.nil?
  mapping[:Ebs][:Iops] = iops unless iops.nil?
  mapping
end

.instance(instance_profile:, availability_zone:, instance_type:, key_name:, administrator_name:, administrator_password:, subnet:, name:, wait_condition_handle:, security_group:, logical_id:, source_dest_check:, os:, ami:) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/AbcSize



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 277

def self.instance(
  instance_profile:,
  availability_zone:,
  instance_type:,
  key_name:,
  administrator_name:,
  administrator_password:,
  subnet:,
  name:,
  wait_condition_handle:,
  security_group:,
  logical_id:,
  source_dest_check:,
  os:,
  ami:
)
  ami = Template.find_in_map(
    Template::REGION_MAP,
    Template.ref('AWS::Region'),
    os
  ) if ami.nil?
  if os.eql? 'windows'
    user_data = Template.base_64(
      Template.join(
        # rubocop:disable Metrics/LineLength
        "<powershell>\n",
        "try\n",
        "{\n",
        Scripts.windows_administrator(
          name: administrator_name,
          password: administrator_password
        ),
        'winrm quickconfig -q', "\n",
        "winrm set winrm/config/winrs '@{MaxMemoryPerShellMB=\"1024\"}'", "\n",
        "winrm set winrm/config '@{MaxTimeoutms=\"1800000\"}'", "\n",
        "winrm set winrm/config/service '@{AllowUnencrypted=\"true\"}'", "\n",
        "winrm set winrm/config/service/auth '@{Basic=\"true\"}'", "\n",
        'netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow', "\n",
        'netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow', "\n",
        'Stop-Service winrm', "\n",
        'Set-Service winrm -startuptype "automatic"', "\n",
        'Start-Service winrm', "\n",
        'cfn-init.exe -v -s ', Template.ref('AWS::StackName'),
        " -r #{logical_id}",
        ' --region ', Template.ref('AWS::Region'), "\n",
        "}\n",
        "catch\n",
        "{\n",
        'cfn-signal.exe -e 1 ',
        Template.base_64(Template.ref(wait_condition_handle)), "\n",
        "}\n",
        '</powershell>'
      # rubocop:enable Metrics/LineLength
      )
    )
  else
    user_data = Template.base_64(
      Template.join(
        # rubocop:disable Metrics/LineLength
        "#!/bin/bash -v\n",
        "apt-get -y update\n",
        "apt-get -y install python-setuptools\n",
        "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
        "export PATH=$PATH:/opt/aws/bin\n",
        'cfn-init --region ', Template.ref('AWS::Region'),
        '    -v -s ', Template.ref('AWS::StackName'), " -r #{logical_id}\n",
        "cfn-signal -e $? -r 'Formatron instance configuration complete' '", Template.ref(wait_condition_handle), "'\n"
      # rubocop:enable Metrics/LineLength
      )
    )
  end
  {
    Type: 'AWS::EC2::Instance',
    Properties: {
      IamInstanceProfile: Template.ref(instance_profile),
      AvailabilityZone: Template.join(
        Template.ref('AWS::Region'),
        availability_zone
      ),
      ImageId: ami,
      SourceDestCheck: source_dest_check,
      InstanceType: instance_type,
      KeyName: key_name,
      SubnetId: Template.ref(subnet),
      SecurityGroupIds: [Template.ref(security_group)],
      Tags: [{
        Key: 'Name',
        Value: name
      }],
      UserData: user_data
    }
  }
end

.internet_gatewayObject



24
25
26
27
28
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 24

def self.internet_gateway
  {
    Type: 'AWS::EC2::InternetGateway'
  }
end

.network_acl(vpc:) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 106

def self.network_acl(vpc:)
  {
    Type: 'AWS::EC2::NetworkAcl',
    Properties: {
      VpcId: Template.ref(vpc)
    }
  }
end

.network_acl_entry(network_acl:, cidr:, egress:, protocol:, action:, icmp_code: nil, icmp_type: nil, start_port: nil, end_port: nil, number:) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists



127
128
129
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
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 127

def self.network_acl_entry(
  network_acl:,
  cidr:,
  egress:,
  protocol:,
  action:,
  icmp_code: nil,
  icmp_type: nil,
  start_port: nil,
  end_port: nil,
  number:
)
  resource = {
    Type: 'AWS::EC2::NetworkAclEntry',
    Properties: {
      NetworkAclId: Template.ref(network_acl),
      CidrBlock: cidr,
      Egress: egress,
      Protocol: protocol,
      RuleAction: action,
      RuleNumber: number
    }
  }
  resource[:Properties][:Icmp] = {
    Code: icmp_code,
    Type: icmp_type
  } unless icmp_code.nil?
  resource[:Properties][:PortRange] = {
    From: start_port,
    To: end_port
  } unless start_port.nil?
  resource
end

.route(route_table:, instance: nil, internet_gateway: nil, vpc_gateway_attachment: nil) ⇒ Object

rubocop:disable Metrics/MethodLength



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 50

def self.route(
  route_table:,
  instance: nil,
  internet_gateway: nil,
  vpc_gateway_attachment: nil
)
  properties = {
    RouteTableId: Template.ref(route_table),
    DestinationCidrBlock: '0.0.0.0/0'
  }
  properties[:GatewayId] =
    Template.ref internet_gateway unless internet_gateway.nil?
  properties[:InstanceId] =
    Template.ref instance unless instance.nil?
  route = {
    Type: 'AWS::EC2::Route',
    Properties: properties
  }
  route[:DependsOn] =
    vpc_gateway_attachment unless vpc_gateway_attachment.nil?
  route
end

.route_table(vpc:) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 40

def self.route_table(vpc:)
  {
    Type: 'AWS::EC2::RouteTable',
    Properties: {
      VpcId: Template.ref(vpc)
    }
  }
end

.security_group(group_description:, vpc:, egress:, ingress:) ⇒ Object

rubocop:disable Metrics/MethodLength



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 164

def self.security_group(
  group_description:,
  vpc:,
  egress:,
  ingress:
)
  {
    Type: 'AWS::EC2::SecurityGroup',
    Properties: {
      GroupDescription: group_description,
      VpcId: Template.ref(vpc),
      SecurityGroupEgress: egress.collect do |rule|
        {
          CidrIp: rule[:cidr],
          IpProtocol: rule[:protocol],
          FromPort: rule[:from_port],
          ToPort: rule[:to_port]
        }
      end,
      SecurityGroupIngress: ingress.collect do |rule|
        {
          CidrIp: rule[:cidr],
          IpProtocol: rule[:protocol],
          FromPort: rule[:from_port],
          ToPort: rule[:to_port]
        }
      end
    }
  }
end

.security_group_egress(security_group:, cidr:, protocol:, from_port:, to_port:) ⇒ Object

rubocop:disable Metrics/MethodLength



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 197

def self.security_group_egress(
  security_group:,
  cidr:,
  protocol:,
  from_port:,
  to_port:
)
  {
    Type: 'AWS::EC2::SecurityGroupEgress',
    Properties: {
      GroupId: Template.ref(security_group),
      CidrIp: cidr,
      IpProtocol: protocol,
      FromPort: from_port,
      ToPort: to_port
    }
  }
end

.security_group_ingress(security_group:, cidr:, protocol:, from_port:, to_port:) ⇒ Object

rubocop:disable Metrics/MethodLength



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 218

def self.security_group_ingress(
  security_group:,
  cidr:,
  protocol:,
  from_port:,
  to_port:
)
  {
    Type: 'AWS::EC2::SecurityGroupIngress',
    Properties: {
      GroupId: Template.ref(security_group),
      CidrIp: cidr,
      IpProtocol: protocol,
      FromPort: from_port,
      ToPort: to_port
    }
  }
end

.subnet(vpc:, cidr:, availability_zone:, map_public_ip_on_launch:) ⇒ Object

rubocop:disable Metrics/MethodLength



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 75

def self.subnet(
  vpc:,
  cidr:,
  availability_zone:,
  map_public_ip_on_launch:
)
  {
    Type: 'AWS::EC2::Subnet',
    Properties: {
      VpcId: Template.ref(vpc),
      CidrBlock: cidr,
      MapPublicIpOnLaunch: map_public_ip_on_launch,
      AvailabilityZone: Template.join(
        Template.ref('AWS::Region'),
        availability_zone
      )
    }
  }
end

.subnet_network_acl_association(subnet:, network_acl:) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 115

def self.subnet_network_acl_association(subnet:, network_acl:)
  {
    Type: 'AWS::EC2::SubnetNetworkAclAssociation',
    Properties: {
      SubnetId: Template.ref(subnet),
      NetworkAclId: Template.ref(network_acl)
    }
  }
end

.subnet_route_table_association(route_table:, subnet:) ⇒ Object

rubocop:enable Metrics/MethodLength



96
97
98
99
100
101
102
103
104
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 96

def self.subnet_route_table_association(route_table:, subnet:)
  {
    Type: 'AWS::EC2::SubnetRouteTableAssociation',
    Properties: {
      RouteTableId: Template.ref(route_table),
      SubnetId: Template.ref(subnet)
    }
  }
end

.volume(size:, type:, iops:, availability_zone:) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 250

def self.volume(size:, type:, iops:, availability_zone:)
  volume = {
    Type: 'AWS::EC2::Volume',
    Properties: {
      AvailabilityZone: availability_zone,
      Size: size
    }
  }
  volume[:Properties][:VolumeType] = type unless type.nil?
  volume[:Properties][:Iops] = iops unless iops.nil?
  volume
end

.volume_attachment(device:, instance:, volume:) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 263

def self.volume_attachment(device:, instance:, volume:)
  {
    Type: 'AWS::EC2::VolumeAttachment',
    Properties: {
      Device: device,
      InstanceId: Template.ref(instance),
      VolumeId: Template.ref(volume)
    }
  }
end

.vpc(cidr:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 12

def self.vpc(cidr:)
  {
    Type: 'AWS::EC2::VPC',
    Properties: {
      CidrBlock: cidr,
      EnableDnsSupport: true,
      EnableDnsHostnames: true,
      InstanceTenancy: 'default'
    }
  }
end

.vpc_gateway_attachment(vpc:, gateway:) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 30

def self.vpc_gateway_attachment(vpc:, gateway:)
  {
    Type: 'AWS::EC2::VPCGatewayAttachment',
    Properties: {
      InternetGatewayId: Template.ref(gateway),
      VpcId: Template.ref(vpc)
    }
  }
end