We have an API endpoint (served under AWS AppSync) which should query records from DB and make some manipulations. This endpoint accepts frew parameters and queried from DB records should match all those 4 parameters. As far as I unerstand, dynamodb can only query records by indexes, using
KeyConditionExpression. Since I have to match 4 fields in row, I should additionally use
FilterExpression. But as far as I understand, I will got perofmance issues because it will select all records that match KeyConditionExpression and only then will filter them. So my idea is to put additional field in the record which would be the hash of those 4 fields which I should match while searching this records. Currenlty the records is saved using AppSync so I have only response and requests vtl templates. I doubt I can put a logic for generating hash in VTL tempalte so I have to do this in lamdba function somehow. Here I have few options:
- I can make a DynamoDb trigger - a lambda function which will be triggered each time any record will be added/updated. This function will update those record with hash than.
- in serverless.yml in mappingTemplates I can set up a dataSource type to be of type: AWS_LAMBDA instead of AMAZON_DYNAMODB and point it to lambda function which would calculate record hash and puit it in database manually.
- I can put hash calculation logic in VTL template somehow (I have no idea how, because this calculation would require using crypto library which is not clear how to use in VTL)
- some kind of group index in dynamoDB?
What is the proper solution for such a task?