Module: Formatron::CloudFormation::Resources::EC2
- Defined in:
- lib/formatron/cloud_formation/resources/ec2.rb
Overview
Generates CloudFormation template EC2 resources rubocop:disable Metrics/ModuleLength
Class Method Summary collapse
-
.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.
- .vpc(cidr:) ⇒ Object
- .vpc_gateway_attachment(vpc:, gateway:) ⇒ Object
Class Method Details
.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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 238 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
21 22 23 24 25 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 21 def self.internet_gateway { Type: 'AWS::EC2::InternetGateway' } end |
.network_acl(vpc:) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 103 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
124 125 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 124 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 47 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
37 38 39 40 41 42 43 44 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 37 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
161 162 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 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 161 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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 194 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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 215 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 72 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
112 113 114 115 116 117 118 119 120 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 112 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 93 def self.subnet_route_table_association(route_table:, subnet:) { Type: 'AWS::EC2::SubnetRouteTableAssociation', Properties: { RouteTableId: Template.ref(route_table), SubnetId: Template.ref(subnet) } } end |
.vpc(cidr:) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 9 def self.vpc(cidr:) { Type: 'AWS::EC2::VPC', Properties: { CidrBlock: cidr, EnableDnsSupport: true, EnableDnsHostnames: true, InstanceTenancy: 'default' } } end |
.vpc_gateway_attachment(vpc:, gateway:) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/formatron/cloud_formation/resources/ec2.rb', line 27 def self.(vpc:, gateway:) { Type: 'AWS::EC2::VPCGatewayAttachment', Properties: { InternetGatewayId: Template.ref(gateway), VpcId: Template.ref(vpc) } } end |