Skip to content

Commit 03b2bef

Browse files
committed
updates README to add usage for AWSV4SignerAuth
Signed-off-by: Shivam Dhar <dhshivam@amazon.com>
1 parent 6ab90be commit 03b2bef

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,58 @@ print('\nDeleting index:')
137137
print(response)
138138
```
139139

140+
## Using IAM credentials for authentication
141+
142+
Refer the AWS documentation regarding usage of IAM credentials to sign requests to OpenSearch APIs - [Signing HTTP requests to Amazon OpenSearch Service.](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/request-signing.html#request-signing-python)
143+
144+
Opensearch-py client library also provides an in-house IAM based authentication feature, `AWSV4SignerAuth` that will help users to connect to their opensearch clusters by making use of IAM roles.
145+
146+
#### Pre-requisites to use `AWSV4SignerAuth`
147+
- Python version 3.6 or above,
148+
- Install [botocore](https://pypi.org/project/botocore/) using pip
149+
150+
`pip install botocore`
151+
152+
Here is the sample code that uses `AWSV4SignerAuth` -
153+
154+
```python
155+
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
156+
import boto3
157+
158+
host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
159+
region = 'us-west-2'
160+
credentials = boto3.Session().get_credentials()
161+
auth = AWSV4SignerAuth(credentials, region)
162+
index_name = 'python-test-index3'
163+
164+
client = OpenSearch(
165+
hosts = [{'host': host, 'port': 443}],
166+
http_auth = auth,
167+
use_ssl = True,
168+
verify_certs = True,
169+
connection_class = RequestsHttpConnection
170+
)
171+
172+
q = 'miller'
173+
query = {
174+
'size': 5,
175+
'query': {
176+
'multi_match': {
177+
'query': q,
178+
'fields': ['title^2', 'director']
179+
}
180+
}
181+
}
182+
183+
response = client.search(
184+
body = query,
185+
index = index_name
186+
)
187+
188+
print('\nSearch results:')
189+
print(response)
190+
```
191+
140192
## Project Resources
141193

142194
* [Project Website](https://opensearch.org/)

0 commit comments

Comments
 (0)