AWS OpenSearch Service. Request failed: [security_exception] authentication/authorization failure

I’m trying to use OpenSearch to connect to my OpenSearch domain on AWS. I set env for AWS. However, I’m encountering an authentication/authorization failure. Here’s my code:

public class Example {

  public static void main(final String[] args) throws IOException {
    SdkHttpClient httpClient = ApacheHttpClient.builder().build();
    try {
      OpenSearchClient client =
          new OpenSearchClient(
              new AwsSdk2Transport(

      InfoResponse info =;
      System.out.println(info.version().distribution() + ": " + info.version().number());
    } finally {

When I run this code, I get the following exception:

Exception in thread "main" org.opensearch.client.opensearch._types.OpenSearchException: Request failed: [security_exception] authentication/authorization failureat Example.main(

I believe I have the correct permissions set up for my IAM user, and I’m using the correct access key and secret key. However, I still can’t seem to resolve this issue. What am I missing or doing wrong? Any help or guidance would be appreciated.

this is my aws config

Seems that the domain name in your IAM policy and the code are not consistent, you can check that.

1 Like