It is just a wrapper for java version, provided by AWS.
- Download and configure AWS CLI V2
- Download DynamoDBLocal
- Put content of the folder in SolutionRootDir/dynamodb_local_latest or use environment variable, for example DYNAMODB_LOCAL_LATEST_COMMON_PATH
-
Example (also see DynamoDbLocalInMemoryBaseFixture and DynamoDbLocalInMemoryFixture)
public class DynamoDbLocalInMemoryFixtureTest: IClassFixture<DynamoDbLocalInMemoryFixture> { private const string TableName = "Example"; private readonly DynamoDbLocalInMemoryFixture _dynamoDbLocalInMemoryFixture; public DynamoDbLocalInMemoryFixtureTest(DynamoDbLocalInMemoryFixture dynamoDbLocalInMemoryFixture) { _dynamoDbLocalInMemoryFixture = dynamoDbLocalInMemoryFixture; _dynamoDbLocalInMemoryFixture.Create(); } [Fact] public async Task CreateTable_And_PutOneItem_In_TheTable_Using_DynamoDBClient() { await _dynamoDbLocalInMemoryFixture.SetupAsync(); var dynamoDbClient = _dynamoDbLocalInMemoryFixture.DynamoDbClient; var putItemResponse = await dynamoDbClient.PutItemAsync(new PutItemRequest { TableName = TableName, Item = GetTestItem() }); Assert.Equal(HttpStatusCode.OK, putItemResponse.HttpStatusCode); var scanResponse = await dynamoDbClient.ScanAsync(new ScanRequest { TableName = TableName }); Assert.Equal(HttpStatusCode.OK, scanResponse.HttpStatusCode); Assert.Equal(1, scanResponse.Items.Count); await _dynamoDbLocalInMemoryFixture.DestroyAsync(); } private Dictionary<string, AttributeValue> GetTestItem() { Dictionary<string, AttributeValue> attributes = new (); attributes["Id"] = new AttributeValue { S = Guid.NewGuid().ToString() }; attributes["CreatedDate"] = new AttributeValue { S = DateTimeOffset.UtcNow.ToString() }; attributes["Content"] = new AttributeValue { S = "aws_dynamodb_local_test" }; return attributes; } }
-
Make sure to always kill a running process at the end and first verify, on any error, there is no active dynamodb java processes (task manager)
dynamoDbProcess.Kill(true);
-
Make sure DynamoDBLocal and DynamoDBClient both are using the same port, otherwise the client may not be able to find the runnig java process. Read more...
-
Alternative usage:
private void CreateTable_UsingCommand() { // create using environment variable using var dynamoDbProcess = DynamoDbLocalWrapper.CreateInMemoryDbProcess(environmentVariableName: "DYNAMODB_LOCAL_LATEST_COMMON_PATH"); // or put dynamodb_local_latest folder inside the solution root folder // using var dynamoDbProcess = DynamoDbLocalWrapper.CreateInMemoryDbProcess(); dynamoDbProcess.Start(); var input = @"aws dynamodb create-table --table-name Example --attribute-definitions AttributeName=Id,AttributeType=S AttributeName=CreatedDate,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH AttributeName=CreatedDate,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 --endpoint-url http://localhost:8000 --region test"; using var createTableProcess = DynamoDBLocalWrapper.CreateProcess(input); createTableProcess.Start(); createTableProcess.Kill(true); // do something else dynamoDbProcess.Kill(true); }
-
SharedDb:
private void CreateSharedDb() { // create using environment variable using var dynamoDbProcess = DynamoDbLocalWrapper.CreateSharedDbProcess(environmentVariableName: "DYNAMODB_LOCAL_LATEST_COMMON_PATH"); // or put dynamodb_local_latest folder inside the solution root folder // using var dynamoDbProcess = DynamoDbLocalWrapper.CreateSharedDbProcess(); dynamoDbProcess.Start(); using var dynamoDbClient = DynamoDbLocalWrapper.CreateClient(); // do something dynamoDbProcess.Kill(true); }