Joey Yi Zhao
Joey Yi Zhao

Reputation: 42556

Why can't it see tables from dynamodb local container when using `aws-sdk-go-v2`?

I am launching a dynamodb docker container (amazon/dynamodb-local:1.16.0) running on my laptop. And I created a table on the instance via aws dynamodb create-table.

I am able to see the table from my local command line:

AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test aws dynamodb list-tables --region local-env --endpoint-url http://localhost:8000

{
    "TableNames": [
        "test"
    ]
}

But I can't find the table when using github.com/aws/aws-sdk-go-v2/service/dynamodb library from a go application.

The code I have in go is:

cfg, err := config.LoadDefaultConfig(context.TODO(),
        config.WithRegion("local-env"),
        config.WithEndpointResolver(aws.EndpointResolverFunc(
            func(service, region string) (aws.Endpoint, error) {
                return aws.Endpoint{URL: "http://localhost:8000"}, nil
            })),
        config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
            Value: aws.Credentials{
                AccessKeyID: "test", SecretAccessKey: "test",
            },
        }),
    )
    if err != nil {
        panic(err)
    }
    db := dynamodb.NewFromConfig(cfg)
    tables, _ :=  db.ListTables(context.TODO(), &dynamodb.ListTablesInput{})
    fmt.Println("tables", tables.TableNames)

// output is:
tables []

I am using the same endpoint, credential as the command line but don't understand what I did wrong.

Upvotes: 2

Views: 1319

Answers (1)

Thomas Steven
Thomas Steven

Reputation: 41

I ran into the same issue.

Initially I was starting the Dynamodb Docker container with this command

# Does not work
docker run -p 8000:8000 amazon/dynamodb-local

This would allow me to interact with Dynamo from the CLI, but when I interacted with Dynamo from the SDK, I could not see the tables I had created from the CLI.

Eventually I stumbled upon DynamoDB local usage notes. From there I realized I need to run a different command to start Dynamodb.

# This worked for me
docker run -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb

The difference is that we are telling Dynamo to use a shared database file.

I suspect what is happening is that connecting through the CLI uses my local AWS credentials and thus creates a bespoke database file. When I try to connect using the SDK, my credentials are not available, so Dynamo creates a new database file.

Upvotes: 3

Related Questions