Testing and Debugging Lambda Functions

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

When working with serverless you only need to worry about your logic i.e. your code and how well it performs, but with no system to log into or troubleshoot how does one triage issue?
AWS provides managed monitoring/watchdog service called AWS CloudWatch among many other offerings such as AWS X-Ray, AWS CloudTrail to monitor and analyze targeted metrics pertaining to your Lambda functions which give you exact and accurate insights into how your business logic is performing. CloudWatch logs both metrics and actual code execution logs to help you troubleshoot and test your application.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Invoke a Lambda Function via the CLI
  1. Invoke the Lambda function provided with the lab.

    aws lambda invoke --function-name DebuggingTestFunction --payload '{"String":"racecar" }' log.txt

    The Lambda function for the lab detects palindromes (strings that, when reversed, are the same — such as "racecar" or "taco cat").

Describe Log Groups within CloudWatch Logs and Select the One for Your Lambda Function
  1. Look for the CloudWatch log group with your Lambda function name in it:

    aws logs describe-log-groups
Describe the Log Stream Name within the CloudWatch Log Group
  1. The command will list the log stream names in chronological order:

    aws logs describe-log-streams --log-group-name "/aws/lambda/DebuggingTestFunction" --order-by LastEventTime
Use the Log Stream Name from the Previous Command to List All Events in It
  1. Use the log stream name to list all its events. Be sure to replace <LOG_STREAM_NAME with the logStreamName returned in the output of the previous command and escape the $ symbol (by entering “ before it) in the log stream name:

    aws logs get-log-events --log-group-name "/aws/lambda/DebuggingTestFunction" --log-stream-name "<LOG_STREAM_NAME>" | jq '.events[].timestamp |= ( ./ 1000 | strftime("%Y-%m-%d %T%p"))'

Note: The initial AWS API command’s output is piped to jq — a JSON parsing utility that converts the timestamp into a human-readable format in UTC from linux epoch time. This part of the command is not compulsory, but we’ve added it to make reading time easier.

(Optional) Invoke Function from AWS Console (GUI) and Check Logs via CLI

You can additionally invoke the Lambda function with different payload/test events from the CLI and AWS console (on the console, you pass input to function using "Test events on top right corner once you’re in function details page."). This is shown in the solution video.

Additional Resources

In this hands on lab, we'll be invoking and debugging an AWS Lambda function written in Python using the AWS CLI. We'll be tapping into the logs provided with the CloudWatch Logs service and see how we can leverage useful flags and Linux command line utilities to make debugging on the command line an easy task.

When confirming and checking resources created through AWS CLI on the AWS console, ensure you're in region us-east-1 (N. Virginia).

To begin, open a terminal session and log in to the provided EC2 instance via SSH using the credentials provided on the lab page:

ssh cloud_user@<PUBLIC IP>

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!