是的,Amazon EC2实例在停止和重新启动时会自动刷新其IP地址。但是,如果实例没有停止和重新启动,它的IP地址通常会保持不变。
以下是使用AWS SDK for Python(Boto3)的代码示例,用于停止和重新启动EC2实例以刷新IP地址:
import boto3
# 创建EC2客户端
ec2_client = boto3.client('ec2')
# 停止实例
response = ec2_client.stop_instances(InstanceIds=['your-instance-id'])
# 等待实例停止
ec2_client.get_waiter('instance_stopped').wait(InstanceIds=['your-instance-id'])
# 启动实例
response = ec2_client.start_instances(InstanceIds=['your-instance-id'])
# 等待实例启动
ec2_client.get_waiter('instance_running').wait(InstanceIds=['your-instance-id'])
请确保将your-instance-id替换为您要停止和重新启动的实例的实际ID。
这段代码使用AWS SDK for Python(Boto3)中的stop_instances和start_instances方法来停止和重新启动实例。然后,使用get_waiter来等待实例停止和启动完成。这将导致实例的IP地址被刷新。