Create AWS AppSync Data Source And Resolver With AWS CDK

The guide demonstrates how to create AWS data source and resolver with AWS CDK

Sunday, October 27, 2019

This guide is part of Managing AWS AppSync With AWS CDK guide.
API & Schema
>> Data Source & Resolver
IAM Authentication

Lambda Function

Before creating the data source and resolver, let's create the underlying lambda function.

This simple function body returns given number number replicated count times. Following is the CDK definition for lambda resource

The creation of the lambda resource is not enough. We need to grant invoke access role of the lambda function to our AppSync API created in the previous post.

Lambda Function Invoke Role

The created role is defined to be assumed by the AppSync service. The attached policy allows invocation of the output lambda function referenced by its ARN. Now that we defined the invocation role we can proceed to the creation of the data source and resolver.

Data Source

Data source is set to the type of the AWS_LAMBDA and we provided the lambda function ARN and service role to assume when invoking the output lambda function.

Resolver

fieldName needs to match the query operation defined in the GraphQL schema. Request mapping template transforms the GraphQL arguments into the JSON body payload to our output lambda function. Response mapping template just returns the lambda result as it matches the GraphQL schema definition.

Closing Notes

We defined AWS AppSync data source and related resolver. In next section we will define the IAM authentication of the API. Further information about configuration of the data source and resolvers can be found at
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-appsync.CfnDataSource.html
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-appsync.CfnResolver.html