Skip to content
Advertisement

How to create the dynamodb table using serverless.yml and delete the items of it using python boto3?

I’ve created the dynamodb table using serverless.yml as below:

JavaScript

But I’ve got this issue:

An error occurred: myTable – One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: PEI9OT7E72HQN4N5MQUOIUQ18JVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null).

Could you help me creating the dynamodb table using serverless.yml? And how can I delete the items that first name is “First” in this table using python boto3?

Advertisement

Answer

If you want to keep your KeySchema you have to drop lastname from AttributeDefinitions:

JavaScript

But if you want to keep lastname, you could define local secondary index for your table:

JavaScript

With the above, you can sort for lastname using the LSI, while firstname would be used in the primary table.

how can I delete the items that first name is “First” in this table using python boto3?

You can’t do it directly with or without boto3, unless you want to perform scan, which should be avoided as it can be expensive and is not efficient. To general solution is to define a Global Secondary Indexes where the firstname would be the new primary key. Then you would query the GSI for the firstname of interest to obtain the id of the record you want to delete. If you have several records with same firstname, which probably will be the case, you get a number of records back (GSI primary keys don’t need to be unique, unlike for the primary table).

Example table with LSI and GSI:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement