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

查看:23
本文介绍了为 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天全站免登陆