I'm debating between either creating a global boto3.Session
for my OpenSearch Python client or creating a new session at the beginning of every endpoint. I can't find documentation anywhere about when Sessions expire or if they expire. Every boto3.Session()
call is costing me 50ms-100ms in latency, so it'd be great if I could just create a global one and have it never expire. Any guidance would be greatly appreciated.
Context: I've set up a Load Balanced Web Service on a public subnet to connect to my OpenSearch cluster in a private subnet, both in the same VPC. The Load Balanced Web Service is a FastAPI application.
The boto3.Session
s don't expire unless your credentials do. So, maintaining one global session make absolutely sense.
I'm not sure how you code is structured but you can find a basic example below:
from fastapi import FastAPI
import boto3
app = FastAPI()
session = boto3.Session() # ← global reusable boto3 session
@app.get("/some-endpoint")
def some_endpoint():
client = session.client('opensearch')
# ...
return {"message": "Success"}