Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit 52c2341

Browse files
Tim-Petroneerichendrickson
authored and
erichendrickson
committed
logs-api-gateway (#23)
* updated lambda-api-gateway file to include a cloudwatch logs section * updated CloudWatch section in lambda-api-gateway.md per review
1 parent f9c83bd commit 52c2341

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed
Loading
Loading
Loading
Loading
Loading

aws/lambda-and-api-gateway/lambda-api-gateway/lambda-api-gateway.md

+40
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,46 @@ callback(null, response);
105105

106106
Depending on whether or not this is a successful case, you can put in other status codes. [Refer to this page](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) to find a status code that is appropriate for your needs.
107107

108+
## CloudWatch Logs
109+
110+
As you get further into development, there may be times where you can no longer check the Lambda console or the API gateway testing environment to view your results. This might be because you are calling a backend function through the UI to see if it is working as intended with the added layer of complexity. In these situations, you will need a way to check the Lambda function's console logs, or more importantly, their error messages.
111+
112+
CloudWatch is another AWS resource that we can utilize in scenarios like this one. CloudWatch keeps a record of all the events from start to finish that a Lambda function goes through when it is called. Here you can see any console logs or errors thrown by the function to determine the status of the function you are trying to test.
113+
114+
In the example above, we created a simple "Hello, World!" Lambda function and hooked it up to API Gateway. Now let us locate the phrase "Hello from Lambda" in the CloudWatch logs.
115+
116+
If you navigate to the CloudWatch console in AWS, you will see a menu on the left hand side. If you click on the "Logs" section, you will see a list of log groups which are essentially all of the Lambda functions that have had activity in whichever AWS region you are currently logged into. Locate the log group for the helloWorld Lambda function in that list.
117+
118+
![alt text](images/18.png)
119+
120+
After clicking on the helloWorld log group, you will see a list of log streams. Each one of these streams is one iteration of the helloWorld function. Each stream logs all events for all of the times the function has been called. A new stream is created every time the Lambda function gets updated. Below you can see this function is on its fourth iteration. The iterations are logged from most recent to least recent by default.
121+
122+
![alt text](images/19.png)
123+
124+
So far, if you were to test your function through API gateway, the Lambda console, or Postman, you would only see one stream. That stream would be almost empty. If you clicked on the stream you would see that it only contains START, END, and REPORT logs.
125+
126+
![alt text](images/20.png)
127+
128+
That is because we do not have any `console.log`s in our code just yet. Right now, the function is running and resolving but not showing any output in our logs. In the Lambda function, we can add a line of code above the `callback` function. Let's make it simple for now:
129+
130+
```javascript
131+
console.log(response);
132+
```
133+
134+
If we now test the function and check our logs, we will see a second log stream that gives us more information inside. Here we can see the entire object that is getting called back. It contains the status code and the body containing the data "Hello from Lambda."
135+
136+
![alt text](images/21.png)
137+
138+
Now, let's try to get the words "Hello from Lambda" by themselves. To do this, we will have to drill down into the object and extract the data from the body. If your first thought was to change the `console.log` to:
139+
140+
```javascript
141+
console.log(response.body.data);
142+
```
143+
144+
you would be close, but incorrect. This would return an `undefined` value. This is because the data key was stringified using `JSON.stringify()` above. This is the cause of undefined responses fairly often while fixing bugs, so be aware of where you are stringifying data and where you are parsing data. We could `JSON.parse()` the data key to fix this error, but lets just take the `JSON.stringify()` out for now as to not over complicate things. Now when you test your function again, you will end up with a log that is just the words "Hello from Lambda" in your CloudWatch logs.
145+
146+
![alt text](images/22.png)
147+
108148
## Conclusion
109149

110150
Hopefully by now you have some grasp at how to use API Gateway to trigger Lambda functions. Lambda functions can be used to read from and write to databases, send text messages or email, and many, many other things. The following tutorials will mostly focus on Lambda functions with the assumption that you can later on trigger these functions with API Gateway.

0 commit comments

Comments
 (0)