Skip to main content

Testing and Debugging Lambda Functions

Hands-On Lab

 

Photo of Moosa Khalid

Moosa Khalid

AWS Training Architect II

Length

00:45:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Testing and Debugging Lambda Functions

Introduction

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.

Solution

Log in to the live AWS environment using the credentials provided. Make sure you're in the N. Virginia (us-east-1) region throughout the lab.

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>

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

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

  1. In the AWS console, navigate to Lambda > Functions.

  2. Click our listed function.

  3. Click the Select a test event box, and select Configure test events in the dropdown.

  4. Give it an Event name of "mytestevent".

  5. Delete the key2 and key3 lines.

  6. Change the key1 line, passing in a value of your name:

    "String": "<YOUR_NAME>"
  7. Click Create.

  8. Click Test.

  9. Back in the CLI, describe the log streams again, using the same command as before:

    aws logs describe-log-streams --log-group-name "/aws/lambda/DebuggingTestFunction" --order-by LastEventTime
  10. Copy the newest logStreamName and list its events using the same command as before (and don't forget to replace <LOG_STREAM_NAME> with the new log stream name and escape the $):

    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"))'
  11. Back in the console, navigate to CloudWatch > Logs.

  12. Click the newest log stream name. We should see the same results as those we received in the CLI.

Conclusion

Congratulations on successfully completing this hands-on lab!