DEV Community

Cover image for Day 34: Create a Lambda Function Using CLI
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

Day 34: Create a Lambda Function Using CLI

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
Enter fullscreen mode Exit fullscreen mode

Verify the file:

cat lambda_function.py
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 2: Zip the Python script

zip function.zip lambda_function.py

# Verify:

ls -l function.zip
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)