Pattern 02 · Event-driven

Double opt-in without a mail server

In part one the newsletter form wrote straight to DynamoDB with no code in the hot path. Now every sign-up gets a confirmation email, and nobody is subscribed until they click. One small function, zero servers, and the sign-up path didn't change at all.

Serverless Society · November 2026 · 8 min read

TL;DR. Keep the write storage-first. React to it afterwards: a DynamoDB Stream triggers a Lambda that issues a token and sends the email with SES. The confirm link opens a page whose button calls an endpoint that is, again, just API Gateway and DynamoDB. If email sending breaks, nobody's sign-up is lost.

Why double opt-in

A form that accepts any address will eventually collect addresses that aren't the visitor's: typos, pranks, bots. Emailing those hurts the people on the receiving end and your sender reputation. Double opt-in fixes it: an address only joins the list after its owner clicks a link sent to that address. It also gives you a clean record of consent.

The wiring

Double opt-in flow The sign-up path writes a pending item to DynamoDB. The table's stream triggers a Lambda function for new items only; it stores a token and sends a confirmation email through Amazon SES. The subscriber opens the link, clicks a button, and API Gateway updates the item to confirmed with a conditional UpdateItem. SYNC · unchanged from part one POST /api/subscribe API Gateway DynamoDB status: pending ASYNC · new Lambda token · send · start TTL Stream (INSERT only) Amazon SES DKIM-signed mail Inbox SQS failure queue Click → POST /api/confirm API Gateway → conditional UpdateItem
Only the dashed and orange boxes are new compute. Everything the visitor waits on is still a managed service talking to another managed service.

Step by step

1. React to the write, don't block it

The table now has a stream (NEW_IMAGE). A Lambda event source mapping reads it, with a filter so the function only wakes up for brand-new items. The function's own updates to the item would otherwise trigger it again, and filtering at the source means those never cost an invocation:

ConfirmStreamMapping:
  Type: AWS::Lambda::EventSourceMapping
  Properties:
    EventSourceArn: !GetAtt SubscribersTable.StreamArn
    FilterCriteria:
      Filters:
        - Pattern: '{"eventName": ["INSERT"]}'
    MaximumRetryAttempts: 3
    BisectBatchOnFunctionError: true
    FunctionResponseTypes: [ReportBatchItemFailures]
    DestinationConfig:
      OnFailure:
        Destination: !GetAtt ConfirmFailureQueue.Arn

Partial batch responses mean one bad record is retried on its own instead of dragging its batch-mates along, and anything that still fails lands in an SQS queue instead of disappearing.

2. Issue a token, idempotently

The function generates a random token (secrets.token_urlsafe(32)) and stores it with a conditional update: only if the item is still pending and doesn't have a token yet. Stream records can be delivered more than once, so the condition is what makes a retry safe. If the update fails because a token already exists and no email has gone out, the function reuses that token. One subscriber, one link, however many retries.

3. Send, then start the clock

The email goes out through SES from hello@serverlesssociety.com, DKIM-signed by the domain. Only after SES accepts it does the function set sentAt and expiresAt (7 days out). DynamoDB's TTL feature deletes unconfirmed items when expiresAt passes, for free.

The ordering matters. If expiresAt were set before sending, a sending outage would quietly delete sign-ups that never got their email. This way, an item that was never emailed is never expired. It waits, stored, until it can be sent.

4. Confirm with a click, not a fetch

Corporate mail filters open links in emails to scan them. If the link itself confirmed the subscription, the scanner would confirm it, and double opt-in would mean nothing. So the link opens a static page, /newsletter/confirm/, with a Yes, subscribe me button. The button posts the email and token to /api/confirm. The page also strips the token from the address bar as soon as it loads.

The confirm endpoint is part one's pattern again: API Gateway validates the body (the token must be exactly 43 URL-safe characters) and calls DynamoDB directly:

"UpdateExpression": "SET #s = :confirmed, confirmedAt = :now REMOVE confirmToken, expiresAt",
"ConditionExpression": "confirmToken = :t AND #s = :pending"

Removing expiresAt cancels the TTL deletion; removing the token makes the link single-use. A wrong, reused or expired token fails the condition and gets one generic 400. No Lambda, and no way to tell which of those it was.

Things we learned the hard way

Control plane vs data plane

PlaneWhatWhen
controlStream, TTL, function, event source mapping, SES identity, confirm method, IAM rolesOne reviewed CloudFormation change set
dataGetRecords, UpdateItem, ses:SendEmail, and on click UpdateItem againEvery sign-up and every confirmation

What it costs

Per sign-up: a few stream reads (free for Lambda triggers), one short Lambda invocation (well inside the free tier at newsletter scale), one SES email at $0.10 per thousand, two small DynamoDB writes, and one API call on confirm. A thousand sign-ups costs on the order of ten cents, nearly all of it email. Check current SES and Lambda pricing for your region.

Well-Architected checklist

When not to do this

Join the field notes →  ·  Part one: storage-first APIs  ·  All posts