Unable to access DynamoDb Locally from Kotlin

Viewed 284

First I want to say I am pretty new to Kotlin and DynamoDB. I am writing a sample program in Kotlin to play with DynamoDb. I am following the steps in this link: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/examples-dynamodb-tables.html

First I instantiate a client object for making requests to DynamoDB

val dynamoDbClient = DynamoDbClient { region = "us-east-1" }

Then I run the code below to create a new table.

    suspend fun createNewTable(ddb: DynamoDbClient, newTableName: String, key: String): String {

    val attDef = AttributeDefinition {
        attributeName = key
        attributeType = ScalarAttributeType.S
    }
    val keySchemaVal = KeySchemaElement {
        attributeName = key
        keyType = KeyType.Hash
    }
    val provisionedVal = ProvisionedThroughput {
        readCapacityUnits = 10
        writeCapacityUnits = 10
    }
    val request = CreateTableRequest {
        attributeDefinitions = listOf(attDef)
        keySchema = listOf(keySchemaVal)
        provisionedThroughput = provisionedVal
        tableName = newTableName
    }
    try {
        val response = ddb.createTable(request)
        val tableActive = false
        // Wait until the table is in Active state.
        while (!tableActive) {
            val tableStatus = checkTableStatus(ddb, newTableName)
            if (tableStatus.equals("ACTIVE"))
                break
            delay(500)
        }
        return response.tableDescription?.tableArn.toString()
    } catch (e: DynamoDbException) {
        println("ERROR (DynamoDbException): " + e.message)
    } catch (e: UnknownServiceErrorException) {
        println("ERROR (UnknownServiceErrorException): " + e.message)
    } finally {
        ddb.close()
    }
    return ""
}

I can see the table created on my AWS account. However I want to modify my DynamoDbClient to execute the table creation on a local instance of the DynamoDB. I followed the instructions from AWS pages and I installed DynamoDB locally.

Here is how I am running it locally:

c:\code\dynamodb_local_latest>java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
Initializing DynamoDB Local with the following configuration:
Port:   8000
InMemory:       false
DbPath: null
SharedDb:       true
shouldDelayTransientStatuses:   false
CorsParams:     *

I am able to access the local DynamoDB instance from the aws cli tool.

In order to try accessing the local instance of DynamoDB from the Kotlin code, I changed DynamoDbClient from this:

val dynamoDbClient = DynamoDbClient { region = "us-east-1" }

to this:

val endpoint = aws.sdk.kotlin.runtime.endpoint.Endpoint( "localhost",
    "http",
    port=8000,
    false,
    null,
    "us-west-1")
val myEndpointResolver = StaticEndpointResolver(endpoint)
val dynamoDbClient = DynamoDbClient {endpointResolver = myEndpointResolver; region ="us-west-1" }

However I get the following error:

Exception in thread "DefaultDispatcher-worker-1" software.amazon.awssdk.crt.http.HttpException: socket connection refused.
    at software.amazon.awssdk.crt.http.HttpClientConnection.onConnectionAcquired(HttpClientConnection.java:85)

What is the proper way to resolve that?

Thanks!

1 Answers

You can specify the EndPointResolver for the DynamoDbClient with the address of your DynamoDb local instance. For example:

class LocalHostDynamoDb: AwsEndpointResolver {
    override suspend fun resolve(service: String, region: String): AwsEndpoint
        = AwsEndpoint("http://localhost:8000")
}

class MyClient {
    ...
    val dynamoDbClient = DynamoDbClient {
        region = awsRegion
        endpointResolver = LocalHostDynamoDb()
    }
    ...
Related