Waseem Parker
Waseem Parker

Reputation: 11

Can I run Cognito in a Lambda function?

I want to sign up users with Cognito in a Lambda function. However I am receiving "TypeError: fetch is not a function"

My code is basically step 3 in this. However I keep getting the above-mentioned error, even though I have node-fetch installed. From what I understand, the Cognito SDK makes use of fetch. So is this simply not possible? Will I need to spin up a Node server?


  const AWS = require("aws-sdk");
  const AmazonCognitoIdentity = require("amazon-cognito-identity-js");

  //Configuring pool data of Cognito Identity Pool
  const poolData = {
    UserPoolId: "us-east-2_aCvZbFzeS",
    ClientId: "4nv2krchr77pbrq3cpk0q0kknu"
  };

  const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

  AWS.config.region = "us-east-2";

  const attributeList = [];

  attributeList.push(
    new AmazonCognitoIdentity.CognitoUserAttribute({
      Name: "email",
      Value: "[email protected]"
    })
  );

  userPool.signUp(
    "[email protected]",
    "SamplePassword123",
    attributeList,
    null,
    function(err, result) {
      if (err) {
        console.log(err);
        return;
      }
      const cognitoUser = result.user;
      console.log("user name is " + cognitoUser.getUsername());
    }
  );

  const data = JSON.parse(event.body);

  const headers = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": true
  };

  const response = {
    statusCode: 200,
    headers: headers,
    "Content-Type": "application/json",
    body: JSON.stringify(data.age)
  };

  callback(null, response);
};


//I keep receiving this error when attempting to hit the endpoint with Postman:

    "errorMessage": "Uncaught error in your 'hello' handler",
    "errorType": "TypeError",
    "stackTrace": [
        "TypeError: fetch is not a function"

Upvotes: 1

Views: 1055

Answers (1)

mattbornski
mattbornski

Reputation: 12563

You can definitely use Cognito from Lambda! Source: have done it.

You may not be able to use the AWS Cognito JS SDK from Lambda nicely, though.

The AWS Cognito JS SDK appears to be designed for client-side applications, where fetch is a built-in. You have installed node-fetch, but the SDK is not loading it because it doesn't think it needs to, because it is expecting it to be built-in.

I see two options:

  • If you aren't particularly attached to JS, you could use another language where you are confident that the library is designed and tested for server-side applications.
  • If you are attached to JS or have a large sunk cost, you could hack up the AWS Cognito JS SDK locally before deploying the code to Lambda to make it require node-fetch or otherwise make it functional server-side.

This thread has a good description of the same issue and some workarounds; probably the best one for you is:

global.fetch = require('node-fetch')
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');

in your script, which should make it appear as a built-in to the SDK's code without hacking up the internals.

Upvotes: 1

Related Questions