Class: Enscalator::Templates::VPCWithNATGateway
- Inherits:
-
RichTemplateDSL
- Object
- TemplateDSL
- RichTemplateDSL
- Enscalator::Templates::VPCWithNATGateway
- Includes:
- Plugins::NATGateway
- Defined in:
- lib/enscalator/vpc_with_nat_gateway.rb
Overview
Amazon AWS Virtual Private Cloud template with NAT gateway
Direct Known Subclasses
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 included from Plugins::NATGateway
#add_route_rule, #allocate_new_eip, #nat_gateway_init
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
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 |
# File 'lib/enscalator/vpc_with_nat_gateway.rb', line 11 def tpl description = <<-EOS.gsub(/^\s+\|/, '') |AWS CloudFormation template for the VPC environment. |For each availability zone stack creates: the public subnet, internet and NAT gateways, |internet access from private subnets, routing configuration for corresponding subnets |and security groups. EOS value Description: description 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 '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') } 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' } ] } # Important! # When updating stack that was previously deployed using VPC template with NAT EC2 instance: # 1. Comment out lines related to NAT device below # 2. Update stack using this template (it will remove NAT instances and related security and routing rules) # 3. Revert changes from 1. (i.e. uncomment lines below) # 4. Update stack again (this time it will create new NAT Gateway, EIP and routing rule resources) nat_device_name = "NATDevice#{suffix.upcase}" nat_gateway_init(nat_device_name, public_subnet_name, private_route_table_name, depends_on: [public_subnet_name, 'PublicRouteTable', 'GatewayToInternet']) output public_subnet_name, Description: "Created Subnet #{suffix.upcase}", Value: ref(public_subnet_name) end # each availability zone output 'VpcId', Description: 'Created VPC', Value: ref('VPC') output 'PrivateSecurityGroup', Description: 'SecurityGroup to add private resources', Value: ref('PrivateSecurityGroup') end |