From 8ff8dacab0cfca174a4cab51b501e73c95d08258 Mon Sep 17 00:00:00 2001 From: Christian Lewis Date: Sun, 25 Apr 2021 07:48:08 -0500 Subject: [PATCH] fix: add-to-group PostConfirmation Lambda should not throw /** * 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 */ --- .../triggers/PostConfirmation/add-to-group.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js index c33dca12fc1..9c663c4a430 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js @@ -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, @@ -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)); } };