When you build modern cloud-based systems, you usually realize quickly that you need to manage the access to your deployed resources. This is especially true with serverless systems, where you often end up with dozens of resources, even for medium-sized architectures.
AWS offers a few services you can use to set up a central entry point to your infrastructure. Elastic Load Balancer, API Gateway, and AWS AppSync.
This article will discuss AppSync, AWS’s managed GraphQL service. Read on if you’re building a new backend or want to see if there is a more refined solution to the gateway problem than ELB and API Gateway.
What is AWS AppSync?
AppSync is a serverless GraphQL service. In simple terms, it’s just an API gateway based on the GraphQL specification you can pay on-demand.
GraphQL offers a friendly query language on top of HTTP, making it possible to fetch multiple different resources in one request, lowering network latency.
It’s higher level than Amazon API Gateway because it builds on HTTP and WebSockets but doesn’t expose these protocols directly to the user. Instead, it offers a data modeling language for queries, mutations, and subscriptions. This distinction makes it a good candidate for the Command Query Responsibility Segregation architectural pattern popularized by Domain-Driven Design.
These characteristics allow you to build real-time and offline applications without thinking about the heavy lifting in the background.
How Does AWS AppSync Work?
AppSync allows you to define a GraphQL schema and link every type to a resolver. You can implement these resolvers in the Velocity Templating Language (VTL) or, when using AWS Lambda as a resolver, in a programming language of your choice. Keep in mind that Lambda resolvers incur extra costs.
VTL is a rather templating language that maps one string to another. This way, you can transform the data coming from AppSync to fit an upstream service. While it takes a bit to get used to, VTL resolvers are much cheaper and have lower latency than Lambda resolvers.
Depending on your skills, you might want to start with Lambda resolvers and change them to VTL resolvers when you notice cost or latency problems.
You have to pay for every invocation of a Lambda resolver but get much more flexibility from them. You can implement them in all languages Lambda supports, and since they’re just Lambda functions, you can do whatever you want.
If you wish to link AppSync to a custom service or want to perform multiple tasks in one resolver, then VTL won’t cut it.
AWS Amplify CLI & Libraries
You would primarily use AppSync with the help of AWS Amplify, a set of libraries, and a command line interface.
The Amplify CLI can generate CloudFormation templates and client code based on a GraphQL schema. This way, you can deploy an API and DynamoDB tables and get an extensive client library with just a few lines of code. If you change the schema, Amplify CLI can automatically update the rest of your stack.
The client libraries come with support for subscriptions and abstractions built on them. The DataStore, for example, uses AppSync to let you create offline-enabled apps that sync with the cloud in real-time.
What are the Benefits of Using AWS AppSync?
AppSync allows you to build sophisticated apps without the need to set up or maintain your GraphQL server. You pay only for what you use, and AWS will keep the service up-to-date for you.
It comes with solutions for real-time and offline apps out-of-the-box. You can deliver these features to your users in a matter of hours instead of weeks.
Also, it gives you some exciting features like filtering subscription data in the backend to ensure your clients won’t get overwhelmed by real-time updates they don’t need.
And finally, when you use AppSync with Amplify, you can generate infrastructure templates and client libraries directly from your GraphQL schemas.
AWS AppSync vs. API Gateway
API Gateway is a more low-level service than AppSync. It exposes HTTP requests and WebSocket connections.
Before AppSync was released, you had to build a GraphQL API with API Gateway and Lambda. A Lambda function had to use a GraphQL server library, like Apollo, to do all the work.
If you wanted to use GraphQL subscriptions, you had to set up an API Gateway WebSockets API and build that subscription feature all by yourself with a Lambda function.
In the end, you would have much more implementation and maintenance work and even pay for the API Gateway and the Lambda functions.
On the other hand, if you want to build a simple API that doesn’t need many features and is all about performance, a well-designed API Gateway endpoint could be a better solution for your workload.
What does AWS AppSync Cost You?
AppSync is a 100% serverless service; it doesn’t cost you a dime if nobody uses it. You even get 250,000 requests a month for free with a new AWS account in your first 12 months.
Had an idea that didn’t get any customers? No risk with serverless!
Otherwise, AppSync is a bit more costly than API Gateway.
You pay $4 for 1 million queries and mutations and $2 for 1 million real-time updates via subscriptions. There is also a connection cost for the subscriptions: 1 million connection minutes cost $0.08.
AWS AppSync and AWS Amplify are good services to get your GraphQL API off the ground quickly with low maintenance costs in the future.
AppSync is more sophisticated than API Gateway but comes at a higher price.
If you want GraphQL-powered, real-time, offline enabled applications with code generators for infrastructure and client libraries, AppSync and Amplify are the way to go.
PS! Dashbird is launching AppSync integration very soon. If you want to be one of the first to hear about it, sign up for our newsletter.