Skip to content

Website Change Alerts on AWS Lambda: Function URL Receiver

Receive AyeWatch website change alerts in an AWS Lambda function behind a function URL or API Gateway. Verify the HMAC on the raw body, dedupe in DynamoDB and publish to SNS.

By AyeWatch Team5 min read

Short answer: To receive website change alerts on AWS Lambda, give a Node.js function a function URL with auth type NONE, or an API Gateway route, and paste that URL into AyeWatch. Decode the body if isBase64Encoded is true, verify X-AyeWatch-Signature with timingSafeEqual, skip an update_id already in DynamoDB, publish, then record the update_id and return 200 within 10 seconds.

Function URL or API Gateway

A function URL is the shortest path: one HTTPS endpoint of the form https://URL_ID.lambda-url.REGION.on.aws, built into the function. Set its auth type to NONE. The other type, AWS_IAM, needs requests signed with AWS credentials, which AyeWatch cannot do. With NONE, anyone who finds the URL can call it, so the signature check is your lock.

API Gateway fits when you already run an API there or want a custom domain, throttling or several routes. An HTTP API route with a Lambda integration sends the same payload format 2.0 event as a function URL. A REST API proxy integration sends the older format, which keeps header names in their original case. The handler below copes with both.

The Lambda free tier covers 1 million requests and 400,000 GB-seconds a month. An alert receiver uses a tiny fraction of that.

The handler

Sample rule: Alert me when Ridgeway Freight's tariff page changes a fuel surcharge or adds a new lane. Ignore news posts and job ads.

This Node.js 22 handler uses the AWS SDK v3 clients that the Lambda Node.js runtime already includes. Set AYEWATCH_SECRET, TABLE and TOPIC_ARN as environment variables.

// index.mjs
import crypto from "node:crypto";
import { DynamoDBClient, GetItemCommand, PutItemCommand } from "@aws-sdk/client-dynamodb";
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";

const ddb = new DynamoDBClient({});
const sns = new SNSClient({});

export const handler = async (event) => {
  // 1. Raw body. Lambda base64-encodes it only when it is binary.
  const raw = Buffer.from(event.body || "", event.isBase64Encoded ? "base64" : "utf8");

  // 2. Signature. REST APIs keep header case, so lowercase the names first.
  const headers = {};
  for (const [name, value] of Object.entries(event.headers || {})) headers[name.toLowerCase()] = value;
  const expected = Buffer.from(
    "sha256=" + crypto.createHmac("sha256", process.env.AYEWATCH_SECRET).update(raw).digest("hex")
  );
  const got = Buffer.from(headers["x-ayewatch-signature"] || "");
  if (got.length !== expected.length || !crypto.timingSafeEqual(got, expected)) {
    return { statusCode: 401, body: "Bad signature" };
  }

  // 3. Skip an update_id you already published.
  const alert = JSON.parse(raw.toString("utf8"));
  const key = { update_id: { N: String(alert.update_id) } };
  const done = await ddb.send(new GetItemCommand({ TableName: process.env.TABLE, Key: key, ConsistentRead: true }));
  if (done.Item) return { statusCode: 200, body: "Already handled" };

  // 4. Act. If the publish fails or the function times out, nothing is recorded,
  //    AyeWatch gets an error and its retry publishes the alert.
  try {
    await sns.send(new PublishCommand({
      TopicArn: process.env.TOPIC_ARN,
      Message: [alert.headline || alert.topic_name, alert.content || "", alert.topic_name].join("\n\n"),
    }));
  } catch (err) {
    return { statusCode: 502, body: "Publish failed" };
  }

  // 5. Record the update_id, then answer.
  await ddb.send(new PutItemCommand({ TableName: process.env.TABLE, Item: key }));
  return { statusCode: 200, body: "ok" };
};

The DynamoDB table needs a partition key named update_id of type Number. The function's role needs dynamodb:GetItem, dynamodb:PutItem and sns:Publish on those two resources. Subscribe an email address, a queue or another function to the SNS topic.

The update_id is written only after the publish succeeds. If the write itself fails, the retry publishes the alert a second time, so a rare repeat is possible but a lost alert is not.

Stay inside the 10 seconds

A new function's timeout is 3 seconds. Raise it to about 8 seconds under the function's general configuration, so a slow cold start or a slow SNS call ends in an error AyeWatch can retry, not in a reply that arrives after AyeWatch gave up. Lambda stops the work when the handler returns, so there is no "answer now, finish later" here. For long work, publish to SNS or a queue as above and let another function do it.

Mistakes that break the signature

  • Parsing before verifying. JSON.stringify(JSON.parse(body)) is not always the same bytes. Verify raw, then parse.
  • Forgetting base64. If something in front marks the body as binary, event.body is base64 text. The first line of the handler handles that.
  • Looking up X-AyeWatch-Signature with one fixed case. Function URLs lowercase header names and REST APIs do not, so normalize them.
  • A plain === on the signature. Use timingSafeEqual after a length check.

Limits and cost

  • One webhook per account. Every monitor posts to this function. Branch on topic_id if monitors need different topics or queues.
  • No redirects. A custom domain or API mapping that answers with a 3xx fails the delivery at once, with no retry.
  • The DynamoDB table grows. One small item per alert. Turn on DynamoDB TTL with an expiry attribute if you want it to clean itself.
  • Public pages only. AyeWatch reads public pages and cannot sign in.

AyeWatch Pro is $9/mo with 50 topics and hourly checks; Pro+ is $19/mo with 200 topics and ASAP mode. Every plan starts with a 7-day free trial. Eight tariff pages in separate web page monitors, checked twice a day at 1 credit a check, use 8 × 60 = 480 credits a month on Pro. On AWS, the receiver fits in the free tier.

Deploy the receiver

  1. Create a Node.js 22 function and paste the handler as index.mjs.
  2. Create the DynamoDB table with partition key update_id (Number) and an SNS topic, then grant the role the three permissions.
  3. Under the function's configuration, create a function URL with auth type NONE and set the timeout to about 8 seconds.
  4. In AyeWatch, open Notification settings, Webhook, paste the function URL and select Save Webhook. Reveal and copy the signing secret.
  5. Add AYEWATCH_SECRET, TABLE and TOPIC_ARN as environment variables.
  6. Wait for a real update and check the function's CloudWatch logs for a 200.
Function URLAPI Gateway HTTP APIAPI Gateway REST API
Auth AyeWatch can useNONENo authorizer on the routeNo authorizer, no API key required
Event format2.02.01.0
Header namesLowercaseLowercaseAs sent
Custom domainNoYesYes
Good forOne receiver, least setupSeveral routesExisting REST APIs

Start a monitor in under a minute

Describe a topic or paste a URL. Cancel before the trial ends and you pay nothing.

Try it free for 7 days

The bottom line

A function URL, one DynamoDB table and one SNS topic give you a verified receiver that costs nothing at alert volumes. Raise the timeout, record each update_id only after the publish, and let SNS carry the alert wherever it needs to go.

FAQ

Which auth type should a Lambda function URL use for a webhook?

NONE. AWS_IAM requires requests signed with AWS credentials, which a webhook sender cannot produce. Check the HMAC signature in the handler instead.

Why is my Lambda webhook body base64-encoded?

Lambda base64-encodes the body when it treats it as binary and sets isBase64Encoded to true. Decode it with Buffer.from(body, 'base64') before you verify.

How do I stop duplicate webhook processing in Lambda?

Look up the update_id in DynamoDB and return 200 if it is there. Otherwise do the work, then write the update_id. If the work fails or the function times out, nothing is written, so AyeWatch's retry runs it again.

Can Lambda reply first and finish the work later?

Not reliably. Work stops when the handler returns. Hand slow work to SNS, SQS or another function, then reply.

Does the same code work behind API Gateway?

Yes. It reads body, isBase64Encoded and headers, which both event formats have, and it lowercases header names because REST APIs keep the original case.

website change alerts aws lambdalambda function url webhookaws lambda webhook hmac signature verificationapi gateway webhook raw body base64dynamodb webhook idempotency

Related articles