Class: Enscalator::Templates::VPCWithNATInstance
- Inherits:
-
RichTemplateDSL
- Object
- TemplateDSL
- RichTemplateDSL
- Enscalator::Templates::VPCWithNATInstance
- Defined in:
- lib/enscalator/vpc_with_nat_instance.rb
Overview
Amazon AWS Virtual Private Cloud template with NAT instance
Constant Summary collapse
- SUBNET_CIDR_BLOCK_SIZE =
Subnet size (256 addresses)
24
Constants inherited from RichTemplateDSL
RichTemplateDSL::TEMPLATE_BODY_LIMIT
Constants included from Plugins::Route53
Plugins::Route53::HEALTH_CHECK_TYPE, Plugins::Route53::RECORD_TYPE
Instance Method Summary collapse
-
#tpl ⇒ Object
Template method.
Methods inherited from RichTemplateDSL
#availability_zones, #creating?, #deploy, #deployment_env, #description, #enqueue, #exec!, #handle_trailing_dot, #has_multiple_envs, #hosted_zone, #initialize, #network_interface, #parameter, #parse_params, #post_run, #pre_run, #private_hosted_zone, #public_hosted_zone, #read_availability_zones, #region, #resource, #tags_to_properties, #vpc_stack_name
Methods included from Plugins::Route53
#create_healthcheck, #create_multiple_dns_records, #create_private_hosted_zone, #create_public_hosted_zone, #create_single_dns_record
Methods included from Helpers
#cfn_call_script, #create_ssh_key, #find_ami, #flatten_hash, #gen_ssh_key_name, #init_assets_dir, #init_aws_config, #read_user_data, #run_cmd
Methods included from Helpers::Dns
#get_dns_records, #upsert_dns_record
Methods included from Helpers::Stack
#cfn_create_stack, #generate_parameters, #get_resource, #get_resources, #wait_stack
Methods included from Helpers::Wrappers
#cfn_client, #cfn_resource, #ec2_client, #route53_client
Methods included from Core::CfResources
#iam_instance_profile_with_full_access, #instance_vpc, #instance_with_network, #security_group, #security_group_vpc, #subnet, #vpc
Methods included from Core::CfParameters
#parameter_allocated_storage, #parameter_ami, #parameter_ec2_instance_type, #parameter_instance_type, #parameter_key_name, #parameter_name, #parameter_password, #parameter_rds_instance_type, #parameter_username
Constructor Details
This class inherits a constructor from Enscalator::RichTemplateDSL
Instance Method Details
#tpl ⇒ Object
Template method
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 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 157 158 159 160 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/enscalator/vpc_with_nat_instance.rb', line 9 def tpl description = <<-EOS.gsub(/^\s+\|/, '') |AWS CloudFormation template for the VPC environment. |For each availability zone stack creates: the public subnet, internet gateway, NAT EC2 instance |internet access from private subnets, routing configuration for corresponding subnets |and security groups. EOS value Description: description nat_key_name = gen_ssh_key_name('vpc-nat', region, stack_name) pre_run { create_ssh_key nat_key_name, region, force_create: false } parameter_ec2_instance_type 'NAT', type: 'c4.large' mapping 'AWSNATAMI', 'us-east-1': { AMI: 'ami-68115b02' }, 'us-west-1': { AMI: 'ami-ef1a718f' }, 'us-west-2': { AMI: 'ami-77a4b816' }, 'eu-west-1': { AMI: 'ami-c0993ab3' }, 'eu-central-1': { AMI: 'ami-0b322e67' }, 'ap-northeast-1': { AMI: 'ami-f885ae96' }, 'ap-southeast-1': { AMI: 'ami-e2fc3f81' }, 'ap-southeast-2': { AMI: 'ami-e3217a80' }, 'sa-east-1': { AMI: 'ami-8631b5ea' } mapping 'AWSRegionNetConfig', Core::NetworkConfig.mapping_vpc_net resource 'VPC', Type: 'AWS::EC2::VPC', Properties: { CidrBlock: find_in_map('AWSRegionNetConfig', ref('AWS::Region'), 'VPC'), EnableDnsSupport: 'true', EnableDnsHostnames: 'true', Tags: [ { Key: 'Name', Value: aws_stack_name }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Public' } ] } resource 'InternetGateway', Type: 'AWS::EC2::InternetGateway', Properties: { Tags: [ { Key: 'Name', Value: 'Public Gateway' }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Public' } ] } resource 'GatewayToInternet', DependsOn: %w( VPC InternetGateway ), Type: 'AWS::EC2::VPCGatewayAttachment', Properties: { VpcId: ref('VPC'), InternetGatewayId: ref('InternetGateway') } resource 'PublicRouteTable', DependsOn: ['VPC'], Type: 'AWS::EC2::RouteTable', Properties: { VpcId: ref('VPC'), Tags: [ { Key: 'Name', Value: 'Public' }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Public' } ] } resource 'PublicRoute', DependsOn: %w( PublicRouteTable InternetGateway ), Type: 'AWS::EC2::Route', Properties: { RouteTableId: ref('PublicRouteTable'), DestinationCidrBlock: '0.0.0.0/0', GatewayId: ref('InternetGateway') } resource 'PublicNetworkAcl', DependsOn: ['VPC'], Type: 'AWS::EC2::NetworkAcl', Properties: { VpcId: ref('VPC'), Tags: [ { Key: 'Name', Value: 'Public' }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Public' } ] } resource 'InboundHTTPPublicNetworkAclEntry', DependsOn: ['PublicNetworkAcl'], Type: 'AWS::EC2::NetworkAclEntry', Properties: { NetworkAclId: ref('PublicNetworkAcl'), RuleNumber: '100', Protocol: '-1', RuleAction: 'allow', Egress: 'false', CidrBlock: '0.0.0.0/0', PortRange: { From: '0', To: '65535' } } resource 'OutboundHTTPPublicNetworkAclEntry', DependsOn: ['PublicNetworkAcl'], Type: 'AWS::EC2::NetworkAclEntry', Properties: { NetworkAclId: ref('PublicNetworkAcl'), RuleNumber: '100', Protocol: '-1', RuleAction: 'allow', Egress: 'true', CidrBlock: '0.0.0.0/0', PortRange: { From: '0', To: '65535' } } resource 'PrivateNetworkAcl', DependsOn: ['VPC'], Type: 'AWS::EC2::NetworkAcl', Properties: { VpcId: ref('VPC'), Tags: [ { Key: 'Name', Value: 'Private' }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Private' } ] } resource 'InboundPrivateNetworkAclEntry', DependsOn: ['PrivateNetworkAcl'], Type: 'AWS::EC2::NetworkAclEntry', Properties: { NetworkAclId: ref('PrivateNetworkAcl'), RuleNumber: '100', Protocol: '6', RuleAction: 'allow', Egress: 'false', CidrBlock: '0.0.0.0/0', PortRange: { From: '0', To: '65535' } } resource 'OutBoundPrivateNetworkAclEntry', DependsOn: ['PrivateNetworkAcl'], Type: 'AWS::EC2::NetworkAclEntry', Properties: { NetworkAclId: ref('PrivateNetworkAcl'), RuleNumber: '100', Protocol: '6', RuleAction: 'allow', Egress: 'true', CidrBlock: '0.0.0.0/0', PortRange: { From: '0', To: '65535' } } resource 'NATSecurityGroup', DependsOn: ['PrivateSecurityGroup'], Type: 'AWS::EC2::SecurityGroup', Properties: { GroupDescription: 'Enable internal access to the NAT device', VpcId: ref('VPC'), SecurityGroupIngress: [ { IpProtocol: 'tcp', FromPort: '22', ToPort: '22', SourceSecurityGroupId: ref('PrivateSecurityGroup') }, { IpProtocol: 'tcp', FromPort: '80', ToPort: '80', SourceSecurityGroupId: ref('PrivateSecurityGroup') }, { IpProtocol: 'tcp', FromPort: '443', ToPort: '443', SourceSecurityGroupId: ref('PrivateSecurityGroup') }, { IpProtocol: 'tcp', FromPort: '465', ToPort: '465', SourceSecurityGroupId: ref('PrivateSecurityGroup') }, { IpProtocol: 'icmp', FromPort: '-1', ToPort: '-1', CidrIp: '10.0.0.0/8' } ], SecurityGroupEgress: [ { IpProtocol: '-1', FromPort: '0', ToPort: '65535', CidrIp: '0.0.0.0/0' } ], Tags: [ { Key: 'Name', Value: 'NAT' } ] } resource 'PrivateSecurityGroup', DependsOn: ['VPC'], Type: 'AWS::EC2::SecurityGroup', Properties: { GroupDescription: 'Allow the Application instances to access the NAT device', VpcId: ref('VPC'), SecurityGroupEgress: [ { IpProtocol: 'tcp', FromPort: '0', ToPort: '65535', CidrIp: '10.0.0.0/8' } ], SecurityGroupIngress: [ { IpProtocol: 'tcp', FromPort: '0', ToPort: '65535', CidrIp: '10.0.0.0/8' } ], Tags: [ { Key: 'Name', Value: 'Private' } ] } current_cidr_block = Core::NetworkConfig.mapping_vpc_net[region.to_sym][:VPC] public_cidr_blocks = IPAddress(current_cidr_block).subnet(SUBNET_CIDR_BLOCK_SIZE).map(&:to_string).first(availability_zones.size) availability_zones.zip(public_cidr_blocks).each do |pair, cidr_block| suffix = pair.first public_subnet_name = "PublicSubnet#{suffix.upcase}" resource public_subnet_name, DependsOn: ['VPC'], Type: 'AWS::EC2::Subnet', Properties: { VpcId: ref('VPC'), AvailabilityZone: join('', ref('AWS::Region'), suffix.to_s), CidrBlock: cidr_block, Tags: [ { Key: 'Name', Value: "Public #{suffix.upcase}" }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Public' } ] } resource "PublicSubnetRouteTableAssociation#{suffix.upcase}", DependsOn: [public_subnet_name, 'PublicRouteTable'], Type: 'AWS::EC2::SubnetRouteTableAssociation', Properties: { SubnetId: ref(public_subnet_name), RouteTableId: ref('PublicRouteTable') } nat_device_name = "NATDevice#{suffix.upcase}" resource nat_device_name, DependsOn: [public_subnet_name, 'NATSecurityGroup'], Type: 'AWS::EC2::Instance', Properties: { InstanceType: ref('NATInstanceType'), KeyName: nat_key_name, SourceDestCheck: 'false', ImageId: find_in_map('AWSNATAMI', ref('AWS::Region'), 'AMI'), NetworkInterfaces: [ { AssociatePublicIpAddress: 'true', DeviceIndex: '0', SubnetId: ref(public_subnet_name), GroupSet: [ref('NATSecurityGroup')] } ], Tags: [ { Key: 'Name', Value: nat_device_name } ] } private_route_table_name = "PrivateRouteTable#{suffix.upcase}" resource private_route_table_name, DependsOn: ['VPC'], Type: 'AWS::EC2::RouteTable', Properties: { VpcId: ref('VPC'), Tags: [ { Key: 'Name', Value: "Private #{suffix.upcase}" }, { Key: 'Application', Value: aws_stack_name }, { Key: 'Network', Value: 'Private' } ] } resource "PrivateRoute#{suffix.upcase}", DependsOn: [private_route_table_name, nat_device_name], Type: 'AWS::EC2::Route', Properties: { RouteTableId: ref(private_route_table_name), DestinationCidrBlock: '0.0.0.0/0', InstanceId: ref(nat_device_name) } output public_subnet_name, Description: "Created Subnet #{suffix.upcase}", Value: ref(public_subnet_name) end output 'VpcId', Description: 'Created VPC', Value: ref('VPC') output 'PrivateSecurityGroup', Description: 'SecurityGroup to add private resources', Value: ref('PrivateSecurityGroup') end |