💻 Beyond the Console: Mastering the AWS CLI for Lambda
Hey Cloud Builders 👋
Welcome to Day 34 of the #100DaysOfCloud Challenge!
Today, we are moving away from the GUI and into the terminal. While the Console is great for learning, the AWS CLI is how real-world automation happens. We are going to write a Python script, package it into a .zip file, and deploy it as a new Lambda function named devops-lambda-cli.
This task is part of my hands-on practice on the KodeKloud Engineer platform, which I highly recommend for anyone looking to master real-world DevOps scenarios.
🎯 Objective
- Create a local Python script named
lambda_function.py. - Package the script into a deployment archive named
function.zip. - Use the
aws lambda create-functioncommand to deploy the code. - Ensure the function uses the existing
lambda_execution_role. - Verify the function returns the message:
Welcome to KKE AWS Labs!.
💡 Why Packaging & CLI Matter
Not all code can be typed into the online editor. Packaging allows you to include complex dependencies and libraries that aren’t available in the standard AWS environment.
🔹 Key Concepts
-
Deployment Package: A
.zipfile archive containing your function code and any dependencies. Lambda needs this format to understand your application structure. -
The Handler Property: This is the most important CLI flag. It tells Lambda where to start:
file_name.function_name. For us, it will belambda_function.lambda_handler. -
IAM PassRole: To create a function via CLI, your user needs permission to “pass” the
lambda_execution_roleto the Lambda service.
🛠️ Step-by-Step: CLI Deployment Workflow
We’ll move logically from Coding → Zipping → Deploying.
🔹 Phase A: Create and Zip the Script
-
Write the Code: Create the file
lambda_function.pyon youraws-clienthost.import json def lambda_handler(event, context): return { 'statusCode': 200, 'body': json.dumps('Welcome to KKE AWS Labs!') } -
Compress the File: Use the
ziputility to create your archive.zip function.zip lambda_function.py
🔹 Phase B: Retrieve the IAM Role ARN
Lambda requires the full Amazon Resource Name (ARN) of the role.
-
Get ARN: Run the following command to find the ARN for
lambda_execution_role:aws iam get-role --role-name lambda_execution_role --query 'Role.Arn' --output text
🔹 Phase C: Create the Lambda Function
Now, execute the core command to create the function in the cloud. Replace with the string you retrieved in Phase B.
aws lambda create-function
--function-name devops-lambda-cli
--runtime python3.9
--role
--handler lambda_function.lambda_handler
--zip-file fileb://function.zip
⚠️ Pro Tip: Notice the
fileb://prefix. This tells the AWS CLI that the file is binary content, which is required for.zipuploads.
✅ Verify Success
- Invoke via CLI: Test your function without leaving the terminal!
aws lambda invoke --function-name devops-lambda-cli out.txt
cat out.txt
-
Confirm: 🎉 If the output shows the message
Welcome to KKE AWS Labs!, mission accomplished!
📝 Key Takeaways
- 🚀 Automation Ready: Commands used today can easily be put into a shell script for automated deployments.
- 📦 Binary Prefix: Always use
fileb://when passing a local zip file to the CLI. - 🕒 Role Sync: IAM roles can take a few seconds to propagate. If you get an “Access Denied” immediately after creating a role, wait 10 seconds and try again.
🚫 Common Mistakes
-
Missing Handler: If you name your file
app.pybut tell the CLI the handler islambda_function.handler, the code will fail to run. - Zip Structure: When zipping folders, ensure your code file is at the root of the zip, not buried inside a sub-folder.
-
CLI Region: If your role is in
us-east-1but your CLI is pointing tous-west-2, the creation might fail.
🌟 Final Thoughts
You’ve just crossed the bridge into Infrastructure Automation! Using the CLI to manage serverless functions is a standard industry practice that makes your deployments faster, repeatable, and less prone to human error.
🌟 Practice Like a Pro
If you want to try these tasks yourself in a real AWS environment, check out:
👉 KodeKloud Engineer – Practice Labs
It’s where I’ve been sharpening my skills daily!
🔗 Let’s Connect
- 💬 LinkedIn: Hritik Raj
- ⭐ Support my journey on GitHub: 100 Days of Cloud




