Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS AppSync Resolvers Lambda Function vs Velocity Template Language (VTL)

I have been looking into AWS AppSync to create a managed GraphQL API with DynamoDB as the datastore. I know AppSync can use Apache Velocity Template Language as a resolver to fetch data from dynamoDB. However, that means I have to introduce an extra language to the programming stack, so I would prefer to write the resolvers in Javascript/Node.js

Is there any downside of using a lambda function to fetch data from DynamoDB? What reasons are there to use VTL instead of a lambda for resolvers?

like image 522
Punisher Avatar asked May 19 '20 09:05

Punisher


1 Answers

There are pros and cons to using lambda functions as your AppsSync resolvers (although note you'll still need to invoke your lambdas from VTLs):

Pros

  • Easier to write and maintain
  • More powerful for marshalling and validating requests and responses
  • Common functionality can be more DRY than possible with VTLs (macros are not supported)
  • More flexible debugging and logging
  • Easier to test
  • Better tooling and linting available
  • If you need to support long integers in your DynamoDB table (DynamoDB number types do support long, but AppSync resolvers only support 32-bit integers. You can get around this if you use a lambda, for example by serializing longs to a string before transport through the AppSync resolver layer) - See (currently) open Feature Request: https://github.com/aws/aws-appsync-community/issues/21

Cons

  • Extra latency for every invocation
  • Cold starts = even more latency (although this can usually be minimised by keeping your lambdas warm if this is a problem for your use case)
  • Extra cost
  • Extra resources for each lambda, eating up the fixed 200 limit

If you're doing a simple vanilla DynamoDB operation it's worth giving VTLs a go. The docs from AWS are pretty good for this: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html

If you're doing anything mildly complex, such as marshalling fields, looping, or generally hacky non-DRY code, then lambdas are definitely worth considering for the speed of writing and maintaining your code provided you're comfortable with the extra latency and cost.

like image 146
Ben Avatar answered Oct 13 '22 22:10

Ben