自动递增EC2实例名称 [英] Auto increment EC2 instance Name
问题描述
我在Stackoverflow上看到了许多问题,询问是否存在一种自动增加实例名称的方法。例如:
I have seen many questions on Stackoverflow asking if there is a way to auto-increment instance names. For example:
foo1
foo2
fooN
我正在尝试查看Powershell中是否有这样做的方法。我正在使用AutoLaunchConfiguration / ASG启动实例。有自动编程实例方式的编程方式吗?到目前为止,我所见过的每一种资源都说有可能,但没有提供一种实现此目的的方法。以下是一些我正在查看的资源:
I am trying to see if there is a way of doing this in Powershell. I am using AutoLaunchConfiguration/ASG to spin up the instances. Is there a programmatic way of auto-incrementing instance ways? Every resource I have seen so far says it's possible but no on provides a means of doing this. Here are some of the resources I am looking at:
- Method to provide incremental name to ec2 instances created using auto scaling group in aws
- How to create variable number of EC2 instance resources in Cloudformation template?
是否有人有使用AutoLaunchConfiguration进行操作的有效示例和ASG?如果是这样,您能否提供可以向我提供此信息的链接/文章?
Does anyone have a working example of doing this using AutoLaunchConfiguration and ASG? If so, can you please provide a link/article that can provide me with this information?
推荐答案
第一件事是决定什么是自动递增 。
例如,如果这些实例已经存在:
For example, if these instances already exist:
-
foo1
-
foo2
foo1
foo2
然后显然下一个实例将是 foo3
。
then obviously the next instance would be foo3
.
但是,我们可以说一个实例先前由于负载减少而终止,并且当前正在运行的实例为:
But let's say that an instance has previously been terminated because load reduced, and the currently-running instances are:
-
foo1
-
foo3
foo1
foo3
然后,问题是下一个实例是 foo2
还是 foo4
。
Then the question is whether the next instance should be foo2
or foo4
.
如果答案是 foo4
,请考虑以下情况: c $ c> foo4 随后终止,并启动另一个实例。应该是 foo4
还是 foo5
(因为以前有一个 foo4
)。
If the answer is foo4
, then think of the situation where foo4
is later terminated and another instance is launched. Should it be foo4
or foo5
(since there was previously a foo4
).
它实际上可以归结为确定为什么您想要自动递增的名称。
It really boils down to determining why you want an auto-incremented name.
原因通常是:
- 需要唯一名称
- 需要人性化名称
- 需要提示启动实例的顺序
- The need for a unique name
- The need for a human-friendly name
- The need for a hint as to the order in which instances where launched
如果需要唯一名称,那么实例ID 可以完全满足此需求,而无需使用其他名称。
If the need is for a unique name, then the Instance ID can fulfil this need perfectly, without needing an additional name.
让我们假设您要使用一个人性化的名称并且您可以重新使用以前使用的名称(例如,如果 foo1
和 foo3
存在,则使用 foo2
接下来)。在这种情况下,您需要在逻辑上检查现有实例并找到第一个未使用的数字。
Let's assume you want to go with a human-friendly name and you are okay to re-use names that were previously used (eg if foo1
and foo3
exists, then use foo2
next). In such a case, you would need something that logically inspects the existing instances and finds the first unused number.
如果,另一方面,您再也不想重复使用名称了,那么您将需要某个位置来存储当前数字,以便可以在下一个实例中增加它。
If, on the other hand, you never wanted to re-use names, then you would need some place to store the 'current' number so that it can be incremented for the next instance.
最后,出现了一个问题,即如何分配自动递增的名称(这是您最初的问题,但是上面的内容对于理解也是很重要的)。我的建议是:
Then, finally, comes the question of how to assign the auto-incremented name (which is your original question, but the above is also very important to understand first). My recommendation would be:
- 向通过 User Data 触发的每个实例添加一些代码,以便将在实例启动时运行(例如PowerShell)
- 代码将使用
describe-instances
或保存计数的地方,然后为其分配下一个数字 - 然后代码可以调用
创建标签
本身,并添加一个名称$ c适当名称的$ c>标记
- Add some code to each instance that is triggered through the User Data so that it will run when the instance is launched (eg PowerShell)
- The code will inspect the existing instances with
describe-instances
or the place where the count is kept, then assign itself the next number - The code can then call
create-tags
on itself, adding aName
tag with the appropriate name
这篇关于自动递增EC2实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!