Ravindra BagaleCourses & study guides मराठी Track your progress

Project: Build a Scheduled Cleanup Job with EventBridge, Lambda and DynamoDB (Source Code)

Apps delete old sessions and logs at night. EventBridge schedule runs your Lambda on a cron; Lambda scans DynamoDB and deletes or archives old rows.

What you'll learn in this chapter

  • EventBridge rule schedule
  • Cleanup Lambda modes delete/flag
  • Seed data and verify
  • Disable rule and cleanup

1. Everyday example

Why do we need this?

Leaving lab rows forever costs nothing huge on-demand — but production tables grow. A nightly job keeps them healthy.

What is it?

rate(1 hour) or cron for the lab → Lambda Scan → delete or set status=archived when createdAt older than MAX_AGE_SECONDS.

Download: scheduled-cleanup.zip — handler.py + seed.py

2. Lab

How to do it (step by step)

A. Table + seed

  1. Table yourname-cleanup, PK itemId.
  2. Run seed logic (temporary test event or paste seed.py once) so some rows are older than 1 day.

B. Lambda + rule

  1. Function handler.py. Env TABLE_NAME, MAX_AGE_SECONDS=86400, MODE=delete.
  2. EventBridge → Rules → Schedule → rate(5 minutes) for the lab (not only nightly). Target = Lambda.
  3. Wait for an invoke. Check CloudWatch logs and table row count.

Ravindra Bagale's Tip

After the lab, disable or delete the rule immediately so it does not keep running.

Cleanup

  1. Disable/delete EventBridge rule. Delete Lambda, table, role.

3. Interview

Q. Why EventBridge not a forever while-loop on EC2?

Serverless schedule; no idle server cost.

Q. Scan caution?

Fine for tiny lab tables; production uses TTL or sparse GSIs.

Q. delete vs flag?

Flag keeps audit; delete frees storage.

Q. Idempotency?

Running twice should be safe — already deleted rows simply vanish.

Q. Cleanup?

Rule first, then Lambda/table.

Try at home

  1. Switch MODE=flag and confirm status archived.
  2. Add DynamoDB TTL as a second line of defence.

In short

  1. EventBridge → Lambda → cleanup DynamoDB.
  2. Disable schedules after labs.
  3. Prefer TTL for pure expiry.
  4. Clean up.

Practice Tasks

About this scenario

Maple Leaf Books is a bookselling business in Toronto. Emily Tremblay looks after its IT and cloud work. They count money in CAD and run their AWS resources in the ca-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy List functions

Why: See which functions exist in this Region.

What to do: List the Lambda functions in ca-central-1.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Invoke a function

Why: Invoke runs the function now and saves its answer in a file.

What to do: Invoke mapleleafbooks-orders-fn and save the response to out.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Trust policy for a Lambda role

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets lambda.amazonaws.com assume the role.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Put an item

Why: DynamoDB JSON shows the type of each value: S for string, N for number.

What to do: Put an item into mapleleafbooks-orders with orderId "o-101" (string) and amount 2500 (number).

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Change timeout and memory

Why: More memory also gives more CPU; timeout stops runaway runs.

What to do: Set mapleleafbooks-orders-fn to timeout 30 seconds and memory 512 MB.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Let a function write logs

Why: Without these actions, Lambda logs never appear in CloudWatch.

What to do: Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Create a Lambda function

Why: Lambda needs the code zip, a runtime, a handler name and an execution role.

What to do: Create function mapleleafbooks-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::102932412000:role/lambda-basic, code in function.zip.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Set environment variables

Why: Keep settings like table names out of the code.

What to do: Set the environment variable TABLE=mapleleafbooks-orders on mapleleafbooks-orders-fn.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Invoke with a JSON payload

Why: In CLI v2, raw JSON needs --cli-binary-format raw-in-base64-out.

What to do: Invoke mapleleafbooks-orders-fn with payload {"orderId":"o-101"} (raw JSON) and save the reply in out.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Lambda GB-seconds

Why: Lambda bills memory × time.

What to do: 1,000,000 invocations, 512 MB memory, 200 ms each. How many GB-seconds?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Connect an SQS queue to Lambda

Why: An event source mapping makes Lambda poll the queue and process messages in batches.

What to do: Create an event source mapping from queue arn:aws:sqs:ca-central-1:102932412000:mapleleafbooks-orders to mapleleafbooks-orders-fn with batch size 10.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Let S3 call your function

Why: A resource-based permission is needed before S3 can invoke the function.

What to do: Add permission statement s3invoke to mapleleafbooks-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::mapleleafbooks-uploads.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: Scheduled cleanup job. Apps delete old sessions and temp rows at night. A forever while-loop on one EC2 dies with the instance. EventBridge schedule (cron or rate) invokes Lambda. Lambda finds old DynamoDB items and deletes or archives them. Table with createdAt or expiresAt, Lambda role DeleteItem/Scan or Query, rule cron(0 21 * * ? *) Asia/Calcutta → target Lambda. Seed 3 old rows; wait for the rule (or run Test); rows disappear; then disable the rule before you leave.
Revision card: Scheduled cleanup job · Download PNG · Open card page
Revision card: Why EventBridge Scheduler?. OS cron on a student laptop is off when the laptop sleeps. Cloud schedules stay on. Amazon EventBridge Scheduler (or rules) invokes targets on a cron or rate expression in a timezone you choose. Create schedule → cron 0 21 * * ? * → timezone Asia/Calcutta → flexible window off → target your Lambda ARN. CloudWatch Logs for the function show a nightly START RequestId around 21:00 IST.
Revision card: Why EventBridge Scheduler? · Download PNG · Open card page
Revision card: Scan caution on cleanup. Deleting “old” rows often starts with a Scan, which reads the whole table and costs as it grows. Lab Scan with a filter on createdAt is OK for tiny seed data. Production should Query a GSI on expiry or use DynamoDB TTL. Prefer TTL attribute expiresAt for pure expiry. For “archive then delete”, Query a date GSI in pages of 100. 100-item lab cleanup finishes in one Scan page; a 10 GB table would need a better key design first.
Revision card: Scan caution on cleanup · Download PNG · Open card page
Comparison: DynamoDB TTL vs EventBridge + Lambda delete. DynamoDB TTL: DynamoDB deletes expired items in the background. EventBridge + Lambda delete: Your Lambda runs on a schedule and deletes/archives.
Comparison: DynamoDB TTL vs EventBridge + Lambda delete · Download PNG · Open card page
Interview card: Why EventBridge instead of a loop on EC2?. EC2 cron dies when the instance stops; you pay idle hours; patching is on you. EventBridge invokes Lambda only when the schedule fires — serverless ops. cron(0 21 * * ? *) Asia/Calcutta → Lambda cleanup target.
Interview card: Why EventBridge instead of a loop on EC2? · Download PNG · Open card page
Interview card: When is DynamoDB TTL better than a cleanup Lambda?. When you only need eventual delete by an expiresAt epoch and no extra archive logic. TTL is free background deletion; not a precise alarm clock. Session tokens: set expiresAt=now+900 and enable TTL on that attribute.
Interview card: When is DynamoDB TTL better than a cleanup Lambda? · Download PNG · Open card page

More cards: Ravi Sir's Cards.