DEV Community

Cover image for πŸ“œ AWS 134: CLI Command Center - Deploying Lambda via Zipped Packages
Hritik Raj
Hritik Raj

Posted on

πŸ“œ 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 <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

Enter fullscreen mode Exit fullscreen mode

⚠️ 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

Enter fullscreen mode Exit fullscreen mode

  • 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

Top comments (0)