示例代码:
import boto3
# create client for EC2
ec2 = boto3.client('ec2')
# create VPC
response = ec2.create_vpc(
CidrBlock='10.0.0.0/16'
)
vpc_id = response['Vpc']['VpcId']
# create subnet
response = ec2.create_subnet(
CidrBlock='10.0.0.0/24',
VpcId=vpc_id
)
subnet_id = response['Subnet']['SubnetId']
# create internet gateway
response = ec2.create_internet_gateway()
igw_id = response['InternetGateway']['InternetGatewayId']
# attach internet gateway to VPC
ec2.attach_internet_gateway(
InternetGatewayId=igw_id,
VpcId=vpc_id
)
# get route table for VPC
response = ec2.describe_route_tables(
Filters=[
{
'Name': 'vpc-id',
'Values': [
vpc_id,
]
},
]
)
route_table_id = response['RouteTables'][0]['RouteTableId']
# add route to internet gateway
response = ec2.create_route(
DestinationCidrBlock='0.0.0.0/0',
GatewayId=igw_id,
RouteTableId=route_table_id
)
# create security group
response = ec2.create_security_group(
Description='Allow all traffic',
GroupName='allow_all_traffic',
VpcId=vpc_id
)
sg_id = response['GroupId']
# allow all inbound traffic
ec2.authorize_security_group_ingress(
GroupId=sg_id,
IpPermissions=[
{
'IpProtocol': '-1',
'IpRanges': [
{
'CidrIp': '0.