Invoking lamdba from AppSync

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:

  1. 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.
  2. 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.
  3. 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)
  4. some kind of group index in dynamoDB?

What is the proper solution for such a task?

When you write the record you could add an additional attribute that contains those four values joined by a separator. You would repeat the same process inside your Lambda to get the key to find the record. You don’t need to use a true hash function.

it seems like a really good solution in out case and can be done in request VTL template. But what if there would be much more complicated logic during record insertion???

If you can’t solve it with VTL and DynamoDB then use Lambda