簡體   English   中英

如何在boto3中關聯彈性IP和NAT網關

[英]How to associate Elastic IP and NAT Gateway in 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)追溯(最近一次調用):AttributeError:'dict'對象中的文件“”,行1沒有屬性“ id”

謝謝。

由於這是一個命令,所以您不這樣做嗎?

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

**我假設subnet1eip都是字典。

我無法驗證您是否在正確執行此操作,但是以上內容可以解決您看到的錯誤。

我設法解決了這種方式。 (但這不是我喜歡的方式)

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM