Hands-On: Deploy Your First Function
Write, deploy, and invoke a function from the terminal in about five minutes.
1 min read
You'll need the role ARN from the IAM tutorial (notesnap-lambda-role). Save this as lambda_function.py:
Python
import json
def lambda_handler(event, context):
name = event.get("name", "world")
return {"greeting": f"Hello, {name}!"}Zip it, create the function, then call it:
Try it
Terminal
zip function.zip lambda_function.py
aws lambda create-function \
--function-name hello-notesnap \
--runtime python3.13 \
--handler lambda_function.lambda_handler \
--role arn:aws:iam::123456789012:role/notesnap-lambda-role \
--zip-file fileb://function.zip
aws lambda invoke \
--function-name hello-notesnap \
--cli-binary-format raw-in-base64-out \
--payload '{"name": "Sam"}' \
response.json
cat response.jsonReading the command
--handler lambda_function.lambda_handlermeans file name.function name.--roleis the IAM role the function runs as. It decides what the code is allowed to touch.--cli-binary-format raw-in-base64-outlets you pass plain JSON as the payload.
Updating code later: re-zip and run aws lambda update-function-code --function-name hello-notesnap --zip-file fileb://function.zip.
Where are my print()s? In CloudWatch Logs, under the log group /aws/lambda/hello-notesnap. You can
stream them live with aws logs tail /aws/lambda/hello-notesnap --follow.
Key takeaways
- A deployment is just your code zipped, plus a runtime, handler, and role.
- `aws lambda invoke` is the quickest way to test a function.
- `print()` output and errors land in CloudWatch Logs.
Check your understanding
Pick an answer to see the explanation.
1. Your file is app.py and your function is main. What's the handler setting?