Direct Lambda Resolvers - AWS AppSync

Direct Lambda Resolvers

Direct Lambda Resolvers

With Direct Lambda Resolvers, you can circumvent the use of VTL mapping templates when using AWS Lambda data sources. AppSync can provide a default payload to your Lamdba 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.

Add a Lambda data source

Before you can activate Direct Lambda resolvers, you first have to add a Lambda data source.

To add a Lambda data source

  1. Sign in to the AWS Management Console and open the AWS AppSync Console.

  2. In the navigation pane, choose Data Sources, and then choose Create data source.

    1. For Data source name, enter a name for your data source, such as myFunction.

    2. For Data source type, choose AWS Lambda function.

    3. For Region, choose the appropriate region.

    4. For Function ARN, choose the Lamdba function from the dropdown list. You can search for the function name or manually enter the ARN of the function you want to use.

    5. Choose the Create button.

  3. 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 attaching a data source.

    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:*" ] } ] }

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.

To link a Lambda data source to a resolver or pipeline function

  1. Sign in to the AWS Management Console and open the AWS AppSync Console.

  2. In the navigation pane, choose Schema.

  3. In the Resolvers section, choose a field or operation and then select the Attach button.

  4. In Create new resolver, choose the Lambda function from the dropdown list.

  5. In order to leverage Direct Lambda resolvers, confirm that request and response mapping templates are disabled in the Configure mapping templates section.

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.