question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct syntax for event.json when mocking a PreSignUp triggered lambda function

See original GitHub issue

Which Category is your question related to? Mocking Lambda functions

Amplify CLI Version 4.18

What AWS Services are you utilizing? API, Auth, Functions, Storage, S3

Hi Amplify,

I’m trying to learn how to mock functions locally using Amplify, so that I can stop doing edit/push over and over, but I can’t seem to find any good docs on the subject.

Starting “simple”, I’m just trying to test a stand alone function.

My (React) app uses Auth.signUp() like this:

const cognitoUser = await Auth.signUp({
  username: username,
  password: password,
  attributes: {
    email: email,
    "custom:appleEmail": appleEmail,
    "custom:googleEmail": googleEmail
  }
});

The request gets intercepted and handled by a Pre sign-up lambda trigger function. And when that event arrives there it looks like this:

Output from console.log(event) in the lambda function

{
  version: '1',
  region: 'us-west-2',
  userPoolId: 'us-west-2_asbcd1234',
  userName: 'Kimfucious',
  callerContext: {
    awsSdkVersion: 'aws-sdk-unknown-unknown',
    clientId: 'ab41fed6r1kspmh12345678'
  },
  triggerSource: 'PreSignUp_SignUp',
  request: {
    userAttributes: {
      'custom:googleEmail': 'example@gmail.com',
      'custom:appleEmail': 'example@icloud.com',
      email: 'example@example.com'
    },
    validationData: null
  },
  response: {
    autoConfirmUser: false,
    autoVerifyEmail: false,
    autoVerifyPhone: false
  }
}

What I don’t understand is what to put in event.json in the src directory of the lambda function in question.

For example, if in the lambda function I want to perform certain actions based on triggerSource like this:

console.log("EVENT", event)
console.log("TRIGGER_SOURCE", event.triggerSource)
if (event.triggerSource === "PreSignUp_SignUp") {
   // do something
} else if (event.triggerSource === "PreSignUp_ExternalProvider") {
  // do something else
} else {
 // do something because no event.triggerSource matches the above
}

If I use the event from the above (where triggerSource is PreSignUp_SignUp) as the event.json for the mock function call, my expectation is that the function would receive the event and be processed by the first if/conditional.

However, here’s what I see when running `amplify mock function functionName:

  1. The first console log prints out event, and it looks okay to me.
  2. The second console log prints out undefined for event.triggerSource
  3. This results in the function always processing with the final else clause of the conditional.

I believe that I have a fundamental misunderstanding here about how this is all supposed to work, like “mocking isn’t invoking”, “you should be using SAM”, or something, but, again, I can’t seem to find the right docs/examples.

Any help toward finding the right docs and/or understanding this better would be most appreciated.

Thanks 🌮 !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kimfuciouscommented, Apr 23, 2020

Thanks for the input @alexkates

I also noticed that while @jplaza’s idea might work to parse the string when working with amplify mock, that parsing is unwanted with actually running the functions through the app while in dev.

1reaction
jplazacommented, Apr 23, 2020

I’m trying out amplify too and the problem you are having @kimfucious is that the event is passed as a string instead of an object as expected. I don’t know if I’m missing something too but for the meantime I’m doing this

exports.handler = async (event, context) => {
    if (process.env.ENV === "dev")
      event = JSON.parse(event);
  // ... rest of my code
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Lambda functions in the console - AWS Documentation
You can test your Lambda function in the console by invoking your function with a test event. A test event is a JSON...
Read more >
Advanced workflows - Mocking and testing - AWS Amplify Docs
The structure of the event that is sent to the lambda trigger can be found here. ... eg. amplify mock function myFunctionName; --event...
Read more >
Pre signup lambda trigger. aws. Auth ... - Wise Home Creation
Go out in style on a beautiful, 145 foot yacht w/ 3 I'm writing a Lambda function for a pre-signup trigger on my...
Read more >
What kind of event does Cognito send from Pre Sign-up ...
I have created a PreSignup Lambda function to be used with Cognito Pre-SignUp trigger with the following code:
Read more >
Chapter 2. Your first Lambda function - AWS Lambda in Action
Triggers invoke a Lambda function when certain events happen. ... When invoking a Lambda function, events are expressed using a JSON syntax that's ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found