I'm new at AWS Lambda Functions and I want to create a new function to fetch user groups from my Cognito User Pool generated by Amplify, I saw many examples but so far my function didn't work, I think I'm probably missing some permission, I'm not sure. Here is my function:
const AWS = require('aws-sdk');
exports.handler = async (event) => {
let cognito = new AWS.CognitoIdentityServiceProvider();
const params = { UserPoolId: 'us-east-1_xxxxxxx'};
let res = { status: 'no changes' };
cognito.listGroups(params, function(err, data) {
console.log('inside of response')
if (err) {
res = { error: err };
}
else {
res = { data: data };
}
})
const response = {
statusCode: 200,
body: JSON.stringify(res),
};
return response;
};
I already add permission following this answer here
I'm following the API docs here to call the Cognito listGroups
method.
For permissions, the role associate with this function has this policy:
I'm doing everything online, and to test it I'm just using the Test button, so far the only response I got was: { status: 'no changes' }
, not even the log inside of the function is displayed.
How to make this function work to listGroups from my Amplify Cognito User Poll? Is there any permission missing?
Similar questions:
I manager to get it to work:
const AWS = require('aws-sdk');
let cognito = new AWS.CognitoIdentityServiceProvider();
exports.handler = async (event) => {
const params = { UserPoolId: 'us-east-1_xxxxxxx'};
let res = { status: 'no changes' };
await cognito.listGroups(params, function(err, data) {
if (err) {
res = { error: err };
}
else {
res = { data: data };
}
}).promise();
const response = {
statusCode: 200,
body: JSON.stringify(res),
};
return response;
};
I was missing the await
and the .promise()
at the end of cognito call.