如何在boto3中关联弹性IP和NAT网关

时间:2017-02-23 17:59:52

标签: python amazon-web-services boto3

我想请求一些关于在boto3中关联弹性IP和NAT网关的帮助。

分配新的弹性IP地址

eip = client.allocate_address(Domain =' vpc')

创建NAT网关并分配到弹性IP

nat_gw = client.create_nat_gateway(SubnetId = subnet1.id,AllocationId = eip.id)

  
    
      

eip = ec2_client.allocate_address(Domain =' vpc')       ec2_client.create_nat_gateway(SUBNETID = subnet1.id,AllocationId = eip.id)       Traceback(最近一次调用最后一次):         文件"",第1行,in       AttributeError:' dict'对象没有属性' id'

    
  

感谢。

2 个答案:

答案 0 :(得分:0)

既然它是一个词典,你不会这样做吗?

nat_gw = client.create_nat_gateway(SubnetId=subnet1['id'],AllocationId=eip['id'])

**我假设subnet1eip都是双语。

我无法验证您是否正确执行此操作,但上述内容应解决您所看到的错误。

答案 1 :(得分:0)

我设法解决了这个问题。 (但这不是我喜欢的方式)

import boto3
ec2 = boto3.resource('ec2', region_name="eu-west-2")
client = boto3.client('ec2', region_name="eu-west-2")
vpc = ec2.create_vpc(CidrBlock='10.10.0.0/16')
subnet1 = vpc.create_subnet(CidrBlock='10.10.1.0/24',AvailabilityZone='eu-west-2a')
subnet1.meta.client.modify_subnet_attribute(SubnetId=subnet1.id, MapPublicIpOnLaunch={"Value": True})

eip_for_nat_gateway = ec2_client.allocate_address(Domain='vpc')
>>> a = client.describe_addresses()
>>> for b in a['Addresses']:
...print b['AllocationId']
eipalloc-3790c652
>>> c= b['AllocationId']
>>> print c
eipalloc-3790c652

#Now I assign c as AllocationId
nat_gw = client.create_nat_gateway(SubnetId=subnet1.id,AllocationId=c)