Twitter LogoFacebook Logo
Confirmation Lambda
We’ll create another lambda function to confirm the account using the confirmation code received when signing up.
By: King

Hello, in this tutorial, we’ll continue building the Login System using Amazon Cognito, API Gateway, Lambda Function, and the HTTP Requests for Blueprints plugin. 

In the last tutorial, we created the Cognito User Pool to store our user accounts and a lambda function to add users to the pool. 

Now, we’ll create another lambda function to confirm the account using the confirmation code received when signing up.

Creating the Confirmation Lambda

Login to your AWS Console and search for Lambda in the search bar. 

Image from Codeible.com

Create a new function. 


Give the lambda a name like ConfirmAccount

Then set the Runtime to Node.js and click “Create function” to complete the process.

Now open the AWS SDK documentation for Cognito Identity Provider


Search for ConfirmSignUpCommand and hit enter a couple of times. You should see the link to the command. Click on it.

Image from Codeible.com

Copy the import statement and paste it inside the Lambda function. 

import { CognitoIdentityProviderClient, ConfirmSignUpCommand } from "@aws-sdk/client-cognito-identity-provider";

Now go back to the documentation, and copy the rest.

Paste the whole thing in the handler function. 

const client = new CognitoIdentityProviderClient(config);
const input = { // ConfirmSignUpRequest
  ClientId: "STRING_VALUE", // required
  SecretHash: "STRING_VALUE",
  Username: "STRING_VALUE", // required
  ConfirmationCode: "STRING_VALUE", // required
  ForceAliasCreation: true || false,
  AnalyticsMetadata: { // AnalyticsMetadataType
    AnalyticsEndpointId: "STRING_VALUE",
  },
  UserContextData: { // UserContextDataType
    IpAddress: "STRING_VALUE",
    EncodedData: "STRING_VALUE",
  },
  ClientMetadata: { // ClientMetadataType
    "<keys>": "STRING_VALUE",
  },
};
const command = new ConfirmSignUpCommand(input);
const response = await client.send(command);

For the client, replace config with a pair of curly brackets and put region, colon, and the region you are in.

const client = new CognitoIdentityProviderClient({region: "us-east-1"});

Now delete everything in the input except for the required ones.

const input = { 
  ClientId: "STRING_VALUE", // required
  Username: "STRING_VALUE", // required
  ConfirmationCode: "STRING_VALUE", // required
};

The ClientId is for a Cognito User pool app client id. 


To get it for the one that was created earlier, search for Cognito in the search bar at the top. Right click on it in the menu and open in a new tab. 

Select the User pool. Go to App integration and scroll down to the App clients section. 

Select the app client and use the client id in the lambda function. 

For the username, type event, followed by square brackets, and “username” inside them. The ConfirmationCode would be the same but with confirmationCode inside. 

Username: event["username"],
ConfirmationCode: event["confirmationCode"]

Testing

To test this, click on the arrow next to the test button and select Configure test event. 

Image from Codeible.com

Give the event a name like ConfirmationTest.

Then edit the JSON by setting the first property to username, followed by the email that was used when you created the account. 

For the next property, replace it with confirmationCode followed by an empty string. 

Save and go back to Cognito. 


Select your user pool, click on the Users tab, select the user account, and delete it. 

Then go back to the CreateAccount lambda. Click Test to create the account again.

You should receive a email with the confirmation code.

Now back to the ConfirmAccount lambda and edit the test configuration by setting the confirmationCode with the code you received. 

Save, and if we click on “Test,” we should get a successful attempt since the status code is 200.

If we go back to the Users tab in Cognito, we should see that the email is verified and the confirmation status is confirmed. 

That’s all for this tutorial. In the next one, we’ll create the lambda function for signing in.


Sign In