DEV Community

Kishan
Kishan

Posted on • Edited on

Solving the Puzzle: How to Pass Environment Variables in AWS AppSync Resolvers using Serverless

Are you just diving into the world of AWS AppSync and puzzled about passing environment variables to the resolver using the Serverless Framework? If you've scoured countless blogs without finding a solution, you've landed on the right page!

Scenario

Imagine you have an AppSync endpoint triggering a mutation. In this scenario, you need to validate user information and perhaps trigger an SQS with a unique accountID for each environment or use the tableName in the resolver function to fetch data from DynamoDB. Whatever your use case, you can follow these steps to seamlessly pass environment variables to your resolver function.

Solution

Step1:- Add a property called substitutions to the resolver you've created. This is where you'll pass your environment variables as key-value pairs. In the example below, I'm using tableName as an environmental variable.

Passing Env's in resolver

Step2:- Now that you've successfully passed your environment variables to the resolver, you'll need to access them within the resolver function. While in a Lambda function, you might use 'process.env.tableName', in the resolver function, you'll need to add "#" at the start and end of the name you passed in Step 1. Here's an example:

const tableName = '#tableName#';
const accountId = '#accountId#';
Enter fullscreen mode Exit fullscreen mode

Ensure you add this before the request and response functions in your resolver.

Accessing Env's in Resolver Function

Step3:- With the setup complete, you now have the knowledge to pass environment variables seamlessly to your AppSync resolver function.

This blog aims to simplify the process, ensuring you can efficiently manage and utilize environment variables in your AppSync projects. Feel free to explore, experiment, and elevate your serverless development experience!"

Top comments (0)