You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/180420-monitor-aws-lambda-python-3-6.markdown
+77-18Lines changed: 77 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,28 +104,49 @@ The Python code expects two environment variables that are read by the
104
104
`os` module with the `getenv` function. The `message` and
105
105
`print_count` variables are set by the environment variables.
106
106
107
+
<imgsrc="/img/180420-monitor-aws-lambda/lambda-coded.png"width="100%"class="shot rnd outl"alt="Python 3.6 code within a Lambda function.">
108
+
107
109
Below the code input text box on this function configuration screen there
108
-
is a section to set environment variable key-value pairs.
110
+
is a section to set environment variable key-value pairs. We need to input
111
+
two environment variables and then we can run our code.
109
112
110
-
<imgsrc="/img/180420-monitor-aws-lambda/lambda-coded.png"width="100%"class="shot rnd outl"alt="Python 3.6 code within a Lambda function.">
113
+
Enter the keys named `message` with a value of `Hello World!`. Then
114
+
enter `print_count` as a second key with the value of `5`.
111
115
112
-
Next we need to input two environment variables and then we can run
113
-
our code.
116
+
Our Python code's error handling is not robust. A value other than a
117
+
number in the `print_count` variable will cause the script to throw
118
+
an exception when it is executed due to the forced casting of `print_count`
119
+
via the `int()` function. We will use the exception that can occur during
120
+
this forced casting as a trivial example that shows what happens when
121
+
errors in our code happen during Lambda function execution.
122
+
123
+
Hit the "Save" button at the top right. Use the
124
+
default "Hello World" test template values and name it "testHelloWorld".
125
+
We do not need any of those values for our function.
126
+
127
+
<imgsrc="/img/180420-monitor-aws-lambda/configure-test-event.png"width="100%"class="shot rnd outl"alt="Configure an empty test event for your Lambda function.">
128
+
129
+
Click "Create" and your test template will be created. Now click
130
+
"Test" to run the function. You should see "Execution result: succeeded"
131
+
with the `message` variable printed five times.
132
+
133
+
<imgsrc="/img/180420-monitor-aws-lambda/success-execution.png"width="100%"class="shot rnd outl"alt="Execution succeeds when there is an integer value for the print_count variable.">
134
+
135
+
Now change the value of `print_count` to `i dunno`. Save the function
136
+
and click "Test" again. The function will fail.
114
137
115
-
Enter the keys named `message` and `print_count`, then enter their values.
116
-
Use a string message for `message`'s value and an integer whole number
117
-
above 0 for `print_count`. Our Python code's error handling is not very
118
-
robust so a value other than a number in the `print_count` variable will
119
-
cause the script to throw an error when it is executed due to the forced
120
-
casting of `print_count` via the `int()` function. That is how we will
121
-
test Rollbar's error monitoring for the Lambda function.
138
+
<imgsrc="/img/180420-monitor-aws-lambda/failed-execution-result.png"width="100%"class="shot rnd outl"alt="Execution fails when we do not have an integer value for print_count variable.">
122
139
123
-
(execute, show with errors)
140
+
It is obvious when we are working in the Console that an error just
141
+
occurred. However, in most cases an error will happen sporadically
142
+
which is why we need a monitoring system in place to catch and report
143
+
on those exceptions.
124
144
125
145
126
146
## Monitoring our Lambda Function
127
147
Head over to the [Rollbar homepage](https://rollbar.com/)
128
-
to add their monitoring serving into our Lambda application.
148
+
to obtain a free account and grab the necessary information to add their
149
+
hosted monitoring service into our Lambda application.
Press "Continue" at the bottom of the screen. The next
145
166
page shows us a few instructions on how to add monitoring.
146
167
147
-
<imgsrc="/img/180202-monitor-django/configure-project.jpg"width="100%"class="shot rnd outl"alt="Configure project using your server-side access token.">
168
+
<imgsrc="/img/180420-monitor-aws-lambda/configure-project.jpg"width="100%"class="shot rnd outl"alt="Configure project using your server-side access token.">
148
169
149
-
We can now update our AWS Lambda Pyton function to collect and aggregate
170
+
Take note of that server-side access token as we will need to set it
171
+
as an environment variable on AWS Lambda.
172
+
173
+
We can now update our Python function to collect and aggregate
150
174
the errors that occur in our application. Add the following highlighted
The above highlighted new code lines incorporate the `rollbar` library
202
+
into our application, set the `ROLLBAR_KEY` with our environment variable
203
+
and use the `rollbar.lambda_function` decorator to catch all errors in
204
+
our `lambda_handler` function.
205
+
206
+
Add the following third environment variable named `ROLLBAR_SECRET_KEY`
207
+
that is the server-side token from your new Rollbar project.
208
+
209
+
<imgsrc="/img/180420-monitor-aws-lambda/rollbar-key-env-var.png"width="100%"class="shot rnd outl"alt="Add your Rollbar server-side key into a Lambda environment variable.">
210
+
211
+
There is just one issue with this function on Lambda as it stands: there is
212
+
no way for Lambda to know about the Rollbar package code. The external Rollbar
213
+
dependency needs to be included. There are a couple of ways to handle the
214
+
issue:
215
+
216
+
1. Download
217
+
[this pre-made zip file](https://github.com/fullstackpython/blog-code-examples/raw/master/monitor-aws-lambda-python-3-6/hello-rollbar.zip)
218
+
from the GitHub repository which includes all of the Rollbar package
219
+
code and our code in the `lambda_function.py` file.
220
+
1. Re-create the above code on your local system and
221
+
[use pip to obtain the dependencies and create a zip file locally](https://haythamsalhi.wordpress.com/2017/10/04/creating-lambda-deployment-package-of-python/).
222
+
223
+
I provided the pre-made zip file to save time in this tutorial so try
224
+
that one now so we can see the final results. Under "Function code", change
225
+
the "Code entry type" from "Edit code inline" to "Upload a .ZIP file".
226
+
Hit the "Upload" button under "Function package".
227
+
228
+
<imgsrc="/img/180420-monitor-aws-lambda/upload-zip-package.png"width="100%"class="shot rnd outl"alt="Upload the ZIP file with Rollbar dependency.">
229
+
230
+
Hit the "Save" button at the top. With our new code we can now see if
231
+
Rollbar will capture and report the exceptions. Hit the "Save" button and
232
+
then "Test".
233
+
234
+
The function will fail as expected. If we move over to our Rollbar
235
+
dashboard and refresh the page, we see the exceptions.
178
236
179
-
(add ROLLBAR env var)
237
+
<imgsrc="/img/180420-monitor-aws-lambda/rollbar-exceptions.png"width="100%"class="shot rnd outl"alt="Rollbar user interface with exceptions.">
180
238
181
-
(re-run code)
239
+
Now we can track Lambda exceptions across many functions regardless
0 commit comments