Trial #21: DynamoDB database not found “Amazon.DynamoDBv2.Model.ResourceNotFoundException”

1 minute read

Problem:

Following a basic AWS SDK for .NET setup as per Matthew Harper ‘s helpful beginner tutorial My program was throwing an “Amazon.DynamoDBv2.Model.ResourceNotFoundException”.

The following line turned out to be at fault blame:

AmazonDynamoDBClient client = 
    new AmazonDynamoDBClient(credentials);

When instantiating the AmazonDynamoDBClient, if no Region is passed to the constructor then it defaults to us-west-1, at least in my case when I tested 10 times in a row. This is not the region I created my DynamoDB instance or the Region in my profile. So when when I ran the

aws dynamodb list-tables --profile Craig

My table was listed e.g.

{
    "TableNames": [
        "GregorianToSolarLunar"
    ]
}

Solution:

Tell the AmazonDynamoDBClient what region your instance is in.

In the constructor using an enum:

    var region = 
        Amazon.RegionEndpoint.USEast2;
        
    AmazonDynamoDBClient client = 
        new AmazonDynamoDBClient(credentials, region);

In the constructor using your profile value:

    AmazonDynamoDBClient client = 
        new AmazonDynamoDBClient(credentials, profile.Region);

In the constructor using a AmazonDynamoDBConfig object. Particularly, its properties RegionEndpoint or ServiceURL if querying a local test instance:

    AmazonDynamoDBConfig clientConfig = 
        new AmazonDynamoDBConfig();

    clientConfig.ServiceURL = 
        "http://localhost:8000";

    AmazonDynamoDBClient client = 
        new AmazonDynamoDBClient(credentials, clientConfig )

Or preferably, once you have proof of concept, get all your config in a configuration manager. AWS give an example using an app.config file on their code examples page. However, a more complete and appropriate approach is documented in Gary Woodfine’s article “Simple Dependency Injection In AWS Lambda .net core”.

Up-Date: 24/9/2020

I found the following resources from Steve Gordon very helpful when working adding AWS Services to my dependencies container. I will attempt to combine what I have learned in a method or template in a later post.

Updated:

Leave a comment