Skip to content

Commit

Permalink
fix: add-to-group PostConfirmation Lambda should not throw
Browse files Browse the repository at this point in the history
/**
 * For some reason, naively returning a value / Promise or throwing an error
 * will not work as it is specified in the Lambda docs:
 * @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
 *
 * This causes an "Invalid JSON" error which can, so far, only be worked
 * around by resolving with deprecated non-async `callback`:
 * @see aws-amplify#2735
 * @see aws-amplify#4341
 * @see aws-amplify#7179
 */
  • Loading branch information
ctjlewis committed Apr 25, 2021
1 parent 70a980f commit 8ff8dac
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable-line */ const aws = require('aws-sdk');

exports.handler = async (event, context) => {
exports.handler = async (event, context, callback) => {
const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
const groupParams = {
GroupName: process.env.GROUP,
Expand All @@ -19,16 +19,21 @@ exports.handler = async (event, context) => {
await cognitoidentityserviceprovider.createGroup(groupParams).promise();
}

/**
* For some reason, naively returning a value / Promise or throwing an error
* will not work as it is specified in the Lambda docs:
* @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
*
* This causes an "Invalid JSON" error which can, so far, only be worked
* around by resolving with deprecated non-async `callback`:
* @see https://github.com/aws-amplify/amplify-cli/issues/2735
* @see https://github.com/aws-amplify/amplify-cli/issues/4341
* @see https://github.com/aws-amplify/amplify-cli/issues/7179
*/
try {
await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise();
return {
statusCode: 200,
body: JSON.stringify(event)
}
callback(null, event);
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error)
}
callback(new Error(error));
}
};

0 comments on commit 8ff8dac

Please sign in to comment.