1
votes

I've created an application that allows users to signup, which adds them into my AWS Cognito user pool. This was done using the AWS-SDK for PHP, specifically the signUp method. I've tried to create a Lambda function that triggers upon PostConfirmation that adds user details into my DynamoDB table and it works when I use the Test feature but when I set it up for live deployment, it won't add users into the table and does not give me any errors.

Here is my method's parameters:

$this->client->signUp([
                'ClientId' => $this->client_id,
                'Username' => $username,
                'Password' => $password,
                'UserAttributes' => [
                    [
                        'Name' => 'name',
                        'Value' => $name
                    ],
                    [
                        'Name' => 'email',
                        'Value' => $email
                    ],
                ]
            ]);

This is my Lambda function:

var aws = require('aws-sdk');
const ddb = new aws.DynamoDB({apiVersion: '2012-10-08'});

aws.config.update({region: 'us-east-1'});

exports.handler = function(event, context, callback) {
    var params = {
            TableName: 'RecipeUsersDB',
            Item: {
                'username': {S: event.request.userName},
                'name': {S: event.request.userAttributes.name},
                'email': {S: event.request.userAttributes.email}
                }
        }
    ddb.putItem(params, function(err, data) {
      if (err) {
        console.log("Error", err);
      } else {
        console.log("Exit", data);
      }
    });
    callback(null);
};

This is the Test event parameters that work:

{
  "request": {
    "userName": "hrefrlo",
    "userAttributes": {
      "sub": "e1feer",
      "name": "2srhserrh",
      "email": "3srwergthgsrg"
    }
  },
  "response": {}
}

I've given the function several different permission policies:


AmazonDynamoDBFullAccess

AmazonCognitoDeveloperAuthenticatedIdentities

AmazonCognitoPowerUser

AWSLambdaBasicExecutionRole

AWSLambdaInvocation-DynamoDB


This is just for a uni assignment so the app(website) is just going to be used for a demo, and is only going to be deployed on Beanstalk. Any help is appreciated, thank you!

1
this is obvious but not clearly mentioned in the question... did you already confirm the user signup? - reda la
Yes sorry, I should have mentioned that. I have a method that confirms the user using a confirmation code sent to their email. - benjaminl13

1 Answers

0
votes

I have realized my silly mistake. Cognito's PostConfirmation response is actually in the form of:

{
    "version": "1",
    "region": "eu-central-1",
    "userPoolId": "eu-central-1_45YtlkflA",
    "userName": "user4",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-java-console",
        "clientId": "4736lckau64in48dku3rta0eqa"
    },
    "triggerSource": "PostConfirmation_ConfirmSignUp",
    "request": {
        "userAttributes": {
            "sub": "a2c21839-f9fc-49e3-be9a-16f5823d6705",
            "cognito:user_status": "CONFIRMED",
            "email_verified": "true",
            "email": "[email protected]"
        }
    },
    "response": {}
}

In my code, I had event.request.userName, when it should have been event.userName.