AppSync Direct Go Lambda resolvers with DynamoDB
This project contains a sample CDK template for using Direct Lambda resolvers using Go with an AppSync API. Direct Lambda resolvers allows you to write resolver logic in a language of your choice and circumvent the use of VTL.
In the given pattern, AWS AppSync will provide a arguments from context payload to your AWS Lambda function and a default of a Lambda function’s response to a GraphQL type.
Requirements
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git installed
- Go installed
- Node and NPM installed
- AWS Cloud Development Kit (AWS CDK) installed
Architecture
To each Query and Mutation of Graphql in AppSyncApi, one Lambda Resolver is attachted. It handles a business logic of the request.
It’s defined as NoteService (Microservice) in Go. Each Lambda function has its own and single responsibility to interact with DynamoDB and it returns the reuslt back to AppSyncApi over ResultTemplateMapping.
In this project, you can learn both cdk example and go microserivce structure.
Deploy
- Clone the project to your local working directory
git clone https://github.com/deloittepark/aws-appsync-graphql-go-lambda-resolver.git
- Change the working directory to cdk’s directory
cd cdk
- Install dependencies
npm install
- This project uses typescript as client language for AWS CDK. Run the given command to compile typescript to javascript
npm run build
- Synthesize CloudFormation template from the AWS CDK app
cdk synth
- Deploy the stack to your default AWS account and region. The output of this command should give you the GraphQL URL and API Key for your AppSync API.
cdk deploy
Test
You can test your AppSync API and Direct Lambda resolver by running a query from AWS AppSync console.
You can run a query directly from your terminal or via postman:
# install curl. https://curl.se/
# optional: install jq. https://stedolan.github.io/jq/
# replace <graphqlUrl> and <apiKey> with the outputs values from `cdk deploy`
curl --location --request POST '<graphqlUrl>' \
--header 'x-api-key: <apiKey>' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query { listNotes }","variables":{}}' | jq
Cleanup
Run the given command to delete the resources that were created. It might take some time for the CloudFormation stack to get deleted. This will delete all deployed resources including cloudwatch lamdba log groups.
cdk destroy
References
-
https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-lambda.html
-
https://aws.amazon.com/blogs/mobile/building-scalable-graphql-apis-on-aws-with-cdk-and-aws-appsync/