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
-
.block_device_mapping(device:, size:, type:, iops:) ⇒ Object
rubocop:enable Metrics/MethodLength.
-
.instance(scripts: nil, script_variables: nil, files: nil, instance_profile:, availability_zone:, instance_type:, key_name:, subnet:, name:, wait_condition_handle:, security_group:, logical_id:, source_dest_check:) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/AbcSize.
- .internet_gateway ⇒ Object
- .network_acl(vpc:) ⇒ Object
-
.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.
-
.route(route_table:, instance: nil, internet_gateway: nil, vpc_gateway_attachment: nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .route_table(vpc:) ⇒ Object
-
.security_group(group_description:, vpc:, egress:, ingress:) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.security_group_egress(security_group:, cidr:, protocol:, from_port:, to_port:) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.security_group_ingress(security_group:, cidr:, protocol:, from_port:, to_port:) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.subnet(vpc:, cidr:, availability_zone:, map_public_ip_on_launch:) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .subnet_network_acl_association(subnet:, network_acl:) ⇒ Object
-
.subnet_route_table_association(route_table:, subnet:) ⇒ Object
rubocop:enable Metrics/MethodLength.
- .volume(size:, type:, iops:, availability_zone:) ⇒ Object
- .volume_attachment(device:, instance:, volume:) ⇒ Object
- .vpc(cidr:) ⇒ Object
- .vpc_gateway_attachment(vpc:, gateway:) ⇒ Object
Class Method Details
.block_device_mapping(device:, size:, type:, iops:) ⇒ Object
rubocop:enable Metrics/MethodLength
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 237 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(scripts: nil, script_variables: nil, files: nil, instance_profile:, availability_zone:, instance_type:, key_name:, subnet:, name:, wait_condition_handle:, security_group:, logical_id:, source_dest_check:) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/AbcSize
276 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 276 def self.instance( scripts: nil, script_variables: nil, files: nil, instance_profile:, availability_zone:, instance_type:, key_name:, subnet:, name:, wait_condition_handle:, security_group:, logical_id:, source_dest_check: ) files ||= {} scripts.each_index do |index| files["/tmp/formatron/script-#{index}.sh"] = { content: scripts[index], mode: '000755', owner: 'root', group: 'root' } end unless scripts.nil? script_variables_content = script_variables.reduce([]) do |content, (key, value)| content.concat(["#{key}=", value, "\n"]) end unless script_variables.nil? files['/tmp/formatron/script-variables'] = { content: Template.join(*script_variables_content), mode: '000644', owner: 'root', group: 'root' } unless script_variables_content.nil? { Type: 'AWS::EC2::Instance', Metadata: { Comment1: 'Create setup scripts', 'AWS::CloudFormation::Init' => { config: { files: files } } }, Properties: { IamInstanceProfile: Template.ref(instance_profile), AvailabilityZone: Template.join( Template.ref('AWS::Region'), availability_zone ), ImageId: Template.find_in_map( Template::REGION_MAP, Template.ref('AWS::Region'), '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: Template.base_64( Template.join( # rubocop:disable Metrics/LineLength "#!/bin/bash -v\n", "function error_exit\n", "{\n", " cfn-signal -e 1 -r \"$1\" '", Template.ref(wait_condition_handle), "'\n", " exit 1\n", "}\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} ", " || error_exit 'Failed to run cfn-init'\n", "for file in /tmp/formatron/script-*.sh; do\n", " $file || error_exit \"failed to run Formatron setup script: $file\"\n", "done\n", "# If all went well, signal success\n", "cfn-signal -e $? -r 'Formatron instance configuration complete' '", Template.ref(wait_condition_handle), "'\n" # rubocop:enable Metrics/LineLength ) ) } } end |
.internet_gateway ⇒ Object
23 24 25 26 27 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 23 def self.internet_gateway { Type: 'AWS::EC2::InternetGateway' } end |
.network_acl(vpc:) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 105 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
126 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 126 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 49 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] = unless .nil? route end |
.route_table(vpc:) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 39 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
163 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 163 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 196 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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 217 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 74 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
114 115 116 117 118 119 120 121 122 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 114 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
95 96 97 98 99 100 101 102 103 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 95 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
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 249 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
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 262 def self.(device:, instance:, volume:) { Type: 'AWS::EC2::VolumeAttachment', Properties: { Device: device, InstanceId: Template.ref(instance), VolumeId: Template.ref(volume) } } end |
.vpc(cidr:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 11 def self.vpc(cidr:) { Type: 'AWS::EC2::VPC', Properties: { CidrBlock: cidr, EnableDnsSupport: true, EnableDnsHostnames: true, InstanceTenancy: 'default' } } end |