📜 AWS 134: CLI Command Center – Deploying Lambda via Zipped Packages

AWS

💻 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-function command 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 .zip file 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 be lambda_function.lambda_handler.

  • IAM PassRole: To create a function via CLI, your user needs permission to “pass” the lambda_execution_role to 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.py on your aws-client host.

    import json
    
    def lambda_handler(event, context):
        return {
            'statusCode': 200,
            'body': json.dumps('Welcome to KKE AWS Labs!')
        }
    
  • Compress the File: Use the zip utility 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 .zip uploads.

✅ 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.py but tell the CLI the handler is lambda_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-1 but your CLI is pointing to us-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

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

From Prototype to Production: Overcoming 3D Printing’s Scalability Challenges

Next Post
working-better-together:-automation-and-conversation

Working Better Together: Automation and Conversation

Related Posts