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 in to or troubleshoot, how does one triage issues? AWS provides a managed monitoring/watchdog service called AWS CloudWatch, among many other offerings (e.g, AWS X-Ray, AWS CloudTrail) to monitor and analyze targeted metrics pertaining to your Lambda functions. These servuces provide 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

After connecting via ssh, run aws configure. Hit the return/enter key on your keyboard to accept the defaults for the AWS Access Key ID and the AWS Secret Access Key. Enter us-east-1 for the Default region name. Accept the Default output format.

Invoke the Lambda function provided with the lab.

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

Note: 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

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

This 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

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 the Function from the AWS Console and Check Logs via the CLI

You can additionally invoke the Lambda function with different payload/test events from the CLI and AWS Management Console.

  1. In the AWS console, navigate to Lambda > Functions.
  2. Click the listed Lambda function.
  3. At the top right, click Select a test event > Configure test events.

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 an easy task.

When confirming and checking resources created through the 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.

Sign In
Welcome Back!

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

Get Started
Who’s going to be learning?