π» 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 <ROLE_ARN> with the string you retrieved in Phase B.
aws lambda create-function \
--function-name devops-lambda-cli \
--runtime python3.9 \
--role <ROLE_ARN> \
--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




Top comments (0)