AWS Cognito · Python
Block disposable email in AWS Cognito
Attach a Lambda to the PreSignUp trigger of your Cognito User Pool. The Lambda runs synchronously and can throw to reject before the user record is written.
The code
# lambda_function.py
import json
import os
import urllib.request
import urllib.parse
CHECK_URL = "https://api.piisend.com/api/v1/check/email"
def should_block(email: str) -> bool:
try:
url = f"{CHECK_URL}/{urllib.parse.quote(email, safe='')}"
req = urllib.request.Request(
url,
headers={
"Authorization": f"Bearer {os.environ['PIISEND_API_KEY']}",
},
)
with urllib.request.urlopen(req, timeout=5) as resp:
data = json.loads(resp.read())
# reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
return data.get("recommendation") == "block"
except Exception:
return False # fail open
def lambda_handler(event, context):
email = event["request"]["userAttributes"].get("email", "")
if email and should_block(email):
raise Exception("Please use a permanent email address.")
return eventAPI key
Set `PIISEND_API_KEY` in Lambda environment variables or Secrets Manager.
Fail-open vs fail-closed
Returning False on errors allows signup — adjust for your risk model.
Pair with verification
Attach to PreSignUp trigger; test with Cognito hosted UI and API signUp flows.
FAQ
Where does the Piisend API key go in AWS Cognito?
Set `PIISEND_API_KEY` in Lambda environment variables or Secrets Manager.
What happens if Piisend is slow or down?
Returning False on errors allows signup — adjust for your risk model.
How does this fit with the rest of AWS Cognito signup?
Attach to PreSignUp trigger; test with Cognito hosted UI and API signUp flows.
What does this snippet block?
Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.
Can I get a full allow / review / block verdict instead?
Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.
Send verification email with Piisend
After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.