Lambda for operations automation
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Operations and security
-
Phase 3: SQL Server on AWS
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Every operations team has a pile of small, boring jobs, like stopping test servers at night, cleaning out old snapshots, tagging new resources or reacting to an alarm. AWS Lambda automation runs them as small functions that need no server to patch, fire on a schedule or an event, and log everything automatically.
You’ll write a Python function that stops running servers tagged AutoStop=true, test it with a dry run, schedule it for 8 p.m. India time with EventBridge Scheduler and read its logs. Then you’ll fix a permissions failure caused by a tag condition, plus the CLI error that everyone hits the first time they invoke Lambda.
| Time | You need | By the end you can |
|---|---|---|
| 50 minutes (two small instances for under an hour) | You need saat-vpc and your laptop signed in as saat-admin, and no Session Manager endpoints this time. The instances cost a few cents, and Lambda and EventBridge Scheduler runs this small sit well inside their monthly allowances. | Write and deploy a Python Lambda function with a least-privilege execution role and invoke it with test payloads. Read its logs, schedule it with EventBridge Scheduler in a time zone, and troubleshoot permission and invocation errors. |
What it is, in one line
AWS Lambda runs your code in response to events or schedules without any servers to manage, which makes it a natural fit for small operations automation jobs.
In Simple Words
The watchman who switches off the lights
An office pays a watchman to walk round at 8 p.m. and switch off the lights in rooms marked “switch off at night”. He leaves rooms without the sign alone, and he writes down in a book which rooms he switched off.
Lambda is that watchman, except he only exists while he’s doing the round and you only pay for those minutes. The AutoStop tag is the sign on the door and CloudWatch Logs is his book.
Hand him a master key and he might switch off the server room by mistake, which is why his role only opens doors that have the sign.
Engineer's View
How Lambda works for operations
A function has code (a zip file or a container image), a runtime such as python3.13, a handler, memory (which also decides the CPU), a timeout (3 seconds by default, up to 15 minutes) and an execution role that gives the code its AWS permissions. Whatever the code prints ends up in CloudWatch Logs under /aws/lambda/FUNCTION.
Triggers include EventBridge Scheduler (cron or rate schedules in any time zone, one-time schedules, retries and dead-letter queues), EventBridge rules for AWS events such as CloudTrail API calls or alarms, S3 events and SQS. Scheduler invokes the function with a role of its own, separate from the function’s role.
A good ops function is idempotent (safe to run twice), supports a dry run, scopes its permissions with conditions such as aws:ResourceTag, handles pagination and fails loudly so errors show up in the metrics (Errors, Throttles, Duration). One CLI detail: AWS CLI v2 expects --payload as base64 unless you add --cli-binary-format raw-in-base64-out.
How it works, step by step
This is what happens every evening at 8.
1Scheduler fires
The schedule’s cron runs in Asia/Kolkata time, so 20:00 is 8 p.m. in India and not UTC. Scheduler assumes its role and calls the function with {"dry_run": false}.
2The function finds tagged servers
boto3 lists the running instances that have AutoStop=true, one page at a time, and prints what it finds.
3It stops only what it may stop
The execution role only allows StopInstances on instances carrying that tag, so even a bug in the code can’t stop production servers.
4Pick jobs like this
Good first functions are small and easy to test:
When a job needs more than 15 minutes, lots of steps or a human approval, reach for a Systems Manager Automation runbook or Step Functions instead.
✅ Checkpoint: You can explain the execution role and the scheduler’s role, where the logs go, and why dry runs and tag conditions matter.
Hands-on lab: AWS Lambda automation that stops test servers
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Sign in and set up:
cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'
$v = Get-Content C:\aws-lab\saat-vpc.json | ConvertFrom-Json
$acct = aws sts get-caller-identity --query Account --output text1Create the execution role
Paste this whole block:
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }' | Set-Content lambda-trust.json
$roleArn = aws iam create-role --role-name saat-lambda-autostop --assume-role-policy-document file://lambda-trust.json --tags Key=course,Value=saatvedha-aws --query Role.Arn --output text
aws iam attach-role-policy --role-name saat-lambda-autostop --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
@'
{
"Version": "2012-10-17",
"Statement": [
{ "Effect": "Allow", "Action": "ec2:DescribeInstances", "Resource": "*" },
{ "Effect": "Allow", "Action": "ec2:StopInstances", "Resource": "arn:aws:ec2:ap-south-1:ACCT:instance/*",
"Condition": { "StringEquals": { "aws:ResourceTag/AutoStop": "TAG_VALUE" } } }
]
}
'@ -replace 'ACCT', $acct | Set-Content autostop-policy-template.json
(Get-Content autostop-policy-template.json -Raw) -replace 'TAG_VALUE', 'true' | Set-Content autostop-policy.json
aws iam put-role-policy --role-name saat-lambda-autostop --policy-name stop-tagged --policy-document file://autostop-policy.json
$roleArnYou should see something like this:
arn:aws:iam::111122223333:role/saat-lambda-autostopAWSLambdaBasicExecutionRole lets the function write logs, and the inline policy lets it list everything while only stopping instances tagged AutoStop=true.
2Deploy the function
Paste this whole block:
@'
import json
import boto3
ec2 = boto3.client('ec2')
def handler(event, context):
dry_run = bool(event.get('dry_run', False))
pages = ec2.get_paginator('describe_instances').paginate(Filters=[
{'Name': 'tag:AutoStop', 'Values': ['true']},
{'Name': 'instance-state-name', 'Values': ['running']},
])
ids = [i['InstanceId'] for page in pages for r in page['Reservations'] for i in r['Instances']]
print(json.dumps({'found': ids, 'dry_run': dry_run}))
if ids and not dry_run:
ec2.stop_instances(InstanceIds=ids)
return {'stopped': [] if dry_run else ids}
'@ | Set-Content lambda_function.py
Compress-Archive -Path lambda_function.py -DestinationPath autostop.zip -Force
Start-Sleep 15
aws lambda create-function --function-name saat-autostop --runtime python3.13 --handler lambda_function.handler --role $roleArn --zip-file fileb://autostop.zip --timeout 30 --tags course=saatvedha-aws --query "[FunctionName,Runtime,Timeout]" --output text
aws lambda wait function-active-v2 --function-name saat-autostop
aws lambda get-function --function-name saat-autostop --query Configuration.State --output textYou should see this:
saat-autostop python3.13 30
ActiveThe 15-second pause gives IAM time to make the new role visible to Lambda. We also raise the timeout from the default 3 seconds, which is too short once an account has a few hundred instances.
3Test with a dry run, then for real
Paste this whole block. It launches one server to stop and one to keep:
$base = aws ssm get-parameter --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --query Parameter.Value --output text
$stopMe = aws ec2 run-instances --image-id $base --instance-type t3.micro --subnet-id $v.PrivateA --security-group-ids $v.AppSg --metadata-options HttpTokens=required --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=stop-me},{Key=AutoStop,Value=true},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=38}]' --query "Instances[0].InstanceId" --output text
$keepMe = aws ec2 run-instances --image-id $base --instance-type t3.micro --subnet-id $v.PrivateA --security-group-ids $v.AppSg --metadata-options HttpTokens=required --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=keep-me},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=38}]' --query "Instances[0].InstanceId" --output text
aws ec2 wait instance-running --instance-ids $stopMe $keepMe
'{ "dry_run": true }' | Set-Content dry.json
'{ "dry_run": false }' | Set-Content real.json
aws lambda invoke --function-name saat-autostop --payload file://dry.json --cli-binary-format raw-in-base64-out response.json | Out-Null
Get-Content response.json
aws lambda invoke --function-name saat-autostop --payload file://real.json --cli-binary-format raw-in-base64-out response.json | Out-Null
Get-Content response.json
aws ec2 wait instance-stopped --instance-ids $stopMe
aws ec2 describe-instances --instance-ids $stopMe $keepMe --query "Reservations[].Instances[].[Tags[?Key=='Name']|[0].Value,State.Name]" --output textYou should see something like this:
{"stopped": []}
{"stopped": ["i-0e1f2a3b4c5d60101"]}
stop-me stopped
keep-me runningThe dry run found the server and changed nothing, and the real run stopped only the tagged one. Now read what the function wrote:
aws logs tail /aws/lambda/saat-autostop --since 10m --format short | Select-String foundYou should see something like this:
2026-10-20T22:41:07 {"found": ["i-0e1f2a3b4c5d60101"], "dry_run": true}
2026-10-20T22:41:09 {"found": ["i-0e1f2a3b4c5d60101"], "dry_run": false}
4Schedule it for 8 p.m. India time
Paste this whole block:
$funcArn = aws lambda get-function --function-name saat-autostop --query Configuration.FunctionArn --output text
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "scheduler.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "aws:SourceAccount": "ACCT" } } } ] }' -replace 'ACCT', $acct | Set-Content sched-trust.json
$schedRole = aws iam create-role --role-name saat-scheduler-autostop --assume-role-policy-document file://sched-trust.json --query Role.Arn --output text
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "FUNC_ARN" } ] }' -replace 'FUNC_ARN', $funcArn | Set-Content sched-policy.json
aws iam put-role-policy --role-name saat-scheduler-autostop --policy-name invoke-autostop --policy-document file://sched-policy.json
@'
{ "Arn": "FUNC_ARN", "RoleArn": "ROLE_ARN", "Input": "{\"dry_run\": false}" }
'@ -replace 'FUNC_ARN', $funcArn -replace 'ROLE_ARN', $schedRole | Set-Content sched-target.json
Start-Sleep 15
aws scheduler create-schedule --name saat-autostop-nightly --schedule-expression "cron(0 20 * * ? *)" --schedule-expression-timezone Asia/Kolkata --flexible-time-window Mode=OFF --target file://sched-target.json | Out-Null
aws scheduler get-schedule --name saat-autostop-nightly --query "[State,ScheduleExpression,ScheduleExpressionTimezone]" --output textYou should see this:
ENABLED cron(0 20 * * ? *) Asia/KolkataOlder setups use EventBridge rules, whose cron always runs in UTC, and teams schedule “14:30” and then forget that means 8 p.m. in India. Scheduler’s time zone setting gets rid of that whole class of mistake, daylight saving included in Regions that have it.

✅ Checkpoint: saat-autostop is Active, a dry run changed nothing, a real run stopped only stop-me, the logs show both runs and saat-autostop-nightly is ENABLED for 20:00 Asia/Kolkata. Keep everything for the break lab.
Break It, Then Fix It
Here’s a permissions failure that looks like a code bug, and a CLI error that looks like a Lambda bug.
1Break it: a tag condition that doesn’t match
Change the policy condition to AutoStop=yes, start stop-me again and run the function:
(Get-Content autostop-policy-template.json -Raw) -replace 'TAG_VALUE', 'yes' | Set-Content autostop-policy.json
aws iam put-role-policy --role-name saat-lambda-autostop --policy-name stop-tagged --policy-document file://autostop-policy.json
aws ec2 start-instances --instance-ids $stopMe | Out-Null
aws ec2 wait instance-running --instance-ids $stopMe
Start-Sleep 10
aws lambda invoke --function-name saat-autostop --payload file://real.json --cli-binary-format raw-in-base64-out response.json --query FunctionError --output text
Get-Content response.jsonYou should see something like this:
Unhandled
{"errorMessage": "An error occurred (UnauthorizedOperation) when calling the StopInstances operation: You are not authorized to perform this operation. User: arn:aws:sts::111122223333:assumed-role/saat-lambda-autostop/saat-autostop is not authorized to perform: ec2:StopInstances on resource: arn:aws:ec2:ap-south-1:111122223333:instance/i-0e1f2a3b4c5d60101 because no identity-based policy allows the ec2:StopInstances action. Encoded authorization failure message: ...", "errorType": "ClientError", ...}
The code found the server, because the filter says true, but the role only allows stopping servers tagged yes, and tag values have to match exactly. At least it fails the safe way, with the scope too narrow and nothing stopped.
Fix: make the condition match the real tag, and agree on tag values once for the whole company:
(Get-Content autostop-policy-template.json -Raw) -replace 'TAG_VALUE', 'true' | Set-Content autostop-policy.json
aws iam put-role-policy --role-name saat-lambda-autostop --policy-name stop-tagged --policy-document file://autostop-policy.json
Start-Sleep 10
aws lambda invoke --function-name saat-autostop --payload file://real.json --cli-binary-format raw-in-base64-out response.json | Out-Null
Get-Content response.jsonYou should see something like this:
{"stopped": ["i-0e1f2a3b4c5d60101"]}2Break it: invoke without the binary format option
Invoke it the way a lot of older guides show:
aws lambda invoke --function-name saat-autostop --payload file://dry.json response.jsonYou’ll get an error like this:
Invalid base64: "{ "dry_run": true }"AWS CLI version 2 treats binary parameters like --payload as base64 by default, so the function was never even called. Fix: add --cli-binary-format raw-in-base64-out to the command, or set it once in your profile with aws configure set cli_binary_format raw-in-base64-out --profile saat-admin.
Clean up:
aws scheduler delete-schedule --name saat-autostop-nightly
aws lambda delete-function --function-name saat-autostop
aws logs delete-log-group --log-group-name /aws/lambda/saat-autostop
aws iam delete-role-policy --role-name saat-scheduler-autostop --policy-name invoke-autostop
aws iam delete-role --role-name saat-scheduler-autostop
aws iam delete-role-policy --role-name saat-lambda-autostop --policy-name stop-tagged
aws iam detach-role-policy --role-name saat-lambda-autostop --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name saat-lambda-autostop
aws ec2 terminate-instances --instance-ids $stopMe $keepMe | Out-Null
aws ec2 wait instance-terminated --instance-ids $stopMe $keepMe
Remove-Item lambda_function.py, autostop.zip, lambda-trust.json, autostop-policy-template.json, autostop-policy.json, dry.json, real.json, response.json, sched-trust.json, sched-policy.json, sched-target.json
.\Find-LabResources.ps1✅ Checkpoint: You fixed an UnauthorizedOperation caused by a tag condition that didn’t match, and the CLI v2 Invalid base64 error. The schedule, function, log group, roles and both servers are gone.
This happened at work
The nightly job that stopped production
A healthcare software company wrote a Lambda function to stop non-production servers at night and save some money. Its logic was to stop every instance whose Environment tag wasn’t “prod”, and the role had ec2:StopInstances on *.
Then a new team launched production servers tagged Environment=Production. At 9 p.m. the function stopped every one of them, the appointment booking API included, right in the evening hours when clinics were confirming the next day’s patients.
The team switched the logic to opt-in, so only AutoStop=true gets stopped, and limited the role with an aws:ResourceTag condition. They also added a dry-run report that’s emailed an hour before each run, and an AWS Organizations tag policy that enforces the allowed tag values.
Common mistakes and troubleshooting
| Mistake | What happens | What to do |
|---|---|---|
| Opt-out logic (“stop everything except prod”) | New or mistagged servers get stopped | Opt-in with an explicit tag |
| StopInstances on * in the role | A code bug can stop production | Scope with aws:ResourceTag conditions |
| Default 3-second timeout | Task timed out on larger accounts | Set a realistic timeout and measure Duration |
| No pagination | Only the first page of instances handled | Use boto3 paginators |
| EventBridge rule cron in UTC | Job runs at the wrong local time | Use Scheduler with a time zone |
| Creating the function right after the role | The role defined for the function cannot be assumed by Lambda | Wait a few seconds after creating the role |
Interview questions
Click a question to open it. Each one is phrased the way an interviewer would ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. Can you explain in simple words what AWS Lambda is?
Why they ask this: They want to know you’ve got the serverless basics, since Lambda is the most common tool for small automation jobs in AWS.
How you might answer: Lambda runs your code when an event happens or on a schedule, and you don’t manage any servers. It scales by itself and you pay per request and for how long the code runs.
For an admin it’s ideal for small jobs like cleaning up resources or sending reports.
2. What is a Lambda execution role, and why does it matter?
Why they ask this: They’re checking you understand how a function gets its permissions, because most real-world Lambda problems come back to this role.
How you might answer: It’s the IAM role the function assumes when it runs, and it decides which AWS actions the code is allowed to take.
I keep it to the least privilege the function needs, so a bug in the code can’t do much damage.
3. A function ran this morning and you want to see what it did. Where would you look?
Why they ask this: They want to know you can find a function’s output and spot problems, since reading the logs is the first step in any Lambda troubleshooting.
How you might answer: I’d open CloudWatch Logs and look in the log group /aws/lambda/FUNCTION_NAME, which has whatever the code printed along with the START, END and REPORT lines.
The REPORT line gives the duration and memory used, so I can tell if it’s getting close to its limits.
2–5 years experience
4. Your manager wants test servers stopped every evening to save money. How would you build that safely with Lambda?
Why they ask this: They want to see you think about safety and not only about getting the job done, because a careless stop script can shut down production.
How you might answer: I’d only stop servers that opt in with a tag, and the function would get a least-privilege role with a tag condition so it can’t touch anything else. I’d add a dry-run mode and handle pagination so no servers get missed.
The schedule would use the right time zone, and I’d keep the logs and set an alert for when the function errors.
5. Your Lambda function fails with AccessDenied. Walk me through how you’d troubleshoot it.
Why they ask this: They want a careful order of checks, since AccessDenied can come from several layers and they want to know you’ll look at all of them.
How you might answer: I’d read the error first to see which action and resource were denied, and then check the execution role’s policies and any conditions on them, like tags or Regions.
If the role looks fine, I’d move on to SCPs and resource policies, and decode the authorization message if the error came with an encoded one.
6. For a nightly cron job, would you use EventBridge Scheduler or an EventBridge rule, and why?
Why they ask this: They’re checking you know the right scheduling tool, because the wrong one can leave jobs running at the wrong local time.
How you might answer: I’d go with EventBridge Scheduler. It supports time zones, one-time schedules, retries and dead-letter queues, and it scales to millions of schedules.
Rules only work in UTC, so I keep those for matching events and not for cron jobs.
Senior level
7. You’re asked to design an event-driven system that fixes risky changes automatically across 30 accounts. How would you build it?
Why they ask this: They want architecture thinking for safe automation at scale, and whether you consider permissions, safety modes and auditing.
How you might answer: I’d set up organization-wide EventBridge rules that send security and config events to a central event bus, and Lambda or Step Functions would apply the fix by assuming a least-privilege role in the target account for each action.
I’d build in dry-run and approval modes, make every action idempotent and add rate limits, with metrics, alarms and an audit trail showing what the system did and when.
8. Your scheduled function is supposed to stop test servers, but on some nights it stops nothing. How would you investigate?
Why they ask this: They want deeper troubleshooting where the cause isn’t obvious, and whether you check the schedule, the code, the data and the permissions.
How you might answer: I’d start with the scheduler’s invocation metrics and the function’s logs for those nights, looking for errors, throttles or timeouts. Then I’d use Config history to see whether the instances were tagged at the time, and check that the filter values and their case match.
Last of all I’d search CloudTrail for any permission changes around those dates.
Quick quiz
Q1. What gives a Lambda function its AWS permissions?
- The caller
- Its execution role
- The VPC
- The scheduler
Show answer
B — The code runs with the execution role.
Q2. Why did StopInstances fail in break 1?
- Wrong Region
- The role’s tag condition said yes, the tag was true
- The function timed out
- No logs
Show answer
B — Condition values are compared exactly.
Q3. What fixed “Invalid base64” when invoking?
- A new role
--cli-binary-formatraw-in-base64-out- A longer timeout
- A new runtime
Show answer
B — CLI v2 treats --payload as base64 by default.
Q4. Why use Scheduler’s time zone setting?
- It is cheaper
- So 20:00 means local time, not UTC
- It is required
- For logs
Show answer
B — Rule cron expressions are UTC only.
Q5. What is the default Lambda timeout?
- 1 second
- 3 seconds
- 60 seconds
- 15 minutes
Show answer
B — Raise it for real jobs.
Quick recap
- Lambda runs small jobs without servers; logs go to CloudWatch automatically.
- The execution role decides what the code can do; scope it with tag conditions.
- Build in a dry run, pagination and a realistic timeout.
- EventBridge Scheduler runs cron in your time zone with its own invoke role.
- CLI v2 needs
--cli-binary-formatraw-in-base64-out for raw JSON payloads.
Next lesson
Lesson 39: Containers on ECS with Fargate → Next you’ll run a container on ECS Fargate behind a load balancer, read its logs and fix the classic task startup failures.
Further reading: AWS Lambda automation
Want to go deeper into AWS Lambda automation? Read What is AWS Lambda? and What is Amazon EventBridge Scheduler?. You can also see the full plan for this course on the AWS Cloud Administration course page.
