Question
I have a lambda function triggered when someone registers via federated identities that creates an entry in a dynamodb table.
I want the same function (or similar) to occur when a user registers (I was thinking post confirm) via an associated user pool.
Background (what I've attempted)
I've linked the federated identity to the user pool but the lambda linked to the Cognito trigger does not get called for user pools. I thought it may not support the same process (is this the case?) and tried adding a customised workflow trigger to the user pool for post confirm. I just get an error back (bad request 400) stating '{"__type":"NotAuthorizedException","message":"User cannot confirm."}' although the user is showing as confirmed in Cognito.
I've looked at the documentation but I don't see many clear examples. The best I found was one emailing on post confirm which I modified to contain a basic dynamo call as follows:
var doc = require('dynamodb-doc');
exports.handler = function(event, context) {
console.log(event);
if (event.request.userAttributes.email) {
var db = new doc.DynamoDB();
var tableName = 'Users'
var user = {
'id' : event.identityId,
'name' : event.datasetRecords.name.newValue,
'email' : event.datasetRecords.email.newValue,
};
var params = {
'TableName' : tableName,
'Item' : user
};
console.log('Inserting user', params);
db.putItem(params, function(err, data) {
console.log(err, data);
if (err) {
console.log('User insert failure', err);
context.done(err);
} else {
console.log('User insert success', data);
context.done(null, event);
}
});
} else {
// Nothing to do, the user's email ID is unknown
context.done(null, event);
}
};
I've looked at similar questions and nearest I found was this previous question although it does not include a working code snippet. I've tried a few variations but no luck!
As stated there I have also seen callbacks used in other examples so it would be good to clear up what the preferred and working code should look like!
I would also like to know if it should return data within the context.done in a particular format as I saw some set responses like the following:
"response": {
}
Many thanks!
Calling ConfirmSignUp on already confirmed user throws the error "User cannot confirm". As a result of that post confirmation lambda function is not called. Though this error could be more descriptive.
Have you looked at this example from the docs already? http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#aws-lambda-triggers-post-confirmation-example