Can not override DynamoDB endpoint for Java Kinesis Consumer

635 views Asked by At

I Can not set up my local environment through java-aws-sdk, localstack and java-aws-kcl. After creating the consumer and trying to run it on my local environment I am getting an error that my credentials are incorrect. So Kinesis consumer always go to the real Amazon DynamoDB, and I can not point it to my localstack dynamodb. The question is: how can I point it to my local dynamodb?

Any suggestions?

1

There are 1 answers

0
Tejas Garde On

You will have to connect your Kinesis and DynamoDB client to localstak. For this you will need the edgePort of localStack , Host Name . and form a Local Stack edge port URL . Here how it should be

public class public class Host {
    private static final String LOCAL = "localhost";
    public static final String DOCKER = "docker-desktop";

    public String getName(){
        ProcessBuilder processBuilder =
            new ProcessBuilder()
                .command("docker", "run", "--net", "host", "bash", "-c", "hostname", "-I");

        String hostname = null;
        try {
          Process process = processBuilder.start();
          BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
          hostname = reader.readLine().strip();
          int exitVal = process.waitFor();
          } catch (Exception e) {
            // log for exception
          }
        
         if (DOCKER.equals(hostname)) {
            return LOCAL_HOST;
        } else {
          return hostname;
        }
    }
}

now you need to get the host name and create URL something like this .

String localStackUrl =
        String.format("http://%s:%s", new Host().getName(), edgePort);

and then create clients as follows

SdkHttpClient httpClient = ApacheHttpClient.builder()
                                           .maxConnections(50)
                                           .build();

KinesisAsyncClient client =
        KinesisAsyncClient.builder()
            .region(Region.of(Localstack.getDefaultRegion()))
            .httpClient(httpClient)
            .credentialsProvider(provider)
            .endpointOverride(new URI(localStackUrl))
            .build();

  DynamoDbAsyncClient  dynamoClient =
        DynamoDbAsyncClient.builder()
            .region(Region.of(Localstack.getDefaultRegion()))
            .httpClient(httpClient)
            .credentialsProvider(provider)
            .endpointOverride(new URI(localStackUrl))
            .build();