

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# awscurl を使用して Prometheus 互換 API でクエリを実行する
<a name="AMP-compatible-APIs"></a>

Amazon Managed Service for Prometheus の API リクエストは、[SigV4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) で署名する必要があります。[awscurl](https://github.com/okigan/awscurl) を使用すると、クエリのプロセスを簡略化できます。

`awscurl` をインストールするには、Python 3 と pip パッケージマネージャーがインストールされている必要があります。

Linux ベースのインスタンスでは、次のコマンドで `awscurl` をインストールします。

```
$ pip3 install awscurl
```

macOS マシンでは、次のコマンドで `awscurl` をインストールします。

```
$ brew install awscurl
```

次の例は、サンプルの `awscurl` クエリです。*Region*、*Workspace-id*、*QUERY* の各入力は、ユースケースに応じた値に置き換えます。

```
# Define the Prometheus query endpoint URL. This can be found in the Amazon Managed Service for Prometheus console page 
# under the respective workspace. 

$ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace-id/api/v1/query

# credentials are infered from the default profile
$ awscurl -X POST --region Region \
                  --service aps "${AMP_QUERY_ENDPOINT}" -d 'query=QUERY'  --header 'Content-Type: application/x-www-form-urlencoded'
```

**注記**  
クエリ文字列は、URL エンコードする必要があります。

`query=up` などのクエリでは、次のような結果が得られます。

```
{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "__name__": "up",
          "instance": "localhost:9090",
          "job": "prometheus",
          "monitor": "monitor"
        },
        "value": [
          1652452637.636,
          "1"
        ]
      },
    ]
  }
}
```

指定したリクエストに `awscurl` で署名するには、有効な認証情報を以下のいずれかの方法で渡す必要があります。
+ IAM ロールのアクセスキー ID とシークレットキーを指定する。ロールのアクセスキーとシークレットキーは、[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) で確認できます。

  例えば、次のようになります。

  ```
  $ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query
  
  $ awscurl -X POST --region <Region> \
                    --access_key <ACCESS_KEY> \
                    --secret_key <SECRET_KEY> \
                    --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>"
  ```
+ `.aws/credentials` および `/aws/config` ファイルに保存されている設定ファイルを参照する。使用するプロファイルの名前を指定することもできます。指定しない場合、` default ` ファイルが使用されます。例えば、次のようになります。

  ```
  $ export AMP_QUERY_ENDPOINT=https://aps-workspaces.<Region>.amazonaws.com/workspaces/<Workspace_ID>/api/v1/query
  $ awscurl -X POST --region <Region> \
                    --profile <PROFILE_NAME> 
                    --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>"
  ```
+ EC2 インスタンスに関連付けられているインスタンスプロファイルを使用する。

## awscurl コンテナを使用したクエリリクエストの実行
<a name="awscurl-container"></a>

別のバージョンの **Python** がインストールされていて、関連する依存関係を満たすことができない場合は、コンテナを使用して `awscurl` アプリケーションとその依存関係をパッケージ化できます。次の例では **Docker** ランタイムを使用して `awscurl` をデプロイしますが、OCI 準拠の任意のランタイムとイメージを使用できます。

```
$ docker pull okigan/awscurl
$ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query
$ docker run --rm -it okigan/awscurl --access_key $AWS_ACCESS_KEY_ID  --secret_key $AWS_SECRET_ACCESS_KEY \ --region Region --service aps "$AMP_QUERY_ENDPOINT?query=QUERY"
```