Tim A
Tim A

Reputation: 43

How to configure client? AWS Elasticsearch request C#

I am new to Amazon Web Services. I configured domain to use ElasticSearch in AWS(Amazon Web Services) console. Confirured usage of Http Requests. Went through documantation of creating ElasticSearch client from https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/security.html

var response = client.RootNodeInfo(c => c
    .RequestConfiguration(rc => rc
        .BasicAuthentication("UserName", "Password")
    ));

Works fine to me (Response is 200) But when i try to configure authentication credentials like this and pass config to client constructor i need to have "cloudId" i didnt find in at AWS where sould i search for it? or what i have to do?

My client code:

BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("UserName", "Password");

var config = new ConnectionSettings("cloudId???", credentials);

var client = new ElasticClient(config);
var response = client.Ping();

Upvotes: 3

Views: 2605

Answers (1)

Jim
Jim

Reputation: 121

I recently did this but a different way. I used the Nuget package AwsSignatureVersion4 and an IAM user with appropriate permissions to the ElasticSearch service.

But basically, use the ImmutableCredentials and just do what I need to do via the REST calls and the C# HttpClient. I find it easier than using the .NET ElasticSearch library. I can then copy/paste back and forth from Kibana.

var credentials = new ImmutableCredentials("access_key", "secret_key", null);
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(someObjOrQuery), Encoding.UTF8);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var resp = httpClient.PostAsync(es_url, 
    httpContent, 
    regionName: "us-east-1",
    serviceName: "es",
    credentials: credentials).GetAwaiter().GetResult();

if(resp.IsSuccessStatusCode) 
{
   //Good to go
}
else
{
  //this gets what ES sent back
  var content = response.Content.ReadAsStringAsync();
  dynamic respJson = JObject.Parse(content.Result());
  //Now you can access stuff by dot and it's dynamic respJson.something
}

Upvotes: 3

Related Questions