14

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?

Punisher
  • 654
  • 5
  • 21

1 Answers1

25

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.

Ben
  • 1,759
  • 1
  • 19
  • 23
  • Any idea of how much more costs it adds to the stack? @ben – Diego Ponciano Sep 10 '21 at 20:41
  • 2
    @DiegoPonciano it depends on the frequency and complexity as AWS charges for lambdas based on a flat rate per invocation, plus per ms billing depending on how big an instance you need and how long it runs for. It also depends on how burstable you expect your load to be (more burstable means more cold starts or more cost keeping more lambdas warm). In general, if the complexity is there, the additional aws cost is going to be worth it compared to the pain (and time) you'll save yourself dealing with complex VTLs. See their pricing for current rates: https://aws.amazon.com/lambda/pricing/ – Ben Sep 10 '21 at 23:01