Deploying AppSync with Terraform

Introduction

Lucas Woodward

Deploying AppSync with Terraform

Deploying AppSync with Terraform

On the 28th November ’17 AWS introduced the world to AppSync, its new fully managed GraphQL service that bridges the gap between your front-end and data-ladened backend. Most notably its features include data synchronisation (no more polling!) and offline capabilities.

However long after its launch Terraform’s AWS Provider still doesn’t have all the resources necessary to deploy a complete instance of AppSync (although they are very close to being released). Leaving some developers turning to Serverless, manually intervening after deployments or using external scripts to make up for this short-fall.

The following shows how use Terraform’s cloudformation_stack resource to create the missing resources - removing the need for the aforementioned workarounds and making future migration a doddle.

What AppSync resources are supported

Currently the AWS Provider supports deploying the API, an authentication key and data sources, but if you actually want to resolve a request you’re going to need a Schema and Resolver, which are both missing.

CloudFormation to the rescue

Luckily Terraform’s AWS Provider offers us a lifeline for dealing with missing resources thanks to its aws_cloudformation_stack resource - allowing us to harness CloudFormation templates within Terraform.

In case you’ve not come across CloudFormation before it is AWS’s native infrastructure-as-code offering that - just like Terraform - allows you to automate the provisioning of AWS services via ‘templates’. Importantly though since it is owned by AWS it supports most (if not all) of their services.

Downsides

Unfortunately though little in life comes without a price and for us it’s that Terraform hands responsibility of creation, updating and destruction to CloudFormation with some unfortunate consequences:

Resource recreation due to minor changes - Updating the CloudFormation templates or its variables will likely lead to the resource being recreated

Manual intervention for some failures - It’s often a result of human error but if there is an error creating a CloudFormation resource - such as providing an invalid template value - then subsequent deploys might fail with a ROLLBACK_COMPLETE message. This is resolved by manually destroying the resource

No dependency inference - Terraform won’t be able to infer dependencies so you’ll likely depend on depends_on

Terraforming the missing pieces

The aws_cloudformation_stack resource has two main arguments, the CloudFormation template and its parameters. In the example below the templates (which can be seen here) define AppSync’s schema and resolver and are stored alongside the modules files. They can then be referenced using the local_file data source, reducing duplication between resolvers.

It’s then just a case of providing the parameters and any hidden dependencies via the depends_on meta-argument.