/appsync-terraform-go-example

An example Terraform configuration to launch an AppSync service with a Go Lambda data source

Primary LanguageHCL

AppSync Terraform Go example

An example Terraform configuration to launch an AWS AppSync service with a Go Lambda data source.

This repo is the complete code example from a blog post I wrote called Easy AWS AppSync with Terraform.

Note: the Makefile may only run on macOS/linux, if you are on Windows you may need to build the Go lambda function another way.

About the contents of this repo:

  • Lambda functions are in the /functions directory
  • Apache Velocity resolver templates are inside the /resolvers directory
  • The GraphQL schema is in schema.graphql
  • All Terraform config is within the following 3 files:
    • variables.tf has variables used throughout the rest of the config
    • iam.tf contains AWS IAM roles and policies
    • main.tf contains the code for generating all other required services

Before publishing:

  1. Download Terraform
  2. Have an existing local AWS credentials profile (this is required so Terraform has the permissions to publish your AWS service)
  3. Have Go installed (required to run commands to build the Go lambda function)
  4. Open variables.tf and set them to whatever you want

Note: if you want your state to persist, remove the line from .gitignore that says *.tfstate. This state is essential to keep track of your AWS resources in order to successfully update or destroy them.

To publish:

Warning: this will fall within the free tier for AWS, however if you have had your account for longer than 1 year you may be charged a small amount for executing AppSync queries since the free tier for AppSync is only 12 months. If you still want to deploy it, the cost should be minimal (cents, not dollars).

  1. Run terraform init to initialize terraform and fetch all the resources
  2. Run make build to build the Go lambda function binary
  3. Run terraform plan to view the diff and all the created resources
  4. Run terraform apply to publish (it will prompt you to type "yes" to confirm)

Once this is done you can view your AppSync console and run queries to test that the Lambda data source is working!

To destroy

  1. Run terraform destroy to remove all AWS services (it will prompt for "yes" again)