有人知道如何通过 boto 标记 AWS Autoscale 旋转 EC2 实例吗?我希望所有 Autoscale EC2 实例都带有“as-”前缀。
谢谢
答案1
我明白了。
创建 AWS Autoscale 组后,为其分配一个“名称”标签。诀窍是在将“propagate_at_launch”标志传递给 Autoscale Tag 对象时将其设置为 True。将此标志设置为 True 可确保在创建标签后将标签应用于任何 Autoscale 旋转的 EC2 实例。示例如下:
import boto
from boto.ec2.autoscale import Tag
# Make sure your access keys are stored in ~/.boto
conn = boto.connect_autoscale()
# This assumes you have already setup an elastic load balancer
# and a launch configuration
ag = AutoScalingGroup(group_name=group_name,
load_balancers=[load_balancer],
availability_zones=availability_zones,
launch_config=config,
min_size=min_size,
max_size=max_size)
# Create auto scaling group
conn.create_auto_scaling_group(ag)
# Fetch the autoscale group after it is created
auto_scaling_group = conn.get_all_groups(names=[group_name])[0]
# Create a Tag for the austoscale group
as_tag = Tag(key='Name',
value = 'as-instance',
propagate_at_launch=True,
resource_id=group_name)
# Add the tag to the autoscale group
conn.create_or_update_tags([as_tag])
瞧!现在,每当基于 CloudWatch Alarms 从此 Autoscale 组中启动 EC2 实例时(即基于 CPU 利用率阈值或其他指标进行扩展),该实例将具有名称值 =“as-instance”
干杯