Giữ lại IP của EC2 instance cũ, assign sang EC2 instance mới
Trong trường hợp bạn tạo lại EC2 instance và muốn giữ lại IP address cũ:
① Với public IP address:
– Nếu bạn không sử dụng EIP: Bạn sẽ không thể giữ lại public IP address.
– Nếu bạn sử dụng EIP: Bạn có thể detach EIP khỏi EC2 instance cũ, và attach lại vào EC2 instance mới.
Chi tiết cách làm, bạn tham khảo thêm trong tài liệu [1]
② Với private IP address: ok nhé
Bạn chỉ có thể detach được additional ENI, không thể detach được primary ENI của EC2 instance. [2]
Phương án workaround:
Step 1: Terminate EC2 instance hiện tại để release địa chỉ private IP hiện tại
Step 2: Attach địa chỉ IP của instance đã bị terminate ở bước 1 làm secondary private IP vào ENI của EC2 instance mới. Sau khi đã attach secondary private IP, bạn sẽ có thể kết nối tới EC2 instance mới bằng private IP address của EC2 instance cũ.
Lưu ý:
– EC2 instance mới phải ở trong cùng 1 VPC, subnet với instance cũ.
– Trong trường hợp bạn tạo EC2 instance mới sau khi đã terminate instance cũ, thì không cần attach làm secondary IP, bạn có thể chỉ định private IP của instance mới là private IP của instance cũ trong khi tạo EC2 instance. Chi tiết KH tham khảo thêm trong tài liệu [3]
– Sau khi attach secondary private IP, cần thiết định EC2 operating system để nhận địa chỉ IP mới.
Chi tiết bạn tham khảo thêm tài liệu [4]
Tài liệu tham khảo:
[1][Work with Elastic IP addresses]
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html#working-with-eips
[2][Elastic network interfaces]
Each instance has a default network interface, called the primary network interface. You cannot detach a primary network interface from an instance.
[3] [How do I assign a custom primary private IP address to my Amazon EC2 instance?]
https://aws.amazon.com/premiumsupport/knowledge-center/custom-private-primary-address-ec2/
[4][How do I configure a secondary private IPv4 address on my EC2 instance?]
https://aws.amazon.com/premiumsupport/knowledge-center/secondary-private-ip-address/