2016-12-26 117 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)