

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Application Auto Scaling 的标签支持
<a name="resource-tagging-support"></a>

您可以使用 Amazon CLI 或 SDK 来标记 Application Auto Scaling 可扩展目标。可扩展目标是代表 Application Auto Scaling 可以扩展的 Amazon 或自定义资源的实体。

每个标签都包含游湖使用 Application Auto Scaling API 定义的键和值。标签可以帮助您根据组织的需求配置对特定可扩展目标的精细访问权限。有关更多信息，请参阅 [结合使用 ABAC 与 Application Auto Scaling](security_iam_service-with-iam.md#security_iam_service-with-iam-tags)。

您可以在注册新的可扩展目标时向目标添加标签，也可以将向现有的可扩展目标添加标签。

用于管理标签的常用命令包括：
+ [register-scalable-target](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/register-scalable-target.html)，用于在注册新的可扩展目标时对其进行标记。
+ [tag-resource](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/tag-resource.html)，用于向现有的可扩展目标添加标签。
+  [list-tags-for-resource](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/list-tags-for-resource.html)，用于返回可扩展目标上的标签。
+ [untag-resource](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/untag-resource.html)，用于删除标签。

## 标签示例
<a name="tagging-example"></a>

使用以下 [register-scalable-target](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令和 `--tags` 选项，如下所示。该示例可用两个标签来标记可扩展目标：一个名称为 **environment**、标签值为 **production** 的标签键；一个名称为 **iscontainerbased**、标签值为 **true** 的标签键。

将和的示例值`--min-capacity``--max-capacity`和的示例文本替换为您在 Application Auto Scaling 中使用的 Amazon 服务的命名空间、`--scalable-dimension`与您正在注册的资源关联的可扩展维度以及`--resource-id`资源的标识符。`--service-namespace`有关每项服务的更多信息和示例，请参阅 [Amazon Web Services 服务 可以与 Application Auto Scaling 一起使用](integrated-services-list.md) 中的主题。

```
aws application-autoscaling register-scalable-target \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}} \
  --min-capacity {{1}} --max-capacity {{10}} \
  --tags {{environment}}={{production}},{{iscontainerbased}}={{true}}
```

如果成功，该命令会返回可扩展目标的 ARN。

```
{
    "ScalableTargetARN": "arn:aws:application-autoscaling:{{region}}:{{account-id}}:scalable-target/1234abcd56ab78cd901ef1234567890ab123"
}
```

**注意**  
如果此命令引发错误，请确保已将 Amazon CLI 本地版本更新到最新版本。

## 安全性标签
<a name="tag-security"></a>

使用标签验证请求者（例如 IAM 用户或角色）是否有权限执行某些操作。使用下面的一个或多个条件键，在 IAM policy 的条件元素中提供标签信息：
+ 使用 `aws:ResourceTag/{{tag-key}}: {{tag-value}}` 可允许（或拒绝）带特定标签的可扩展目标上的用户操作。
+ 使用 `aws:RequestTag/{{tag-key}}: {{tag-value}}` 要求在请求中存在（或不存在）特定标签。
+ 使用 `aws:TagKeys [{{tag-key}}, ...]` 要求在请求中存在（或不存在）特定标签键。

例如，以下 IAM policy 授予执行 `DeregisterScalableTarget`、`DeleteScalingPolicy` 和 `DeleteScheduledAction` 操作的权限。但如果对其执行操作的可扩展目标组具有标签 `environment`=`production`，在此策略也会拒绝操作。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "application-autoscaling:DeregisterScalableTarget",
                "application-autoscaling:DeleteScalingPolicy",
                "application-autoscaling:DeleteScheduledAction"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Deny",
            "Action": [
                "application-autoscaling:DeregisterScalableTarget",
                "application-autoscaling:DeleteScalingPolicy",
                "application-autoscaling:DeleteScheduledAction"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/{{environment}}": "{{production}}"
                }
            }
        }
    ]
}
```

------

## 控制对标签的访问
<a name="tag-permissions"></a>

使用标签来验证请求者（例如 IAM 用户或角色）是否有权限添加、修改或删除可扩展目标的标签。

例如，您可以创建一个 IAM policy，以仅允许从可扩展目标中删除具有 `temporary` 键的标签。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "application-autoscaling:UntagResource",
            "Resource": "*",
            "Condition": {
                "ForAllValues:StringEquals": { "aws:TagKeys": ["{{temporary}}"] }
            }
        }
    ]
}
```

------