2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenSearch Serverless へのアクセス方法(VPC エンドポイント + SigV4)

Posted at

本記事では、Amazon OpenSearch Serverless に対して
VPC エンドポイント経由でアクセス
する方法を解説します。

OpenSearch Serverless は AWS SigV4 認証が必須のため、
通常の curl ではなく、SigV4 に対応したクライアントを利用する必要があります。

今回は EC2(Amazon Linux 2023) から Python を使ってアクセスします。

前提条件

  • OpenSearch Serverless の コレクションが作成済み
  • ネットワークポリシーで VPC エンドポイント経由のアクセスを許可
  • EC2 に 適切な IAM ロール が付与されている(aoss:APIAccessAll など)

1. OpenSearch Serverless のエンドポイントを確認する

まずは AWS CLI でコレクション情報を取得します。

aws opensearchserverless list-collections --region ap-northeast-1

出力結果の collectionEndpoint が、今回アクセスするエンドポイントです。

2. アクセス方法の概要

OpenSearch Serverless では以下が必要です。

  • 認証方式:AWS SigV4
  • サービス名:aoss
  • 認証情報:EC2 インスタンスプロファイル(IAM ロール)

そのため、以下のライブラリを使います。

  • boto3
  • requests
  • requests-aws4auth

3. 事前準備(Python 環境)

Python / pip のインストール

Amazon Linux 2023 では Python 3 が標準で利用できます。

sudo yum install -y python3 python3-pip

必要な Python ライブラリをインストール

python3 -m pip install --user boto3 requests requests-aws4auth

4. OpenSearch Serverless にアクセスする Python スクリプト

以下のスクリプトは、

  • EC2 の IAM ロールから一時クレデンシャルを取得
  • SigV4 で署名
  • OpenSearch Serverless に GET リクエストを送信

という流れになっています。

cat > /tmp/opensearch_access.py << 'EOF'
import sys
import boto3
import requests
from requests_aws4auth import AWS4Auth

endpoint = sys.argv[1]
region = sys.argv[2]
path = sys.argv[3] if len(sys.argv) > 3 else "/_cat/indices?v"

# EC2 インスタンスプロファイルの認証情報を取得
credentials = boto3.Session().get_credentials()

auth = AWS4Auth(
    credentials.access_key,
    credentials.secret_key,
    region,
    'aoss',
    session_token=credentials.token
)

response = requests.get(f"{endpoint}{path}", auth=auth)
print(response.text)
sys.exit(0 if response.status_code == 200 else 1)
EOF

5. スクリプトを実行して接続確認

ENDPOINT="<OpenSearch Serverless のエンドポイント>"

python3 /tmp/opensearch_access.py "$ENDPOINT" "ap-northeast-1"

実行結果例

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

現時点ではインデックスが存在しないため、_cat/indices の ヘッダーのみが表示されています。

👉 この出力が得られていれば、アクセスは成功しています。

まとめ

  • OpenSearch Serverless は SigV4 認証が必須
  • VPC エンドポイント経由でも IAM ロール + SigV4 で問題なくアクセス可能
  • requests-aws4auth を使うと Python から簡単に検証できる
  • ネットワーク疎通と IAM 権限の切り分けにも有効

OpenSearch Serverless への疎通確認や、運用スクリプトのベースとしてもそのまま使える構成です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?