2016-12-26 83 views
0

是否可以使用您所在的服務器更改ec2中的IP?如何更改ec2中的彈性IP

我目前做的控制檯:

  1. 關機EC2
  2. 撇清彈性IP地址
  3. 釋放彈性IP地址(這樣我就可以創建一個新的,否則,它說最大IPS)
  4. 分配一個新的彈性IP地址
  5. 啓動EC2
  6. 關聯的新的彈性IP地址的EC2

但是,我在控制檯做ec2服務器關閉後,這樣做。有沒有辦法在我使用的當前服務器上執行此操作。例如,僞代碼:

import ec2, boto 
ec2.disappociate_current_ip() 
ec2.release_ip() 
ec2.allocate_new_ip() 
ec2.associate_new_ip() 

回答

3

這是可能的。 但是當你斷開你的彈性IP地址時,根據你的子網設置你可能會失去互聯網連接。如果您的子網配置爲自動分配公共IP,您將在解除關聯和關聯之間獲得公共IP(非彈性IP)。但是,如果您的公有子網未配置爲自動獲取公共IP,則您的實例將失去互聯網連接(除非有到達互聯網的路由),並且腳本的其餘部分不會執行。以下是Boto3腳本給​​你一個想法但未達標。調整它以適應您的需求。

import boto3 
import requests 

client = boto3.client('ec2') 
inst_id = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text 
print inst_id 

public_ip = requests.get('http://169.254.169.254/latest/meta-data/public-ipv4').text 
print 'Current IP:', public_ip 
print 'Disassociating:', public_ip 
client.disassociate_address(PublicIp=public_ip) 

public_ip = client.allocate_address(Domain='vpc')['PublicIp'] 
print 'New IP:', public_ip 
print 'Associating:', public_ip 
client.associate_address(InstanceId=inst_id, PublicIp=public_ip) 
相關問題