

# API Gateway で API ステージのタグをセットアップする
<a name="set-up-tags"></a>

API Gateway では、API ステージにタグを追加したり、ステージからタグを削除したり、タグを表示したりすることができます。これを行うには、API Gateway コンソール、AWS CLI/SDK、または API Gateway REST API を使用できます。

ステージは、その親 REST API からタグを継承することもできます。詳細については、「[Amazon API Gateway V1 API でのタグの継承](apigateway-tagging-supported-resources.md#apigateway-tagging-inheritance)」を参照してください。

API Gateway リソースのタグ付けの詳細については、「[API Gateway リソースのタグ付け](apigateway-tagging.md)」を参照してください。

**Topics**
+ [API Gateway コンソールを使用した API ステージのタグのセットアップ](#set-up-tags-using-console)
+ [AWS CLI を使用して API ステージのタグを設定する](#set-up-tags-using-cli)
+ [API Gateway REST API を使用した API ステージのタグのセットアップ](#set-up-tags-using-api)

## API Gateway コンソールを使用した API ステージのタグのセットアップ
<a name="set-up-tags-using-console"></a>

次の手順では、API ステージのタグを設定する方法を説明します。

**API Gateway コンソールを使用して API ステージのタグを設定するには**

1. [API Gateway コンソール] にサインインします。

1. 既存の API を選択するか、リソース、メソッド、および対応する統合を含む新しい API を作成します。

1. ステージを選択するか、新しいステージに API をデプロイします。

1. メインナビゲーションペインで、**[ステージ]** を選択します。

1. **[タグ]** タブを選択します。タブを表示するには、右矢印ボタンを選択する必要がある場合があります。

1. [**Manage tags (タグの管理)**] を選択します。

1. **[タグエディター]** で、**[新しいタグの追加]** を選択します。[**キー**] フィールドにタグキー (`Department` など) を入力し、[**値**] フィールドにタグ値 (`Sales` など) を入力します。**[保存]** を選択してタグを保存します。

1.  必要に応じて、ステップ 5 を繰り返して API ステージにさらにタグを追加します。ステージあたりのタグの最大数は 50 です。

1.  既存のステージからタグを削除するには、**[削除]** を選択します。

1. API が API Gateway コンソールで既にデプロイされているという場合は、それを再度デプロイして変更を有効にする必要があります。

## AWS CLI を使用して API ステージのタグを設定する
<a name="set-up-tags-using-cli"></a>

AWS CLI で [create-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-stage.html) コマンドまたは [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/tag-resource.html) コマンドを使用して API ステージのタグを設定できます。API ステージから 1 つ以上のタグを削除するには、[untag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/untag-resource.html) コマンドを使用できます。

次の [create-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-stage.html) コマンドは、`test` ステージを作成するときにタグを追加します。

```
aws apigateway create-stage --rest-api-id abc1234 --stage-name test --description 'Testing stage' --deployment-id efg456 --tag Department=Sales
```

次の [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/tag-resource.html) コマンドは、`prod` ステージにタグを追加します。

```
aws apigateway tag-resource --resource-arn arn:aws:apigateway:us-east-2::/restapis/abc123/stages/prod --tags Department=Sales
```

次の [untag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/untag-resource.html) コマンドは、`test` ステージから `Department=Sales` タグを削除します。

```
aws apigateway untag-resource --resource-arn arn:aws:apigateway:us-east-2::/restapis/abc123/stages/test --tag-keys Department 
```

## API Gateway REST API を使用した API ステージのタグのセットアップ
<a name="set-up-tags-using-api"></a>

API Gateway REST API を使用して API ステージのタグを設定するには、次のいずれかの操作を行います。
+ API ステージにタグ付けするには [https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html) を呼び出します。
+  API ステージから 1 つまたは複数のタグを削除するには [https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html) を呼び出します。
+ 作成する API ステージに 1 つまたは複数のタグを追加するには [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html) を呼び出します。

また、API ステージでタグを記述するには [https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html](https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html) を呼び出します。

### API ステージのタグ付け
<a name="tag-a-stage-using-api"></a>

ステージ (`m5zr3vnks7`) に API (`test`) をデプロイした後は、[https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html) を呼び出すことで、ステージをタグ付けします。必須のステージである Amazon リソースネーム (ARN) (`arn:aws:apigateway:us-east-1::/restapis/m5zr3vnks7/stages/test`) は URL エンコードされている必要があります (`arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest`)。

```
PUT /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest

{
  "tags" : {
    "Department" : "Sales"
  }
}
```

 前回のリクエストを使用して既存のタグを新しい値で更新することもできます。

[https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html) を呼び出してステージを作成するときに、ステージにタグ付けできます。

```
POST /restapis/<restapi_id>/stages

{
  "stageName" : "test",
  "deploymentId" : "adr134",
  "description" : "test deployment",
  "cacheClusterEnabled" : "true",
  "cacheClusterSize" : "500",
  "variables" : {
    "sv1" : "val1"
  },
  "documentationVersion" : "test",

  "tags" : {
    "Department" : "Sales",
    "Division" : "Retail"
  }
}
```

### API ステージのタグ解除
<a name="untag-a-stage-using-api"></a>

 ステージから `Department` タグを削除するには、[https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html) を呼び出します。

```
DELETE /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest?tagKeys=Department
Host: apigateway.us-east-1.amazonaws.com
Authorization: ...
```

 複数のタグを削除するには、クエリ式でタグキーのカンマ区切りのリストを使用します (例: `?tagKeys=Department,Division,…`)。

### API ステージのタグを説明する
<a name="get-tags-using-api"></a>

特定のステージで既存のタグを記述するには、[https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html](https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html) を呼び出します。

```
GET /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags
Host: apigateway.us-east-1.amazonaws.com
Authorization: ...
```

正常に終了すると、レスポンスは以下のようになります。

```
200 OK

{
    "_links": {
        "curies": {
            "href": "http://docs.aws.amazon.com/apigateway/latest/developerguide/restapi-tags-{rel}.html",
            "name": "tags",
            "templated": true
        },
        "tags:tag": {
            "href": "/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags"
        },
        "tags:untag": {
            "href": "/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags{?tagKeys}",
            "templated": true
        }
    },
    "tags": {
        "Department": "Sales"
    }
}
```