1 min read

Adding Custom Cognito SignUp Message with Terraform, Lambda & Compoze

Adding Custom Cognito SignUp Message with Terraform, Lambda & Compoze

AWS Cognito is a secure and scalable user access management solution offered by Amazon Web Services. At Compoze Labs it is our go to solution for managing users when building apps in AWS.

Compoze is a platform that enables the rapid, secure, and scalable development of software products in AWS.

Custom Cognito Message

 

Cognito provides an easy way to send emails to your users during various user events such as: verifications, invitations, and MFA events.

Screenshot of a message templates interface showing three entries: Verification message (Email), Invitation message (SMS, Email), and MFA message (SMS). There's an Edit button on the top right.

However, if you want to add customization to these messages you need to create Lambda Triggers that fire during each of these events. These triggers can be executed during: pre-signup, pre-authentication, post-authentication, etc. The full list can be found in the AWS Docs.

A user interface for customizing workflows with triggers using AWS Lambda functions. It includes sections for pre-sign-up, pre-authentication, post-authentication, verify auth challenge response, and define auth challenge, each with dropdown menus.

For a recent app, we needed to implement a confirmation email when a user signed up. We wanted to have a custom link that the user would click, that would then redirect back to our app to confirm their user. In order to do this we needed to create a Lambda handler for the Custom Message Signup trigger .

Custom Message Trigger

 

In order to create this custom signup flow we needed to do the following:

  1. Create a custom Lambda Function to handle the Custom Message Trigger
  2. Update the Lambda Function to create our custom message with the generated confirmation link
  3. Update our Cognito instance to include the trigger (in our case we used Terraform to handle our Cognito configuration)

Create a custom Lambda Function to handle the Custom Message Trigger

 

The first thing we needed to do was create the Lambda project. With the Compoze CLI we quickly spun up our base Lambda project

Terminal screenshot showing text-based interface. The user is using Compoze to create a new component. Selected "Lambda Function" with the name "CustomAuthMessaging." The creation process is in progress.

Now we have a basic Lambda project configured with source code and a working deployment pipeline.

Screenshot of a GitHub repository with files and folders for a project named "CustomAuthMessaging." It shows details such as commits, branches, and tags.

Update the Lambda Function to create our custom message with the generated confirmation link

Next, we need to create our Lambda Handler to be able to process the custom message triggers.

First we need to update our AWS SAM template with our function definition.

Screenshot of a YAML configuration file for AWS Lambda. It defines a function named "customEmailFunction" with properties for type, handler, runtime, environment, variables, and tags. Hosted on GitHub, with coding lines numbered.

Once we have that, we need to create our Lambda Handler to generate our custom signup link and update the email message.

Screenshot of TypeScript code defining a Lambda function for sending a custom message. It checks event trigger source, logs activity, and customizes an email template with a confirmation link. The function responds with an email message or an empty return.

Then, we push and deploy!

Update our Cognito instance to include the trigger (in our case we used Terraform to handle our Cognito configuration)

Once our Lambda functions are deployed, we can update our Terraform module to contain the Lambda ARNs

Screenshot of a code snippet defining an AWS Cognito user pool module. It includes a lambda_config with a custom_message ARN and a variable for custom_message_lambda_arn. The code is hosted on GitHub.

 

After a simple terraform plan and terraform deploy we’re all set!

If you’re interested in learning more about how your team can leverage the Compoze CLI and the Compoze Platform reach out to us.

Secure By Default: Using HashiCorp’s Packer With AWS and EBS

Secure By Default: Using HashiCorp’s Packer With AWS and EBS

In my years at Compoze Labs, I’ve worked on numerous AWS based projects, in a number of highly regulated industries, throughout the years. Whether...

Read More
How to Fix Your Spaghetti Code (and Avoid it in the First Place)

How to Fix Your Spaghetti Code (and Avoid it in the First Place)

Software is said to be written with “Spaghetti Code” when the software is difficult to maintain or extend. Spaghetti code is hard to understand what...

Read More
Mastering Generative AI: 4 Tips for Software Developers

Mastering Generative AI: 4 Tips for Software Developers

Questions on every software developer’s mind:

Read More