将一个AWS Elastic IP用于多个实例 [英] Use one AWS Elastic IP for several instances

查看:116
本文介绍了将一个AWS Elastic IP用于多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为客户提供固定的URL,这些URL在停止/启动EC2实例时不会更改,因为有时我们需要更改EC2的大小,并且在重新启动实例时,公共IP已更改.

I need to provide my customers with fixed urls that don't change when the EC2 instances are stopped/started because sometimes we need to change the size of the EC2 and when we restart the instance the public IP has changed.

我考虑过使用弹性IP,以便在实例重新启动时可以保留相同的公共IP,但是我已经看到Amazon告诉您您只有5个弹性IP.如果您问他们,他们说他们可以给您更多,但是我想他们不会给您10.000.

I thought on using Elastic IPs so I can keep the same public IP when the instance is rebooted, but I've seen that Amazon tells you that you only have 5 Elastic IPs. If you ask them they say that they can give you more, but I guess they're not giving you 10.000 of them.

如何使用单个公共弹性IP为每个用户提供不同的URL进行服务?

How can I use a single public Elastic IP to give each user different URLs for out service?

就像这样,是11.22.33.44弹性IP和192.168.0.X two EC2 instances:

It would be something like this, being 11.22.33.44 the Elastic IP and 192.168.0.X two EC2 instances:

11.22.33.44:**1000** --> 192.168.0.**1**:22
11.22.33.44:**1001** --> 192.168.0.**1**:80
11.22.33.44:**1002** --> 192.168.0.**1**:443

11.22.33.44:**1003** --> 192.168.0.**2**:22
11.22.33.44:**1004** --> 192.168.0.**2**:80
11.22.33.44:**1005** --> 192.168.0.**2**:443

我需要使它以编程方式工作,因为我需要根据需要从SDK创建EC2实例.

I need to make it work programmatically, as I'm creating EC2 instances from the SDK as needed.

我认为的另一种方式是使用.com域中的子域,该子域指向每个EC2实例的当前公共IP,但是使用前面所述的IP听起来更好.

Another way I thought is using subdomains from my .com domain that points to the current public IP of each EC2 instance, but using the IP as I described before sounds better.

推荐答案

问题是实例在停止和启动后正在接收新的(临时)公共IP地址.

The issue is that instances are receiving new (temporary) Public IP addresses after they are stopped and started.

一种简单的处理方法是向每次启动期间运行的每个实例添加脚本.该脚本可以更新DNS记录以将其指向实例.

A simple way to handle this is to add a script to each instance that runs during every boot. This script can update a DNS record to point it at the instance.

该脚本应进入/var/lib/cloud/scripts/per-boot目录,这将使Cloud-Init在每次启动实例时自动运行该脚本.

The script should go into the /var/lib/cloud/scripts/per-boot directory, which will cause Cloud-Init to automatically run the script each time the instance is started.

# Set these values based on your Route 53 Record Sets
ZONE_ID=Z3NAOAOAABC1XY
RECORD_SET=my-domain.com

# Extract information about the Instance
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)
AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/)
MY_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/)

# Extract Name tag associated with instance
NAME_TAG=$(aws ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`Name`].Value' --output text)

# Update Route 53 Record Set based on the Name tag to the current Public IP address of the Instance
aws route53 change-resource-record-sets --hosted-zone-id $ZONE_ID --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"'$NAME_TAG.$RECORD_SET'","Type":"A","TTL":300,"ResourceRecords":[{"Value":"'$MY_IP'"}]}}]}'

该脚本将提取实例的Name标签并更新Route 53中的相应记录集.(可以随意更改此记录集以使用其他标签.)该实例还将需要ec2 describe-tags和Ic的IAM权限. route53 change-resource-record-sets.

The script will extract the Name tag of the instance and update the corresponding Record Set in Route 53. (Feel free to change this to use a different Tag.) The instance will also require IAM permissions for ec2 describe-tags and route53 change-resource-record-sets.

更新:我已将此答案变成了博客文章:

Update: I've turned this answer into a blog post: Amazon Route 53: How to automatically update IP addresses without using Elastic IPs

这篇关于将一个AWS Elastic IP用于多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆