Code examples for connecting to a OpenSearch cluster
Written by
Updated at March 5, 2026
Before connecting, prepare a certificate.
To connect, enter admin for the username and the password you set when creating the cluster.
To see code examples with the host FQDN filled in, open the cluster page in the management console
Go
Before connecting, install the required dependencies:
go mod init opensearch-example && \
go get github.com/opensearch-project/opensearch-go
Connecting with SSL
-
Code example:
connect.gopackage main import ( "crypto/tls" "crypto/x509" "crypto/x509" "github.com/opensearch-project/opensearch-go" "io/ioutil" "log" "net/http" ) var hosts = []string{ "<FQDN_of_host_1_with_DATA_role>:9200", ..., "<FQDN_of_host_N_with_DATA_role>:9200" } var CA = "/home/<home_directory>/.opensearch/root.crt" var password = "<password>" func main() { caCert, err := ioutil.ReadFile(CA) if err != nil { log.Fatal(err) } caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) cfg := opensearch.Config{ Addresses: hosts, Transport: &http.Transport{ TLSClientConfig: &tls.Config{ RootCAs: caCertPool, }, }, Username: "admin", Password: password, } es, err := opensearch.NewClient(cfg) if err != nil { log.Printf("Error creating the client: %s", err) } else { log.Println(es.Info()) } }Unlike other connection methods, in this example, you need to use the full path to the
CA.pemcertificate for OpenSearch in theCAvariable. -
Connecting:
go run connect.go
To learn how to get a host FQDN, see this guide.
Python
Before connecting, install the required dependencies:
sudo apt update && sudo apt install --yes python3 python3-pip && \
pip3 install opensearch-py
Connecting with SSL
-
Code example:
connect.pyfrom opensearchpy import OpenSearch CA = '~/.opensearch/root.crt' PASS = '<password>' HOSTS = [ "<FQDN_of_host_1_with_DATA_role>", ..., "<FQDN_of_host_N_with_DATA_role>" ] conn = OpenSearch( HOSTS, http_auth=('admin', PASS), use_ssl=True, verify_certs=True, ca_certs=CA) print(conn.info()) -
Connecting:
python3 connect.py
To learn how to get a host FQDN, see this guide.