Published in

AppSync: how to compare strings lexicographically in VTL

I had been working on a client project where I built the GraphQL backend for a new social network using primarily AppSync, Lambda and DynamoDB.

One of the features of the app was to support private messaging between two users. To allow either user to fetch their chat messages in chronological descending order, I modelled the chat_messages DynamoDB table as below:

where userIds is the HASH key, and timestamp is the RANGE key.

Here, userIds is a composite key made up of the two users’ IDs, arrange in lexicographical order. If you know the two users’ IDs, you can fetch the chat messages using a DynamoDB Query operation.

So in the VTL template, I had something like this:

However, this didn’t work.

Given the following input:

The $userIds variable would be interpreted as 35c1f714–46c1–4ccb-87fc-933da626ce6a_08371580–48e2–43e7–9b1c-75804b9e7e12, where the user IDs are arranged in the wrong order. What’s going on?

If you work with Java everyday then the problem might be obvious to you, that to compare strings lexicographically you need to use the compareTo method on the first string. And since VTL is implemented in Java, that is also the case here.

Instead, the right way to do it is this:

I have found the VTL Reference page very handy while working with VTL. But unfortunately, lexicographical string comparison was not mentioned in the VTL docs. Without helpful folks like Richard and Franck I would have easily spent hours banging my head against this problem!

I hope this post helps you in a similar way should you run into the same problem, as my way of passing it forward.

Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.

Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!

Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!

Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you

Originally published at on May 1, 2020.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yan Cui

Yan Cui

Helping companies go faster for less with serverless. AWS Serverless Hero.