2
votes

admittedly, this is my first attempt at doing anything with Cognito, but I am having trouble adding a user to a user group via lambda. here is the lambda code:

var AWS = require('aws-sdk');
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});

exports.handler = (event, context, callback) => {

    var params = {
        GroupName: 'ROLE_ADMIN',
        // UserPoolId: 'arn:aws:cognito-idp:us-east-1:23453453453:userpool/us-east-1_XXX',
        UserPoolId: 'us-east-1_XXX',
        // Username: '[email protected]'
        Username: 'ec12f604-a83c-4c76-856b-3acd9ca70562'
      }

console.log('before')
  cognitoidentityserviceprovider.adminAddUserToGroup(params, function(err, data) {
    console.log(params)
    if (err) console.log("Error");
    else     console.log("Success");
  });

  console.log('after')

  console.log("Executed.");

  context.succeed(event);
};

I have added the AWS resources to the lambda

Amazon CloudWatch Logs Amazon Cognito Identity Amazon Cognito Sync Amazon Cognito User Pools Amazon SNS Identity And Access Management

here is the output:

Function Logs: START RequestId: e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 Version: $LATEST 2019-10-29T19:04:58.516Z e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 INFO before 2019-10-29T19:04:59.017Z e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 INFO after 2019-10-29T19:04:59.018Z e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 INFO Executed. END RequestId: e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 REPORT RequestId: e6fb3c51-3928-40e1-b0c4-f7a2d9054ef0 Duration: 657.73 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 37 MB Init Duration: 464.08 ms

what I am not seeing is the cognitoidentityserviceprovider.adminAddUserToGroup being called, and the test runs, but it doesn't look like any uses are being added to the ROLE_ADMIN group. what should I be doing to be adding users into a group?

any input would be greatly appreciated... thanks.

1

1 Answers

1
votes

As you can see, you can not see Success or Error log.

Action adminAddUserToGroup has been canceled before it finishes itself, because you call context.succeed(event) to kill the function immediately.

How to solve, just wait until the action finished, then kill the function. In general we have 2 options:

  1. Do right with callback function (recommended):
var AWS = require('aws-sdk');
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });

exports.handler = (event, context, callback) => {

  var params = {
    GroupName: 'ROLE_ADMIN',
    // UserPoolId: 'arn:aws:cognito-idp:us-east-1:23453453453:userpool/us-east-1_XXX',
    UserPoolId: 'us-east-1_XXX',
    // Username: '[email protected]'
    Username: 'ec12f604-a83c-4c76-856b-3acd9ca70562'
  }

  console.log('before')
  cognitoidentityserviceprovider.adminAddUserToGroup(params, function (err, data) {
    console.log(params)
    if (err) console.log("Error");
    else console.log("Success");

    // when the action finished
    console.log('after');

    console.log("Executed.");

    context.succeed(event);
  });
};

  1. Tell to the Lambda function that wait until the call stack is clear: Insert context.callbackWaitsForEmptyEventLoop = true to the first line of function (before declare params variable)