-
-
Save ijrsvt/80954a02421e6ca611786def37f7c948 to your computer and use it in GitHub Desktop.
Configure VPC for Anyscale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
# REGION_NAME = "ap-northeast-2" | |
# VPC_ID = "vpc-07db02be7cc4e1331" | |
ec2 = boto3.client("ec2", region_name=REGION_NAME) | |
for sg in ec2.describe_security_groups()["SecurityGroups"]: | |
if sg["VpcId"] == VPC_ID and sg["GroupName"] != "default": | |
ec2.delete_security_group( | |
GroupId=sg["GroupId"]) | |
for subnet in ec2.describe_subnets()["Subnets"]: | |
if subnet["VpcId"] == VPC_ID: | |
ec2.delete_subnet(SubnetId=subnet["SubnetId"]) | |
for igw in ec2.describe_internet_gateways()["InternetGateways"]: | |
if all(a["VpcId"] == VPC_ID for a in igw["Attachments"]): | |
ec2.detach_internet_gateway(InternetGatewayId=igw["InternetGatewayId"],VpcId=VPC_ID) | |
ec2.delete_internet_gateway(InternetGatewayId=igw["InternetGatewayId"]) | |
ec2.delete_vpc(VpcId=VPC_ID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
# REGION_NAME = "ap-northeast-2" | |
ec2 = boto3.client("ec2", region_name=REGION_NAME) | |
# Create a new VPC | |
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | |
# Create subnets in each zone (this assumes a region has less than 16 AZs) | |
zones = [ | |
zone["ZoneName"] | |
for zone in ec2.describe_availability_zones()["AvailabilityZones"] | |
if zone["State"] == "available" | |
] | |
for i, zone in enumerate(zones): | |
subnet = ec2.create_subnet( | |
CidrBlock="10.0.{}.0/20".format(i * 16), | |
VpcId=vpc["Vpc"]["VpcId"], | |
AvailabilityZone=zone, | |
) | |
ec2.modify_subnet_attribute( | |
SubnetId=subnet["Subnet"]["SubnetId"], MapPublicIpOnLaunch={"Value": True} | |
) | |
# Create an Internet Gateway to allow outbound traffic | |
igw = ec2.create_internet_gateway() | |
ec2.attach_internet_gateway( | |
VpcId=vpc["Vpc"]["VpcId"], | |
InternetGatewayId=igw["InternetGateway"]["InternetGatewayId"], | |
) | |
# Create a route from the VPC -> the internet (via the internet gateway) on the "Main" route table (created with the VPC) | |
main_tables = [ | |
table["RouteTableId"] | |
for table in ec2.describe_route_tables()["RouteTables"] | |
if table["VpcId"] == vpc["Vpc"]["VpcId"] | |
] | |
assert len(main_tables) == 1, "Only one main route table should be found" | |
ec2.create_route( | |
RouteTableId=main_tables[0], | |
GatewayId=igw["InternetGateway"]["InternetGatewayId"], | |
DestinationCidrBlock="0.0.0.0/0", | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment