CredentialはローカルのProfieから読み取る形になってます。
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import boto3
import time
# Build the client using the default credential configuration.
# You can use the CLI and run 'aws configure' to set access key, secret
# key, and default region.
client = boto3.client("opensearchserverless")
service = "aoss"
region = "ap-northeast-1"
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
region,
service,
session_token=credentials.token,
)
host = "xxxxxxxxxxxx.ap-northeast-1.aoss.amazonaws.com"
index_name = "sitcoms-eighties"
def indexData(host):
"""Create an index and add some sample data"""
# Build the OpenSearch client
client = OpenSearch(
hosts=[{"host": host, "port": 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
timeout=300,
)
# It can take up to a minute for data access rules to be enforced
time.sleep(5)
if not client.indices.exists(index=index_name):
# Create index
response = client.indices.create(index_name)
print("\nCreating index:")
print(response)
# Add a document to the index.
response = client.index(
index="sitcoms-eighties",
body={"title": "Seinfeld", "creator": "Larry David", "year": 1989},
id="1",
)
print("\nDocument added:")
print(response)
def main():
indexData(host)
if __name__ == "__main__":
main()
参考
https://docs.aws.amazon.com/ja_jp/opensearch-service/latest/developerguide/serverless-sdk.html
