'Amplify throws error when mocking/running lambda function using require('aws-appsync')

I'm trying to call my appsync API in a lambda in my Amplify project so I want to use the aws-appsync library.

My lambda looks like this:

const AWSAppSyncClient = require("aws-appsync");
exports.handler = async (event) => {
  console.log(event)
};

When running amplify mock function <myfunction> or when running it in the AWS console.

This will throw an error:

{
  stack: 'Error: Could not load lambda handler function due to TypeError: Cannot convert undefined or null to object\n' +
    '    at loadHandler (C:\\Users\\lalle\\nvm\\v12.18.0\\node_modules\\@aws-amplify\\cli\\node_modules\\amplify-nodejs-function-runtime-provider\\lib\\utils\\execute.js:106:15)\n' +
    '    at processTicksAndRejections (internal/process/task_queues.js:97:5)',
  message: 'Could not load lambda handler function due to TypeError: Cannot convert undefined or null to object'
}

What does this mean and how do I fix it?



Solution 1:[1]

I was running into this issue and it was because I was trying to use an environment variable that I'd forgotten to include. I had to update the permissions to give the lambda access to my API.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman