Keep same IP address for EC2 Instance

独自空忆成欢 提交于 2020-12-08 07:52:28

问题


Do EC2 instances change the IP address for your instance each time you stop/start the instance? Is there a way to keep the IP address constant?


回答1:


Yes, there is a way: Elastic IP Addressing.

AWS instances are launched with a dynamic IP address by default, which means that the IP address changes every time the server is stopped and restarted. In many cases, this is not desired and so, users also have the option to assign the server a static IP address (also known as an “elastic IP”).3


According to Amazon 1, 2:

An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. An Elastic IP address is associated with your AWS account. With an Elastic IP address, you can mask the failure of an instance or software by rapidly remapping the address to another instance in your account.

And:

You can have one Elastic IP (EIP) address associated with a running instance at no charge. If you associate additional EIPs with that instance, you will be charged for each additional EIP associated with that instance per hour on a pro rata basis. Additional EIPs are only available in Amazon VPC.


To configure a static IP address:

  • Log in to the AWS EC2 Dashboard. If required, use the region selector in the top right corner to switch to the region where your instance was launched.
  • Select the instance in the dashboard.
  • In the left navigation bar, select the “Network & Security -> Elastic IPs” menu item.
  • Click the “Allocate New Address” button.

For more details on setting it up, see Allocating an Elastic IP Address and Configure a static IP address.

There is a charge a small hourly charge if an Elastic IP address is not associated with a running instance, or if it is associated with a stopped instance or an unattached network interface. The charge is prorated and depends on the region; details can be found on Amazon EC2 Pricing.




回答2:


Get an EIP (Elastic IP) which is free when the instance is running but paid a bit when the instance is stopped.

https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html




回答3:


Elastic IP has its limitations.

If you have reached the maximum number of Elastic IP addresses in a region, and all you want is a constant way to connect to an EC2 instance, I would recommend using a route53 record instead of using IP address.

I create a route53 record that points to the IP address of my EC2 instance. The record doesn't get changed when the EC2 is stopped.

And the way to keep the record pointing to the address of the EC2 is by running a script that changes the route53 record when the EC2 launches.

Here's the user data of my EC2:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash

# get the public ip address
# Ref: https://stackoverflow.com/questions/38679346/get-public-ip-address-on-current-ec2-instance
export public_ip=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)

cat <<EOF > input.json
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "my-domain.my-company.com",
        "Type": "A",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "${public_ip}"
          }
        ]
      }
    }
  ]
}
EOF

# change route53 record 
/usr/bin/aws route53 change-resource-record-sets \
  --hosted-zone-id <hosted_zone_of_my-company.con> \
  --change-batch file://input.json >
--//

Here I use my-domain.my-company.com as the route53 record for my EC2.

By using this method, you get a route53 record that points to your EC2 instance. And the record does not change when you stop and start the EC2. So you can always use the route53 record to connect to your EC2.

Remember to assign an IAM role that has route53 permissions to the EC2 instance so that you can run the user data without errors.

And remember that the user data I provided is intended for use with Amazon Linux 2, and the commands may not work for other Linux distributions.



来源:https://stackoverflow.com/questions/57228441/keep-same-ip-address-for-ec2-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!