An AWS AppSync Lambda resolver is a type of resolver used in AWS AppSync, which is a managed service that makes it easy to develop GraphQL APIs by connecting your data sources and AWS services with GraphQL. In the context of AWS AppSync, a Lambda resolver is specifically designed to connect your GraphQL API to AWS Lambda functions. Here’s what you need to know about AWS AppSync Lambda resolvers:

Resolver in GraphQL:

In GraphQL, a resolver is responsible for fetching the data associated with a specific field in a GraphQL type. In the case of AWS AppSync, resolvers can be connected to various data sources, including AWS Lambda functions.

AWS Lambda Function:

AWS Lambda is a serverless compute service that allows you to run code in response to events or triggers. You can write custom functions in languages like Node.js, Python, Java, and more, and AWS Lambda takes care of scaling and managing the underlying infrastructure.

Lambda Resolvers in AWS AppSync:

AWS AppSync Lambda resolvers enable you to execute AWS Lambda functions in response to GraphQL queries, mutations, or subscriptions. When a request is made to your AWS AppSync API, the Lambda resolver invokes the associated AWS Lambda function, which performs custom logic, retrieves data, or interacts with external services.

Custom Business Logic:

AWS Lambda resolvers are commonly used when you need to execute custom business logic that goes beyond simple data retrieval from a database. You can use Lambda functions to perform complex computations, access data from various sources, integrate with third-party APIs, and apply authentication and authorization checks.

Resolver Configuration:

To use AWS Lambda resolvers, you configure your AWS AppSync API by specifying which GraphQL fields or types should be resolved using AWS Lambda functions. You map these resolvers to AWS Lambda function ARNs in your API’s schema.

Request and Response Mapping Templates:

Lambda resolvers rely on request and response mapping templates. The request mapping template defines how incoming GraphQL requests are transformed into input for the Lambda function. The response mapping template specifies how the Lambda function’s output is shaped into the GraphQL response format.

Security and Access Control:

AWS Lambda resolvers are secured through AWS Identity and Access Management (IAM). You can define IAM roles and policies to control who can invoke the Lambda functions and access specific resources.

Scalability and Availability:

AWS Lambda functions are highly scalable and available. They automatically handle the scaling of resources based on demand, ensuring the reliability and performance of your application.

Error Handling:

AWS AppSync Lambda resolvers allow you to handle errors gracefully. You can use the response mapping template to format errors and return them to the client in a structured way.

AWS AppSync Lambda resolvers provide a powerful way to extend and customize your GraphQL API by connecting it to AWS Lambda functions, allowing you to perform a wide range of operations and apply complex business logic to your data.

Create AWS Appsync Datasource with Lambda

1.Sign in to the AWS Management Console and open the AppSync console.
2.In the APIs dashboard, choose your GraphQL API.
3.In the Sidebar, choose Data sources.
4.Choose Create data source.
5.For Data source name, enter a name for your data source, such as myFunction.
6.For Data source type, choose AWS Lambda function.
7.For Region, choose the appropriate region.
8.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.
9.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.

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

10.Choose the Create button.

We can invoke a lambda function from aws appsync using lambda as a datasource using AppSync VTL and AppSync JS

AppSync VTL:

invokeLambda.request.vtl :

#
The value of ‘payload’ after the template has been evaluated
will be passed as the event to AWS Lambda.
*#
{
"operation": "Invoke",
"payload": $util.toJson($context.args)
}

invokeLambda.response.vtl:

## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
$util.toJson($context.result)

These Velocity Template Language (VTL) templates are used as part of an AWS AppSync resolver to invoke an AWS Lambda function. They handle the request and response mapping between GraphQL and the Lambda function. Let’s break down these templates in detail:

invokeLambda.request.vtl:

This template handles the request mapping. It prepares the input for the AWS Lambda function. Here’s a detailed explanation of its components:

– `”operation”: “Invoke”`: This part specifies that the operation to be performed is the invocation of an AWS Lambda function.

– `”payload”: $util.toJson($context.args)`: This section defines the payload for the Lambda function. It takes the arguments from the GraphQL request (`$context.args`) and converts them into a JSON format using `$util.toJson()`. The resulting JSON structure is passed as an event to the Lambda function.

invokeLambda.response.vtl:

This template deals with the response mapping. It formats the response from the Lambda function to fit the GraphQL response structure. Here’s a detailed explanation:

– `#if($ctx.error) … #end`: This conditional check is used to handle errors. If there’s an error in the data source invocation, it captures the error message and type. 

– `$util.error($ctx.error.message, $ctx.error.type)`: This line formats the error and raises a GraphQL field error with the error message and type. This ensures that error information is returned in a structured manner to the client.

– `$util.toJson($context.result)`: If there is no error, this line converts the result of the Lambda function execution into JSON format using `$util.toJson()`. The JSON structure is then passed as the response to the GraphQL client.

In summary, these VTL templates work together to invoke an AWS Lambda function from an AWS AppSync resolver. The request template prepares the event data for the Lambda function by converting GraphQL arguments to JSON. The response template handles both successful responses and errors, ensuring that the GraphQL response is properly formatted. This allows you to seamlessly integrate AWS Lambda functions into your GraphQL API and provide efficient error handling.

Appsync JS

invokeLambda.js

import { util } from "@aws-appsync/utils";
export function request(ctx) {
return {
operation: "Invoke",
payload: ctx.args,
};
}
export function response(ctx) {
const { result, error } = ctx;
if (error) {
util.error(error.message, error.type, result);
}
return result;
}

This resolver is used to invoke an AWS Lambda function and manage the request and response mapping. Let’s break down the code in detail:

This file contains two functions, `request` and `response`, which handle the request and response mapping, respectively.

request(ctx):

This function handles the request mapping, which prepares the input for the AWS Lambda function:

– `return`: The `request` function returns an object with two properties, `operation` and `payload`.

– `”operation”: “Invoke”`: This specifies that the operation to be performed is the invocation of an AWS Lambda function.

– `payload: ctx.args`: This property sets the `payload` to the `ctx.args` from the context. `ctx.args` typically contains the arguments passed in the GraphQL request. By passing these arguments as the payload, you provide input to the Lambda function. The Lambda function will receive these arguments as its event data.

response(ctx):

This function handles the response mapping, which formats the response from the Lambda function:

– `const { result, error } = ctx;`: This line destructures the `ctx` object to extract `result` and `error` properties. The `result` contains the response from the Lambda function, and `error` contains information about any errors that may have occurred.

– `if (error) { … }`: This conditional statement checks if an error occurred during the Lambda function execution. If there is an error, it proceeds to handle it.

– `util.error(error.message, error.type, result)`: If there is an error, this line calls the `util.error` function to format and raise a GraphQL field error. It includes the `error.message` and `error.type`, which are part of the error object, and the `result` as additional information. This ensures that error information is properly structured and sent to the client.

– `return result;`: If there is no error, the function simply returns the `result`. This means that the response from the Lambda function is returned to the GraphQL client as-is.

In summary, the `request` function prepares the input for the Lambda function by setting the operation to “Invoke” and passing the GraphQL request arguments as the payload. The `response` function handles both successful responses and errors, ensuring that errors are structured and presented as GraphQL field errors while successful responses are sent directly to the client. These functions allow you to integrate AWS Lambda functions as resolvers in your AWS AppSync API.