如何在使用 EC2 自动扩展时管理弹性 IP?

huangapple go评论56阅读模式
英文:

How to manage elastic IP with ec2 auto-scaling?

问题

我已创建了一个仅运行一个实例的Ec2自动扩展组。此Ec2实例与弹性IP相关联。当Ec2实例被终止时,弹性IP保持附加到已终止的实例,并自动扩展启动一个新实例,但没有弹性IP。

是否有一种好的方法可以在实例终止时分离弹性IP,并将相同的弹性IP附加到新创建的EC2实例上?

我还尝试使用EC2用户数据来附加弹性IP,使用以下命令:

#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id 4x.xx6.xx0.xx

但这也没有奏效。此外,我已向EC2添加了适当的关联角色。

我考虑设置AWS EventBridge规则来捕获实例状态更改,并且该规则最终将触发Lambda函数,该函数将从已终止的EC2实例中分离弹性IP并将其附加到新创建的EC2实例上。

这种方法是否可行,还是有更好和更高效的方法可用?

英文:

I have created an Ec2 auto-scaling group which runs only one instance. This Ec2 instance is associated with Elastic IP. When Ec2 instance gets terminated, the Elastic IP is kept attaching to the terminated instance and auto-scaling spins up a new instance with no Elastic IP.

Is there any good way to detach that Elastic IP when the instance is terminated and attach the same Elastic IP to newly created EC2 instance?

I also try to use EC2 user data to attach elastic IP by using these commands:

#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id 4x.xx6.xx0.xx

but it didn't work too. Moreover, I added proper association role to ec2.

I am thinking of setting up AWS EventBridge rule to catch the instance status change and that rule will eventually trigger lambda function which will disassociate the Elastic IP from the terminated EC2 instance and associate it to the newly created EC2.

Is this approach is good or there is some better and efficient approach available?

答案1

得分: 0

你似乎已经启用了实例元数据服务版本2(IMDSv2),这需要令牌传递以提供额外的安全性。

您可以通过操作 / 实例设置 / 修改实例元数据选项将其返回到版本1,并将IMDSv2设置为“可选”。

英文:

It sounds like you have activated Instance Metadata Service Version 2 (IMDSv2), which requires token-passing for extra security.

You can return it to Version 1 via Actions / Instance Settings / Modify instance metadata options and make IMDSv2 'Optional'.

huangapple
  • 本文由 发表于 2023年4月11日 16:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定