Code examples for connecting to a OpenSearch cluster
Written by
Updated at August 7, 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
Code example:
Connecting with SSL
connect.go
package 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.pem certificate for OpenSearch in the CA variable.
Learn how to get a host FQDN in this guide.
Connecting:
go run connect.go
Python
Before connecting, install the required dependencies:
sudo apt update && sudo apt install --yes python3 python3-pip && \
pip3 install opensearch-py
Code example:
Connecting with SSL
connect.py
from 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())
Learn how to get a host FQDN in this guide.
Connecting:
python3 connect.py