Note
We now primarily support the APPSYNC_JS runtime and its documentation. Please consider using the APPSYNC_JS runtime and its guides here.
With direct Lambda resolvers, you can circumvent the use of VTL mapping templates when using AWS Lambda data sources. AWS AppSync can provide a default payload to your Lambda function as well as a default translation from a Lambda function's response to a GraphQL type. You can choose to provide a request template, a response template, or neither and AWS AppSync will handle it accordingly.
To learn more about the default request payload and response translation that AWS AppSync provides, see the Direct Lambda resolver reference. For more information on setting up an AWS Lambda data source and setting up an IAM Trust Policy, see Attaching a data source.
Configure direct Lambda resolvers
The following sections will show you how to attach Lambda data sources and add Lambda resolvers to your fields.
Add a Lambda data source
Before you can activate direct Lambda resolvers, you must add a Lambda data source.
-
Sign in to the AWS Management Console and open the AppSync console
. -
In the APIs dashboard, choose your GraphQL API.
-
In the Sidebar, choose Data sources.
-
-
Choose Create data source.
-
For Data source name, enter a name for your data source, such as
myFunction
. -
For Data source type, choose AWS Lambda function.
-
For Region, choose the appropriate region.
-
For Function ARN, choose the Lambda function from the dropdown list. You can search for the function name or manually enter the ARN of the function you want to use.
-
Create a new IAM role (recommended) or choose an existing role that has the
lambda:invokeFunction
IAM permission. Existing roles need a trust policy, as explained in the Attaching a data source section.The following is an example IAM policy that has the required permissions to perform operations on the resource:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:invokeFunction" ], "Resource": [ "arn:aws:lambda:us-west-2:123456789012:function:myFunction", "arn:aws:lambda:us-west-2:123456789012:function:myFunction:*" ] } ] }
-
-
Choose the Create button.
Activate direct Lambda resolvers
After creating a Lambda data source and setting up the appropriate IAM role to allow AWS AppSync to invoke the function, you can link it to a resolver or pipeline function.
-
Sign in to the AWS Management Console and open the AppSync console
. -
In the APIs dashboard, choose your GraphQL API.
-
In the Sidebar, choose Schema.
-
-
In the Resolvers window, choose a field or operation and then select the Attach button.
-
In the Create new resolver page, choose the Lambda function from the dropdown list.
-
In order to leverage direct Lambda resolvers, confirm that request and response mapping templates are disabled in the Configure mapping templates section.
-
Choose the Save Resolver button.
When you disable your mapping templates, there are several additional behaviors that will occur in AWS AppSync:
-
By disabling a mapping template, you are signalling to AWS AppSync that you accept the default data translations specified in the Direct Lambda resolver reference.
-
By disabling the request mapping template, your Lambda data source will receive a payload consisting of the entire Context object.
-
By disabling the response mapping template, the result of your Lambda invocation will be translated depending on the version of the request mapping template or if the request mapping template is also disabled.