Skip to content

Commit

Permalink
Adding example in user guide
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Jan 18, 2023
1 parent ebc794c commit 64e573a
Showing 1 changed file with 93 additions and 2 deletions.
95 changes: 93 additions & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
- [User Guide](#user-guide)
- [Example](#example)
- [How to use IAMs as authentication method](#how-to-use-iams-as-authentication-method)
- [Example](#example)
- [How to use IAMs as authentication method](#how-to-use-iams-as-authentication-method)
- [AWS SDK V1](#aws-sdk-v1)
- [AWS SDK V2](#aws-sdk-v2)
- [AWS SDK V2 with OpenSearch Serverless](#aws-sdk-v2-with-opensearch-serverless)

# User Guide

Expand Down Expand Up @@ -283,3 +286,91 @@ func main() {
}

```

#### AWS SDK V2 with OpenSearch Serverless

AWS SDK V2 for Go can be used for Amazon OpenSearch Serverless. To create a client, please see the example below:

```go
package main

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
opensearch "github.com/opensearch-project/opensearch-go/v2"
opensearchapi "github.com/opensearch-project/opensearch-go/v2/opensearchapi"
requestsigner "github.com/opensearch-project/opensearch-go/v2/signer/awsv2"
)

const endpoint = "" // serverless collection endpoint

func main() {
ctx := context.Background()

awsCfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion("<AWS_REGION>"),
config.WithCredentialsProvider(
getCredentialProvider("<AWS_ACCESS_KEY>", "<AWS_SECRET_ACCESS_KEY>", "<AWS_SESSION_TOKEN>"),
),
)
if err != nil {
log.Fatal(err) // Do not log.fatal in a production ready app.
}

// Create an AWS request Signer and load AWS configuration using default config folder or env vars.
signer, err := requestsigner.NewSignerWithService(awsCfg, "aoss") // "aoss" for Amazon OpenSearch Serverless
if err != nil {
log.Fatal(err) // Do not log.fatal in a production ready app.
}

// Create an opensearch client and use the request-signer
client, err := opensearch.NewClient(opensearch.Config{
Addresses: []string{endpoint},
Signer: signer,
})
if err != nil {
log.Fatal("client creation err", err)
}

IndexName = "go-test-index"

// Create an index with non-default settings.
createIndex := opensearchapi.IndicesCreateRequest{
Index: IndexName,
}
createIndexResponse, err := createIndex.Do(context.Background(), client)
if err != nil {
log.Println("Error ", err.Error())
log.Println("failed to create index ", err)
log.Fatal("create response body read err", err)
}
log.Println(createIndexResponse)

// Delete previously created index.
deleteIndex := opensearchapi.IndicesDeleteRequest{
Index: []string{IndexName},
}

deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
if err != nil {
log.Println("failed to delete index ", err)
log.Fatal("delete index response body read err", err)
}
log.Println("deleting index", deleteIndexResponse)
}

func getCredentialProvider(accessKey, secretAccessKey, token string) aws.CredentialsProviderFunc {
return func(ctx context.Context) (aws.Credentials, error) {
c := &aws.Credentials{
AccessKeyID: accessKey,
SecretAccessKey: secretAccessKey,
SessionToken: token,
}
return *c, nil
}
}

```

0 comments on commit 64e573a

Please sign in to comment.