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
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
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
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
Use the log stream name to list all its events. Be sure to replace
<LOG_STREAM_NAME
with thelogStreamName
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.