orome
orome

Reputation: 48486

How do I configure AWS Amplify to automatically add a new user to a group?

I've tried editing GROUP in

amplify/backend/function/<appnameXXXXX>PostConfirmation/function-parameters.json

followed by amplify push but that has no effect (and seems to leave the backend unchanged). I've tried editing the GROUP environment variable in the Lambda Console for <appnameXXXXX>PostConfirmation-<env> followed by amplify pull, which does update the local file above as expected, but new users are still not added to the specified group.

What am I doing wrong? How do I configure my app to automatically add a new user to a default group?


Relevant files, neither of which I've edited (except as noted for GROUP above):

function-parameters.json:

{
  "trigger": true,
  "modules": [
    "add-to-group"
  ],
  "parentResource": "app_nameXXXXXX",
  "functionName": "app_nameXXXXXXPostConfirmation",
  "resourceName": "app_nameXXXXXXPostConfirmation",
  "parentStack": "auth",
  "triggerEnvs": [],
  "triggerDir": "/snapshot/repo/build/node_modules/@aws-amplify/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation",
  "triggerTemplate": "PostConfirmation.json.ejs",
  "triggerEventPath": "PostConfirmation.event.json",
  "roleName": "app_nameXXXXXXPostConfirmation",
  "skipEdit": true,
  "GROUP": "Testers",
  "enableCors": false
}

src/add-to-group.js:

const aws = require('aws-sdk');

const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({
  apiVersion: '2016-04-18',
});

/**
 * @type {import('@types/aws-lambda').PostConfirmationTriggerHandler}
 */
exports.handler = async event => {
  const groupParams = {
    GroupName: process.env.GROUP,
    UserPoolId: event.userPoolId,
  };
  const addUserParams = {
    GroupName: process.env.GROUP,
    UserPoolId: event.userPoolId,
    Username: event.userName,
  };
  /**
   * Check if the group exists; if it doesn't, create it.
   */
  try {
    await cognitoidentityserviceprovider.getGroup(groupParams).promise();
  } catch (e) {
    await cognitoidentityserviceprovider.createGroup(groupParams).promise();
  }
  /**
   * Then, add the user to the group.
   */
  await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise();

  return event;
};

Upvotes: 0

Views: 890

Answers (1)

LuckyTuvshee
LuckyTuvshee

Reputation: 1194

You can use amplify update function command to manage your function's env variables.

Which will add corresponding configs in, amplify/team-provider-info.json and your lambda functions's cloudformation template json file.


This is how the command generate the ENV variable.

Lambda function's cloudformation-template.json

{
  "Parameters": {
    ...
    "GROUP": {
      "Type": "String"
    },
    ...
  },
  "Resources": {
    ...
    "LambdaFunction": {
      ...
      "Properties": {
        ...
        "Environment": {
          "Variables": {
            "GROUP": {
              "Ref": "GROUP"
            },
          }
        }
      }
    }
  }
}

team-provider-info.json

{
  "dev": {
    "categories": {
      "function": {
        "<function-name>": {
          "GROUP": "users",
        }
      }
    }
  }
}

Upvotes: 1

Related Questions