Skip to main content

Command Palette

Search for a command to run...

Day 34: Create a Lambda Function Using CLI

Updated
•2 min read
Day 34: Create a Lambda Function Using CLI
T

Cloud & SRE specializing in AWS and DevOps. I share my ongoing learning journey through practical tutorials and insights. Let's grow together.

Lab Information

The Nautilus DevOps team continues to explore serverless architecture by setting up another Lambda function. This time, the task must be completed using the AWS Console to familiarize the team with the web interface. The function will return a custom greeting and demonstrate the capabilities of AWS Lambda effectively.

Create Python Script: Create a Python script named lambda_function.py with a function that returns the body Welcome to KKE AWS Labs! and status code 200.

Zip the Python Script: Zip the script into a file named function.zip.

Create Lambda Function: Create a Lambda function named xfusion-lambda-cli using the zipped file and specify Python as the runtime.

IAM Role: Use the IAM role named lambda_execution_role.

Use AWS CLI which is already configured on the aws-client host.

Lab Solutions

🔹 Step 1: Create the Python script

On the aws-client host, run:

cat << 'EOF' > lambda_function.py
def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "Welcome to KKE AWS Labs!"
    }
EOF

Verify the file:

cat lambda_function.py

🔹 Step 2: Zip the Python script

zip function.zip lambda_function.py

# Verify:

ls -l function.zip

🔹 Step 3: Get the IAM role ARN

You must use the existing role lambda_execution_role.

Run:

aws iam get-role \
  --role-name lambda_execution_role \
  --query "Role.Arn" \
  --output text

📌 Copy the ARN (you’ll use it in the next command).

🔹 Step 4: Create the Lambda function using the zip file

aws lambda create-function \
  --function-name xfusion-lambda-cli \
  --runtime python3.9 \
  --role arn:aws:iam::831788756997:role/lambda_execution_role \
  --handler lambda_function.lambda_handler \
  --zip-file fileb://function.zip

✅ If successful, AWS will return a JSON output with function details.

🔹 Step 5: Verify the Lambda function (optional but recommended) Invoke the function:

aws lambda invoke \
  --function-name xfusion-lambda-cli \
  response.json

Check the response:

✅ Expected output: { "statusCode": 200, "body": "Welcome to KKE AWS Labs!" }


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

More from this blog

W

Whispering Cloud Insights

88 posts

Documenting my path to cloud journey. Sharing lessons, tutorials, and insights from my continuous learning journey. Learn with me.