CredentialはローカルのProfieから読み取る形になってます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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