Python Boto3:如何创建和标记虚拟专用网关[create_vpn_Gateway()]
我仍在思考如何正确地创建和标记一个Python Boto3:如何创建和标记虚拟专用网关[create_vpn_Gateway()],python,amazon-web-services,boto3,Python,Amazon Web Services,Boto3,我仍在思考如何正确地创建和标记一个虚拟专用网关,并将其连接到专有网络 ec2_inst = boto.Session(profile_name='my_profile').resource('ec2') vpg = ec2_inst.create_vpn_gateway(Type='ipsec.1', AmazonSideAsn=64512) 但是我得到:AttributeError:'ec2.ServiceResource'对象没有属性“create\u vpn\u gateway”(可能是
虚拟专用网关
,并将其连接到专有网络
ec2_inst = boto.Session(profile_name='my_profile').resource('ec2')
vpg = ec2_inst.create_vpn_gateway(Type='ipsec.1', AmazonSideAsn=64512)
但是我得到:AttributeError:'ec2.ServiceResource'对象没有属性“create\u vpn\u gateway”
(可能是因为显而易见的原因)。如果我将代码改为使用client('ec2')
,则它可以工作:
ec2_inst = boto.Session(profile_name='my_profile').client('ec2')
vpg = ec2_inst.create_vpn_gateway(Type='ipsec.1', AmazonSideAsn=64512)
我知道resources()
是client()
的高级包装,并没有涵盖所有client()功能,但是有没有一种方法可以使用create\u vpn\u gateway()
另外,我如何标记所创建的网关并将其连接到专有网络?这也不起作用:
vpg.create_tags(
Tags = [ { 'Key': 'Name', 'Value': 'MY-VPG' }, ]
)
vpg.attach_to_vpc(VpcId=vpc.vpc_id)
为既不具有“创建标记”属性也不具有“附加vpn网关”属性的“dict”对象提供AttributeError
。知道我该怎么做吗?最好的 使用boto3.resource
进行ec2
操作,您可以从.meta.client
访问客户端
import boto3
TAGS = [{'Key':'label', 'Value': 'test'}]
ec2 = boto3.resource('ec2')
vpc = list(ec2.vpcs.all())[0] # or make a new vpc & subnet:
# https://github.com/boto/boto3/tree/1.4.8/docs/source/guide/migrationec2.rst#creating-a-vpc-subnet-and-gateway
operation_result = ec2.meta.client.create_vpn_gateway(Type='ipsec.1')
try:
gateway_id = operation_result['VpnGateway']['VpnGatewayId']
ec2.meta.client.attach_vpn_gateway(VpcId=vpc.id, VpnGatewayId=gateway_id)
ec2.create_tags(Tags=TAGS, Resources=[gateway_id])
except KeyError:
print('Failed to create VPN gateway.')
使用boto3.resource
进行ec2
操作,您可以从.meta.client
访问客户端
import boto3
TAGS = [{'Key':'label', 'Value': 'test'}]
ec2 = boto3.resource('ec2')
vpc = list(ec2.vpcs.all())[0] # or make a new vpc & subnet:
# https://github.com/boto/boto3/tree/1.4.8/docs/source/guide/migrationec2.rst#creating-a-vpc-subnet-and-gateway
operation_result = ec2.meta.client.create_vpn_gateway(Type='ipsec.1')
try:
gateway_id = operation_result['VpnGateway']['VpnGatewayId']
ec2.meta.client.attach_vpn_gateway(VpcId=vpc.id, VpnGatewayId=gateway_id)
ec2.create_tags(Tags=TAGS, Resources=[gateway_id])
except KeyError:
print('Failed to create VPN gateway.')
谢谢你的更正,苏尔奥卢瓦菲米!谢谢你的更正,苏尔奥卢瓦菲米!我想你想说:ec2.创建标签(标签=标签,资源=[gateway\u id])
?使用Resources=[gateway\u id]
it重命名VPC标签。@MacUsers是的,这是正确的。我已经编辑了代码示例,我想您应该说:ec2.创建标签(标签=标签,资源=[gateway\u id])
?使用Resources=[gateway\u id]
it重命名VPC标签。@MacUsers是的,这是正确的。我已经编辑了代码示例