AWS AppSync provides an easy way to run a GraphQL API that triggers AWS Lambda functions and other AWS services. If you start with AppSync, you likely have existing systems running next to it. Sooner or later, you want to call the GraphQL API from your Lambda function (Node.js). For example, to trigger an AppSync subscription from another system.
To do so, you need:
- A GraphQL client library
- Authentication
- Send your GraphQL request to the AppSync endpoint
Let’s walk through the steps.
Installing the library dependencies
The easiest way to talk to an AppSync GraphQL API is by using the aws-appsync
package which wraps the apollo
GraphQL client package. The libraries assume that they run in a browser environment where the Fetch API is available. The package cross-fetch
provides a polyfill to bring The Fetch API to Node.js environments as well. The package graphql-tag
is used to parse a GraphQL query.
npm i [email protected] |
Creating a client with authentication
AppSync supports multiple authentication types. If your API uses AWS_IAM
, you are all fine. If not:
- In your AppSync Api Settings, go to Additional auth providers and add
AWS_IAM
- Add the schema directive
@aws_iam
to the mutation like this
type Mutation { |
In your JavaScript code, create a client object:
const appsync = require('aws-appsync'); |
Now, you are ready to send your GraphQL request to the AppSync endpoint
Sending your GraphQL request to the AppSync endpoint
Finally, you create your GraphQL query and send it to the endpoint.
const mutation = gql`mutation Test($value: String!) { |
Keep in mind that your Lambda function needs the following permission to invoke the AppSync API endpoint (replace REGION
with the region identifier and AWS_ACCOUNT_ID
with the AWS account id):
{ |
Summary
Invoking a AppSync GraphQL API endpoint from Lambda requires a few tricks. A typical use case for calling an AppSync API from a Lambda function is to trigger a subscription from an external system.
- AWS Weekly 2016-32
- Mastering Network Security: Your Ultimate 2025 Guide to NSE7_LED-7.0 Certification for Advanced Threat Defense
- Builder’s Diary Vol. 3: Infrastructure Pipeline with GitLab and Terraform Cloud
- Master VMware VCAP-DCV Design 2025: Unlock Career Growth with Our Comprehensive 3V0-624 Certification Guide
- All you need to know about encrypting S3 buckets