Integrations · Sign-up and auth

Stop bad emails at Amazon Cognito sign-up

An AWS Lambda function for your Amazon Cognito user pool's pre sign-up trigger. It checks the email address of every new user and refuses the sign-ups whose address you do not want: by default addresses that cannot receive mail (invalid) and throwaway inboxes (disposable). Cognito runs it just before it creates the user. If the check cannot be made, because our API is slow, unreachable or answers with an error, the sign-up goes through as if the function were not there. It never confirms or verifies anyone and never changes the user's attributes.

There is nothing to install from a marketplace. Download email-validation-cognito.zip: it holds the function (function/index.js, one CommonJS file with no dependencies), an AWS SAM template (template.json) and LICENSE (MIT), in a folder named email-validation-cognito.

What you need

  • An AWS account with the Cognito user pool, and the right to create a Lambda function, an IAM role and a Systems Manager parameter in the pool's Region.
  • The AWS SAM CLI if you deploy with the template.

What gets checked

Cognito runs the pre sign-up trigger in three cases, and all three are checked unless you narrow it with CheckSources:

  • SignUp: a person signs up through your app (the SignUp API) or your managed login sign-up page.
  • AdminCreateUser: an administrator creates the user. A refusal makes that call fail, and Cognito sends no invitation to the address.
  • ExternalProvider: a person signs in for the first time with Google, a SAML or an OIDC provider. A refused federated user is not created and cannot sign in.

A sign-up by phone number, or a federated user whose provider sends no email address, goes through unchecked. Cognito runs the trigger only when it creates a user, so a user who later changes their email address is not checked, and user import jobs do not reach it.

Set it up

  1. Create an API key on the API keys page with the permission Validate addresses and a daily credit cap.

  2. In the pool's Region, open AWS Systems Manager → Parameter Store → Create parameter: name /email-validation/api-key, tier Standard, type SecureString, the default KMS key, and paste the key as the value.

  3. Unpack the download and deploy the function from the email-validation-cognito folder with the template:

    sam deploy --guided --template-file template.json

    Answer with your user pool's ID (for example us-east-1_AbCdEfGhI) and keep or change the other settings. The stack creates the function, a role that may read only that one parameter, and the permission for that one pool to call the function.

    Or by hand in the Lambda console: a Node.js 24.x function, function/index.js as index.js (delete index.mjs), handler index.handler, timeout 5 seconds, the settings as environment variables, and the two permissions from the template added to the function's role.

  4. In the Amazon Cognito console, open your user pool, then Extensions → Lambda triggers → Add Lambda trigger, choose the sign-up category's pre sign-up trigger, assign the function and save.

  5. Sign up with no_mailbox@sandbox.email-validation.org, a test address: it is refused, for free. Do not sign up with a test address that goes through, such as accepted@sandbox.email-validation.org: Cognito would send its verification code to a domain that receives no mail. The outcome is in the function's CloudWatch log.

If your pool already has a pre sign-up function, call the check from it instead: a pool has one pre sign-up trigger. Copy function/index.js beside that function's code as email-validation.cjs (the .cjs name works from CommonJS and ES modules alike; in an ES module, import ev from './email-validation.cjs' and call ev.checkSignupEmail(...)), and give that function the same settings as environment variables and the two permissions from the template:

const { checkSignupEmail } = require('./email-validation.cjs');

exports.handler = async (event, context) => {
  // CheckSources does not apply here: choose the trigger sources yourself.
  if (event.triggerSource === 'PreSignUp_SignUp') {
    const check = await checkSignupEmail(event.request?.userAttributes?.email, context); // never throws
    if (check.stop) throw new Error(check.message);
  }
  // ... the pool's own rules
  return event;
};

Call it first in your function and keep the function's timeout at 5 seconds: its 3.5 seconds count from the call, and Cognito's 5 include a cold start, so keep the work before it and after it short.

Settings

Template parameterEnvironment variableWhat it does
ApiKeyParameterNameEV_API_KEY_PARAMETERThe Parameter Store parameter with your key. Default /email-validation/api-key.
ApiRegionEV_REGIONeu sends the checks to our EU host. Default us.
StopStatusesEV_STOP_STATUSESThe statuses that refuse a sign-up, separated by commas: invalid, disposable, catch_all, unknown. Default invalid,disposable; none refuses nothing and only logs the results.
DenyMessageEV_DENY_MESSAGEYour own message for a refused sign-up. Default: one sentence for invalid, one for disposable and one for the others.
CheckSourcesEV_CHECK_SOURCESWhich sign-ups are checked: SignUp, AdminCreateUser, ExternalProvider. Default all three.

A result marked disposable is refused while disposable is on the list, whatever its status (the API keeps invalid or unknown as the status of some addresses on disposable domains), except an unknown that means our check ran out of time or failed (timeout, internal_error). Otherwise a valid result goes through, and an unknown result is refused only if you add it to the list, never for timeout or internal_error.

What the person sees

Cognito turns the refusal into its UserLambdaValidationException error, with your message inside its own words ("PreSignUp failed with error …"). Managed login shows it above the form; an app that calls SignUp itself shows the message it chooses for that error. There is no "Did you mean …?" suggestion, because the message also goes to the function's log, which never holds an address.

If something goes wrong

Cognito gives the function 5 seconds and cannot be given more. The function gives up after 1 second for reading the key, 3 seconds for the check and 3.5 seconds in all, and lets the sign-up through. Cognito's 5 seconds include Lambda starting a new instance of the function (a cold start), so the 3.5 leave at least 1.5 seconds for it: a function that answers too late fails the sign-up, and Cognito may run it again, a second charged check. The same happens when the key is missing or wrong, when our API answers with an error or a rate limit (after a rate limit it pauses its checks for the time our API asks: 30 seconds if it names none, at most 5 minutes), and on any unexpected error. The function never resends a check (Cognito may run the trigger again). A refusal, a check that could not be made and a setting that was not understood each write one line to the function's CloudWatch log with the outcome and the request id, never the address or the key; a sign-up that simply goes through writes nothing.

What it costs

Each check costs at most 1 credit, like any validation request. unknown results, errors, malformed addresses and test addresses are free. A check our API finishes after the function stopped waiting can still be charged. Credits come in one-time packs, from $0.15 per 1,000 with the 50,000,000-credit pack (smaller packs cost more per credit), and new accounts get 500 free credits (100 when you sign up with a free email provider such as Gmail, Outlook or Yahoo, another personal mailbox such as a paid one like Fastmail or one from your internet provider, or an address from a relay or alias service). Lambda and Parameter Store are billed by AWS to your own account.

Privacy

The function sends only the new user's email attribute, in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org), and nothing else Cognito sent. It never puts the address in a URL and sends it to no other host. The key stays in Parameter Store; it is never in the code, the template or an environment variable.

More on Amazon Cognito

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us