为Terraform aws_autoscaling_group模块的每个实例设置唯一的标记 [英] Set a unique tag to each instance of Terraform aws_autoscaling_group module

查看:150
本文介绍了为Terraform aws_autoscaling_group模块的每个实例设置唯一的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以为Terraform的aws_autoscaling_group模块创建的每个EC2实例分配不同的标记值.例如,用于4个实例的ASG的node1,node2,node3和node4.

is possible to assign a different tag value to each EC2 instance created by the aws_autoscaling_group module of Terraform. For example, node1, node2, node3 and node4 for a ASG of 4 instances.

谢谢.

推荐答案

AWS中的自动伸缩组只能(可选)将ASG的标签传播到其创建的实例.它无法基于ASG中实例的数量进行任何动态标记.

Autoscaling groups in AWS can only (optionally) propagate the tags of the ASG to the instances it creates. It can't do any dynamic tagging based on the count of instances in the ASG.

如果您确实需要这样做,则必须让实例的用户数据执行一些步骤,计算出名称标签应该是什么,然后对其自身进行重新标记(当然,该实例必须具有相关的IAM权限)这个).

If you really need this then you'd have to have the user data of the instances perform some step that calculates what the name tag should be and then retags itself (of course the instance would have to have the relevant IAM permissions for this).

要让一个实例算出ASG中的数字可能会比较棘手(考虑一下,如果您的自动缩放操作同时添加多个实例,或者足够快地添加了多个实例,以至于某些实例可能在创建另一个实例之前没有重新标记自己的位置),但是您可以更容易让它找到自己的实例ID,并在需要时使用它来创建唯一的标记名称.

For an instance to work out what number it is in an ASG might be tricky (consider if your autoscaling action adds multiple instances at the same time or quickly enough that some may not have retagged themselves before another is created) but you could more easily get it to find its own instance Id and use that to create a unique tagged name if that's more what you want.

在这种情况下,类似的事情可能会有用:

Something like this might be useful in that case:

ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
CURRENT_NAME=$(aws ec2 describe-tags --filters Name=resource-id,Values=${ID} Name=key,Values=Name --query Tags[].Value --output text
NEW_NAME=${CURRENT_NAME}-${ID}

aws ec2 create-tags --resources ${ID} --tags Key=Name,Value=${NEW_NAME}

这篇关于为Terraform aws_autoscaling_group模块的每个实例设置唯一的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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