Amplify-cli: Add custom modules to cognito trigger functions

Created on 17 Jul 2020  路  5Comments  路  Source: aws-amplify/amplify-cli

Is your feature request related to a problem? Please describe.
So I've added custom "modules" to my postConfirmation lambda trigger. These are added to the parameters.json file in a comma-separated list. Custom modules (which are JS files that get executed from the event handler function) get removed between checking out staging/production amplify environments. The only one which stays is the one that amplify adds for adding users to a cognito user group.

Describe the solution you'd like
Ability to add a new module without the modules list removing my custom module file between checking out environments.

Describe alternatives you've considered
Manually remembering to add the name of the custom module to the parameters.json file.

functions pending-response question

All 5 comments

I'm not sure I understand why you need to add the module list into the parameters.json file.
Could you share the content in the amplify/backend/function/<function-resource-name>/ folder?
You can send it to [email protected] with title "Re Github Issue 4862"

@UnleashedMind

So I have a postConfirmation cognito lambda trigger function:

amplify/backend/function/postConfirmationFunction

Amplify adds a add-to-group.js file which is referenced in the parameters.json file and the index.js file.

index.js

exports.handler = (event, context, callback) => {
  const modules = process.env.MODULES.split(',');
  for (let i = 0; i < modules.length; i += 1) {
    const { handler } = require(`./${modules[i]}`);
    handler(event, context, callback);
  }
};

parameters.json

{"modules":"add-to-group"}

add-to-group.js

/* eslint-disable-line */ const aws = require('aws-sdk');

exports.handler = async (event, context, callback) => {
  const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
  const groupParams = {
    GroupName: process.env.GROUP,
    UserPoolId: event.userPoolId,
  };

  const addUserParams = {
    GroupName: process.env.GROUP,
    UserPoolId: event.userPoolId,
    Username: event.userName,
  };

  try {
    await cognitoidentityserviceprovider.getGroup(groupParams).promise();
  } catch (e) {
    await cognitoidentityserviceprovider.createGroup(groupParams).promise();
  }

  try {
    await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise();
    callback(null, event);
  } catch (e) {
    callback(e);
  }
};

I've added to this to run a separate js file to other stuff - like send welcome emails and initialize dynamodb tables etc. The parameters.json file is updated between environments and doesn't remember the new modules (js files) I have added.

For the particular trigger, when you configure the auth category, you can select both "Add User To Group" and "Create your own module", then your will see {"modules":"add-to-group, custom"}

For the particular trigger, when you configure the auth category, you can select both "Add User To Group" and "Create your own module", then your will see {"modules":"add-to-group, custom"}

@UnleashedMind oh really? I'll give that a try!

Please close this issue if you are not blocked anymore.

Was this page helpful?
0 / 5 - 0 ratings