在Amazon VPC中可以存在以下资源:
import boto3
# 创建VPC
ec2 = boto3.resource('ec2')
vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
vpc.create_tags(Tags=[{"Key": "Name", "Value": "my-vpc"}])
# 创建子网
subnet = ec2.create_subnet(CidrBlock='10.0.0.0/24', VpcId=vpc.id)
subnet.create_tags(Tags=[{"Key": "Name", "Value": "my-subnet"}])
import boto3
# 创建路由表
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('vpc-id')
route_table = vpc.create_route_table()
route_table.create_tags(Tags=[{"Key": "Name", "Value": "my-route-table"}])
# 关联子网和路由表
subnet = ec2.Subnet('subnet-id')
route_table.associate_with_subnet(SubnetId=subnet.id)
import boto3
# 创建Internet Gateway
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('vpc-id')
internet_gateway = ec2.create_internet_gateway()
internet_gateway.create_tags(Tags=[{"Key": "Name", "Value": "my-internet-gateway"}])
# 将Internet Gateway连接到VPC
vpc.attach_internet_gateway(InternetGatewayId=internet_gateway.id)
import boto3
# 创建安全组
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('vpc-id')
security_group = vpc.create_security_group(GroupName='my-security-group', Description='My security group')
security_group.create_tags(Tags=[{"Key": "Name", "Value": "my-security-group"}])
# 添加入站规则
security_group.authorize_ingress(
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 80,
'ToPort': 80,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
}
]
)
# 添加出站规则
security_group.authorize_egress(
IpPermissions=[
{
'IpProtocol': '-1',
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
}
]
)
import boto3
# 创建网络ACL
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('vpc-id')
network_acl = vpc.create_network_acl(VpcId=vpc.id)
network_acl.create_tags(Tags=[{"Key": "Name", "Value": "my-network-acl"}])
# 添加入站规则
network_acl_entry = network_acl.create_entry(
RuleNumber=100,
Protocol='-1',
RuleAction='allow',
Egress=False,
CidrBlock='0.0.0.0/0'
)
# 添加出站规则
network_acl_entry = network_acl.create_entry(
RuleNumber=100,
Protocol='-1',
RuleAction='allow',
Egress=True,
CidrBlock='0.0.0.0/0'
)
以上代码示例使用了Python的boto3库来创建和管理Amazon VPC中的资源。请确保已正确配置AWS凭证,并将代码中的'vpc-id'、`'subnet-id'