Skip to content

Commit 4ea149d

Browse files
committed
add use_default_vpc option for VPC stack
1 parent dd206c5 commit 4ea149d

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

web_analytics/vpc.py

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env python3
2+
# -*- encoding: utf-8 -*-
3+
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
24

5+
import os
36
import aws_cdk as cdk
47

58
from aws_cdk import (
@@ -21,40 +24,40 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2124
# for example,
2225
# cdk -c vpc_name=your-existing-vpc syth
2326
#
24-
# vpc_name = self.node.try_get_context("vpc_name")
25-
# vpc = aws_ec2.Vpc.from_lookup(self, "ExistingVPC",
26-
# is_default=True,
27-
# vpc_name=vpc_name)
28-
29-
#XXX: To use more than 2 AZs, be sure to specify the account and region on your stack.
30-
#XXX: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/Vpc.html
31-
vpc = aws_ec2.Vpc(self, "WebAnalyticsVPC",
32-
ip_addresses=aws_ec2.IpAddresses.cidr("10.0.0.0/21"),
33-
max_azs=3,
34-
35-
# 'subnetConfiguration' specifies the "subnet groups" to create.
36-
# Every subnet group will have a subnet for each AZ, so this
37-
# configuration will create `2 groups × 3 AZs = 6` subnets.
38-
subnet_configuration=[
39-
{
40-
"cidrMask": 24,
41-
"name": "Public",
42-
"subnetType": aws_ec2.SubnetType.PUBLIC,
43-
},
44-
{
45-
"cidrMask": 24,
46-
"name": "Private",
47-
"subnetType": aws_ec2.SubnetType.PRIVATE_WITH_EGRESS
27+
if str(os.environ.get('USE_DEFAULT_VPC', 'false')).lower() == 'true':
28+
vpc_name = self.node.try_get_context("vpc_name")
29+
self.vpc = aws_ec2.Vpc.from_lookup(self, "ExistingVPC",
30+
is_default=True,
31+
vpc_name=vpc_name)
32+
else:
33+
#XXX: To use more than 2 AZs, be sure to specify the account and region on your stack.
34+
#XXX: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/Vpc.html
35+
self.vpc = aws_ec2.Vpc(self, "WebAnalyticsVPC",
36+
ip_addresses=aws_ec2.IpAddresses.cidr("10.0.0.0/21"),
37+
max_azs=3,
38+
39+
# 'subnetConfiguration' specifies the "subnet groups" to create.
40+
# Every subnet group will have a subnet for each AZ, so this
41+
# configuration will create `2 groups × 3 AZs = 6` subnets.
42+
subnet_configuration=[
43+
{
44+
"cidrMask": 24,
45+
"name": "Public",
46+
"subnetType": aws_ec2.SubnetType.PUBLIC,
47+
},
48+
{
49+
"cidrMask": 24,
50+
"name": "Private",
51+
"subnetType": aws_ec2.SubnetType.PRIVATE_WITH_EGRESS
52+
}
53+
],
54+
gateway_endpoints={
55+
"S3": aws_ec2.GatewayVpcEndpointOptions(
56+
service=aws_ec2.GatewayVpcEndpointAwsService.S3
57+
)
4858
}
49-
],
50-
gateway_endpoints={
51-
"S3": aws_ec2.GatewayVpcEndpointOptions(
52-
service=aws_ec2.GatewayVpcEndpointAwsService.S3
53-
)
54-
}
55-
)
56-
57-
self.vpc = vpc
58-
59-
cdk.CfnOutput(self, '{}_VPCID'.format(self.stack_name), value=self.vpc.vpc_id,
60-
export_name='VPCID')
59+
)
60+
61+
62+
cdk.CfnOutput(self, 'VPCID', value=self.vpc.vpc_id,
63+
export_name=f'{self.stack_name}-VPCID')

0 commit comments

Comments
 (0)