Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 9, 2025

Custom message

You can use defineAuth and defineFunction to create an Amazon Cognito custom message AWS Lambda trigger thats sends an custom email or phone verification message, or a multi-factor authentication (MFA) code.

To get started, install @types/aws-lambda package that will be used to define the type of the handler:

Terminal
npm add --save-dev @types/aws-lambda

Next, create a new directory and a resource file, amplify/auth/custom-message/resource.ts. Then, define the function with defineFunction:

amplify/auth/custom-message/resource.ts
import { defineFunction } from '@aws-amplify/backend';
export const customMessage = defineFunction({
name: "custom-message",
resourceGroupName: 'auth'
});

Next, create the corresponding handler file, amplify/auth/custom-message/handler.ts, file with the following contents:

The input event for the CustomMessage_AdminCreateUser trigger source includes both a username and verification code. Admin-created users must receive both their username and code in order to sign in and thus you must include both the usernameParameter and codeParameter in your message template.

amplify/auth/custom-message/handler.ts
import type { CustomMessageTriggerHandler } from "aws-lambda";
export const handler: CustomMessageTriggerHandler = async (event) => {
if (event.triggerSource === "CustomMessage_ForgotPassword") {
const locale = event.request.userAttributes["locale"];
if (locale === "en") {
event.response.emailMessage = `Your new one-time code is ${event.request.codeParameter}`;
event.response.emailSubject = "Reset my password";
} else if (locale === "es") {
event.response.emailMessage = `Tu nuevo código de un solo uso es ${event.request.codeParameter}`;
event.response.emailSubject = "Restablecer mi contraseña";
}
}
if (event.triggerSource === "CustomMessage_AdminCreateUser") {
event.response.emailMessage = `Your username is ${event.request.usernameParameter} and your temporary password is ${event.request.codeParameter}`;
event.response.emailSubject = 'Welcome to Example App';
}
return event;
};

Lastly, set the newly created function resource on your auth resource:

amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';
import { customMessage } from "./custom-message/resource";
export const auth = defineAuth({
// ...
triggers: {
customMessage,
}
});

After deploying the changes, whenever a user with user attribute locale set to es attempts to reset a password they will receive an email with a one-time code in Spanish.