I'm running AWS DynamoDB Local docker image and I'm getting the following error when I execute CreateTableAsync
. What's wrong with it?
The number of attributes in key schema must match the number of attributes defined in attribute definitions.
version: '3.9'
name: sts-firm-service
services:
dynamodb-local:
container_name: dynamodb-local
image: amazon/dynamodb-local:latest
ports:
- 8000:8000
networks:
- webnet
networks:
webnet:
driver: bridge
public interface IDynamoService
{
Task CreateTableAsync();
Task<List<string>> GetAllTablesAsync();
}
public sealed class DynamoService : IDynamoService
{
private readonly AmazonDynamoDBClient _client;
public DynamoService(IConfiguration configuration)
{
_client = new AmazonDynamoDBClient(
new BasicAWSCredentials(configuration["DynamoDB:AccessKey"], configuration["DynamoDB:SecretKey"]),
new AmazonDynamoDBConfig
{
ServiceURL = "http://localhost:8000",
AuthenticationRegion = "eu-central-1"
});
}
public Task CreateTableAsync()
{
var bookStructureTableRequest = new CreateTableRequest
{
TableName = "BookStructure",
ProvisionedThroughput = new ProvisionedThroughput
{
ReadCapacityUnits = 1,
WriteCapacityUnits = 1
},
KeySchema = new List<KeySchemaElement>
{
new()
{
AttributeName = "Id",
KeyType = KeyType.HASH
}
},
AttributeDefinitions = new List<AttributeDefinition>
{
new()
{
AttributeName = "Id",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "AccountExternalId",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "AccountLabel",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "BookLabel",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "Counterparty",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "LegalEntity",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "SecretPath",
AttributeType = ScalarAttributeType.S
},
new()
{
AttributeName = "StrategyName",
AttributeType = ScalarAttributeType.S
}
}
};
return _client.CreateTableAsync(bookStructureTableRequest);
}
public async Task<List<string>> GetAllTablesAsync()
{
var tables = await _client.ListTablesAsync();
return tables.TableNames;
}
}
public class BookStructure
{
public required string Id { get; init; }
public required string AccountExternalId { get; init; }
public required string AccountLabel { get; init; }
public required string BookLabel { get; init; }
public required string Counterparty { get; init; }
public required string LegalEntity { get; init; }
public required string SecretPath { get; init; }
public required string StrategyName { get; init; }
}
CodePudding user response:
Don't include non key attributes in the attributes definition. Dynamodb is schemaless you don't need those -- only what will be the key