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

AWS Lambda Triggers: S3, EventBridge, SQS and SNS Explained

A function alone does nothing. Something must call it. This chapter wires the common triggers: S3 (image upload), EventBridge schedule (link to the EC2 start/stop alarm chapter), SQS and SNS. You also learn cold starts, timeouts, retries, layers and environment variables.

What you'll learn in this chapter

  • S3 event → Lambda image-resize pattern
  • EventBridge schedule for EC2 start/stop
  • SQS and SNS as triggers
  • Cold starts, timeouts, retries
  • Layers and environment variables

1. A story: photos that resize themselves

Meera's campus app stores student photos in S3. Full-size uploads are 5 MB. The student list needs 200 KB thumbnails. She does not want a server watching a folder. She wants: photo lands in S3 → thumbnail appears.

That is an S3 trigger on ObjectCreated, pointing at a Lambda that reads the object, resizes it, and writes to a thumbs/ prefix.

2. Trigger map

TriggerWhen it firesTypical use
Amazon S3Object created/removed in a bucketImage resize, virus scan, ETL kickoff
Amazon EventBridge Scheduler / rulesCron or rate, or AWS event bus eventsNightly reports, EC2 start/stop
Amazon SQSMessages available in a queueSmooth bursts, retries with backoff
Amazon SNSMessage published to a topicFan-out alerts to Lambda + email + SMS
API GatewayHTTP request to a routePublic or private HTTP APIs
S3 upload to thumbnailUser PUTs photo.jpginto s3://pics/raw/S3 emits ObjectCreatedeventLambda downloadsraw objectPillow/Sharp resizesto 400 pxLambda PUTss3://pics/thumbs/
No polling loop. S3 calls Lambda for you.

3. Cold starts, timeouts and retries

Why do we need this?

The first request after idle can be slower. Timeouts cut runaway work. Retries can double-charge or double-write if your code is not safe.

What is it?

  • Cold start: AWS creates a new execution environment, pulls your package, starts the runtime. Then your handler runs. Later calls on the same environment are warm and faster.
  • Bigger packages and Java/.NET often cold-start slower than Python/Node. Provisioned concurrency keeps environments warm for a price.
  • Timeout: function setting, max 15 minutes. Set it above your p99 duration or real work dies mid-flight.
  • Retries: async invokes (S3, SNS, EventBridge) retry twice by default. SQS uses the queue redrive policy. Make handlers idempotent (safe to run twice).

4. Layers and environment variables

What is it?

A layer is a zip of libraries you attach to many functions (up to 5). Example: a Pillow layer shared by three image functions. Keep the function zip small.

Environment variables store config (bucket names, log level). Total size max 4 KB. Do not put passwords here — use Secrets Manager or SSM.

5. Lab A: S3 → Lambda (notify only)

How to do it (step by step)

Full Pillow resize needs a layer; this lab proves the trigger with a log line. Region ap-south-1.

  1. Create bucket yourname-lam-pics, Block Public Access on.
  2. Create Lambda s3-notify-lab, Python 3.12, basic execution role.
  3. Code:
def lambda_handler(event, context):
    for r in event.get("Records", []):
        b = r["s3"]["bucket"]["name"]
        k = r["s3"]["object"]["key"]
        print(f"uploaded s3://{b}/{k}")
    return {"ok": True}
  1. Deploy. Configuration → Permissions → role → Add permissions → Attach policies → AmazonS3ReadOnlyAccess (lab only). Why: later labs that download need s3:GetObject.
  2. Lambda → Add trigger → S3 → bucket yourname-lam-pics → Event type All object create events → Suffix .jpg → Add. Confirm the recursive warning (do not write back to the same prefix you listen on).
  3. Upload: aws s3 cp cat.jpg s3://yourname-lam-pics/raw/cat.jpg.
  4. CloudWatch logs for s3-notify-lab should show uploaded s3://yourname-lam-pics/raw/cat.jpg within a minute.

6. Lab B: EventBridge schedule → Lambda (EC2 echo)

In the CloudWatch EC2 alarm chapter you stopped idle instances with alarms. Schedules are cleaner for fixed office hours. This lab only prints; add StartInstances/StopInstances when ready.

  1. Create Lambda ec2-schedule-echo that prints event and returns ok.
  2. EventBridge → Schedules → Create schedule. Name office-open-echo. Recurring — Cron: 0 9 * * ? *. Time zone Asia/Calcutta. Why: office 9 AM India.
  3. Target: AWS Lambda Invoke → pick ec2-schedule-echo → Create.
  4. For a quick test use rate(5 minutes), watch logs, then switch back or delete.

7. SQS and SNS in one minute

  • SQS trigger: Lambda polls the queue for you (event source mapping). Good when producers are bursty and you want retry + DLQ.
  • SNS trigger: publish once, many subscribers (Lambda, email, HTTPS). Good for alarms and fan-out.
  • Permission: the trigger adds a resource-based policy on the function so that service may invoke it.

Cleanup

  1. Delete schedules, remove S3 trigger, delete functions, empty and delete the bucket.
  2. Detach extra IAM policies; delete unused roles.

8. Interview questions

Q. Name four Lambda triggers.

S3, EventBridge, SQS, SNS (also API Gateway, DynamoDB Streams, ALB…).

Q. What is a cold start?

First invoke on a new environment: download package, start runtime, then handler. Slower than warm invokes.

Q. Why is idempotency important?

Async retries can deliver the same event more than once. Safe handlers do not create duplicate rows or charges.

Q. S3 trigger recursive risk?

If Lambda writes back to the same prefix it listens on, it can loop forever. Write to another prefix or bucket.

Q. Layer vs packing deps in the function zip?

Layer = shared libraries across functions, smaller function zips. Function zip = simple, but duplicated.

Try at home

  1. Add a second EventBridge schedule that runs at 21:00 Asia/Calcutta and logs "office closed".
  2. Create an SNS topic, subscribe the Lambda, publish a message, confirm the log.

In short

  1. Triggers start Lambda; S3, EventBridge, SQS, SNS are the daily ones.
  2. Cold start = new environment; design for warm and cold.
  3. Timeouts and retries need idempotent code.
  4. Layers share libs; env vars hold non-secret config.

Practice Tasks

About this scenario

Dublin Fitness Club is a fitness business in Dublin. Siobhan Murphy looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-west-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 eu-west-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 dublinfit-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 Change timeout and memory

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

What to do: Set dublinfit-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.

Medium Producer may only send

Why: The web app should add jobs but never read or delete them.

What to do: Allow sqs:SendMessage on arn:aws:sqs:eu-west-1:132788974236:dublinfit-orders 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 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:eu-west-1:132788974236:dublinfit-orders to dublinfit-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 Create a Lambda function

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

What to do: Create function dublinfit-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::132788974236: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=dublinfit-orders on dublinfit-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 dublinfit-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 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 dublinfit-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::dublinfit-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: S3 ObjectCreated trigger. Manual thumbnail jobs miss uploads. Events never sleep. S3 can invoke Lambda on ObjectCreated for a prefix or suffix. Add trigger → S3 → bucket → event type → prefix images/ suffix .jpg. Upload hero.jpg → Lambda writes thumbs/hero.jpg.
Revision card: S3 ObjectCreated trigger · Download PNG · Open card page
Revision card: EventBridge schedules. Cron on one EC2 dies with the instance. Cloud schedules stay. EventBridge Scheduler (or rules) invokes Lambda on a cron or rate. Create schedule → cron 0 21 * * ? * Asia/Calcutta → target Lambda. Nightly stop-dev Lambda runs at 9 PM India time.
Revision card: EventBridge schedules · Download PNG · Open card page
Revision card: SQS and SNS with Lambda. Bursty producers need a buffer. Many listeners need fan-out. SQS queues work; SNS topics notify many subscribers including Lambda. Event source mapping for SQS. SNS subscription protocol Lambda. Order service publishes to SNS → Lambda email + Lambda analytics both run.
Revision card: SQS and SNS with Lambda · Download PNG · Open card page
Revision card: Cold starts, layers, retries. First invoke after idle feels slow. Shared libs bloat every zip. Cold start = new environment. Layers share libraries. Async invokes retry on failure. Keep packages small; put heavy deps in a layer; design handlers idempotent. S3 trigger fails twice then can go to a DLQ you configure.
Revision card: Cold starts, layers, retries · Download PNG · Open card page
Comparison: Synchronous invoke vs Asynchronous invoke. Synchronous invoke: Caller waits for the response. Asynchronous invoke: Event is queued; Lambda runs later.
Comparison: Synchronous invoke vs Asynchronous invoke · Download PNG · Open card page
Comparison: Amazon SQS vs Amazon SNS. Amazon SQS: Queue: buffer work for consumers. Amazon SNS: Topic: fan-out one message to many.
Comparison: Amazon SQS vs Amazon SNS · Download PNG · Open card page
Interview card: Why avoid writing back to the same S3 prefix you listen on?. Your write can create another ObjectCreated event and recurse. Bills and throttling explode until you stop the trigger. Read images/; write thumbs/ or another bucket.
Interview card: Why avoid writing back to the same S3 prefix you listen on? · Download PNG · Open card page
Interview card: What is a cold start?. First invoke on a fresh environment loads runtime and your package. Later warm invokes reuse the environment and are faster. Shrink deps; provisioned concurrency only when latency SLAs need it.
Interview card: What is a cold start? · Download PNG · Open card page
Interview card: SQS vs SNS for Lambda — when each?. SQS buffers and retries per consumer. SNS fans out one message to many. Use SQS when work must be processed with backlog. Use SNS for notify-many. Checkout → SQS → worker Lambda. Order placed → SNS → email + stock Lambdas.
Interview card: SQS vs SNS for Lambda — when each? · Download PNG · Open card page
Interview card: Why must async handlers be idempotent?. Retries can deliver the same event more than once. Re-applying the same work must be safe. Key writes by event id; skip if already processed.
Interview card: Why must async handlers be idempotent? · Download PNG · Open card page

More cards: Ravi Sir's Cards.