in mephisto/abstractions/architects/ec2/ec2_helpers.py [0:0]
def create_mephisto_vpc(session: boto3.Session) -> Dict[str, str]:
"""
Create the required vpc with two subnets, an associated
internet gateway, and routing tables.
Currently sets up using US-east for both subnets
"""
client = session.client("ec2")
# Create VPC
vpc_response = client.create_vpc(
CidrBlock="10.0.0.0/16",
TagSpecifications=[
{
"ResourceType": "vpc",
"Tags": [
{"Key": "Name", "Value": "mephisto-core-vpc"},
get_owner_tag(),
],
}
],
)
vpc_id = vpc_response["Vpc"]["VpcId"]
# Create internet gateway
gateway_response = client.create_internet_gateway(
TagSpecifications=[
{
"ResourceType": "internet-gateway",
"Tags": [{"Key": "Name", "Value": "mephisto-gateway"}, get_owner_tag()],
}
],
)
gateway_id = gateway_response["InternetGateway"]["InternetGatewayId"]
client.attach_internet_gateway(
InternetGatewayId=gateway_id,
VpcId=vpc_id,
)
# Create subnets
subnet_1_response = client.create_subnet(
TagSpecifications=[
{
"ResourceType": "subnet",
"Tags": [
{"Key": "Name", "Value": "mephisto-subnet-1"},
get_owner_tag(),
],
}
],
CidrBlock="10.0.0.0/24",
AvailabilityZone="us-east-2a",
VpcId=vpc_id,
)
subnet_1_id = subnet_1_response["Subnet"]["SubnetId"]
subnet_2_response = client.create_subnet(
TagSpecifications=[
{
"ResourceType": "subnet",
"Tags": [
{"Key": "Name", "Value": "mephisto-subnet-2"},
get_owner_tag(),
],
}
],
CidrBlock="10.0.1.0/24",
AvailabilityZone="us-east-2b",
VpcId=vpc_id,
)
subnet_2_id = subnet_2_response["Subnet"]["SubnetId"]
# Create routing tables
table_1_response = client.create_route_table(
TagSpecifications=[
{
"ResourceType": "route-table",
"Tags": [
{"Key": "Name", "Value": "mephisto-routes-1"},
get_owner_tag(),
],
}
],
VpcId=vpc_id,
)
route_table_1_id = table_1_response["RouteTable"]["RouteTableId"]
table_2_response = client.create_route_table(
TagSpecifications=[
{
"ResourceType": "route-table",
"Tags": [
{"Key": "Name", "Value": "mephisto-routes-2"},
get_owner_tag(),
],
}
],
VpcId=vpc_id,
)
route_table_2_id = table_2_response["RouteTable"]["RouteTableId"]
# Add routes in tables to gateway
client.create_route(
DestinationCidrBlock="0.0.0.0/0",
GatewayId=gateway_id,
RouteTableId=route_table_1_id,
)
client.create_route(
DestinationCidrBlock="0.0.0.0/0",
GatewayId=gateway_id,
RouteTableId=route_table_2_id,
)
# Associate routing tables
client.associate_route_table(
RouteTableId=route_table_1_id,
SubnetId=subnet_1_id,
)
client.associate_route_table(
RouteTableId=route_table_2_id,
SubnetId=subnet_2_id,
)
return {
"vpc_id": vpc_id,
"gateway_id": gateway_id,
"subnet_1_id": subnet_1_id,
"subnet_2_id": subnet_2_id,
"route_1_id": route_table_1_id,
"route_2_id": route_table_2_id,
}