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
| Trigger | When it fires | Typical use |
|---|---|---|
| Amazon S3 | Object created/removed in a bucket | Image resize, virus scan, ETL kickoff |
| Amazon EventBridge Scheduler / rules | Cron or rate, or AWS event bus events | Nightly reports, EC2 start/stop |
| Amazon SQS | Messages available in a queue | Smooth bursts, retries with backoff |
| Amazon SNS | Message published to a topic | Fan-out alerts to Lambda + email + SMS |
| API Gateway | HTTP request to a route | Public or private HTTP APIs |
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.
- Create bucket
yourname-lam-pics, Block Public Access on. - Create Lambda
s3-notify-lab, Python 3.12, basic execution role. - 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}
- Deploy. Configuration → Permissions → role → Add permissions → Attach policies →
AmazonS3ReadOnlyAccess(lab only). Why: later labs that download need s3:GetObject. - 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). - Upload:
aws s3 cp cat.jpg s3://yourname-lam-pics/raw/cat.jpg. - CloudWatch logs for
s3-notify-labshould showuploaded s3://yourname-lam-pics/raw/cat.jpgwithin 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.
- Create Lambda
ec2-schedule-echothat printseventand returns ok. - EventBridge → Schedules → Create schedule. Name
office-open-echo. Recurring — Cron:0 9 * * ? *. Time zone Asia/Calcutta. Why: office 9 AM India. - Target: AWS Lambda Invoke → pick
ec2-schedule-echo→ Create. - 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
- Delete schedules, remove S3 trigger, delete functions, empty and delete the bucket.
- 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
- Add a second EventBridge schedule that runs at 21:00 Asia/Calcutta and logs "office closed".
- Create an SNS topic, subscribe the Lambda, publish a message, confirm the log.
In short
- Triggers start Lambda; S3, EventBridge, SQS, SNS are the daily ones.
- Cold start = new environment; design for warm and cold.
- Timeouts and retries need idempotent code.
- Layers share libs; env vars hold non-secret config.
Practice Tasks
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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- 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.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- 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.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- 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.










More cards: Ravi Sir's Cards.