Skip to content

Commit 31dfe66

Browse files
committed
add latest post. update ToC with new turbogears page
1 parent 08167b9 commit 31dfe66

File tree

9 files changed

+78
-19
lines changed

9 files changed

+78
-19
lines changed

content/posts/180420-monitor-aws-lambda-python-3-6.markdown

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,49 @@ The Python code expects two environment variables that are read by the
104104
`os` module with the `getenv` function. The `message` and
105105
`print_count` variables are set by the environment variables.
106106

107+
<img src="/img/180420-monitor-aws-lambda/lambda-coded.png" width="100%" class="shot rnd outl" alt="Python 3.6 code within a Lambda function.">
108+
107109
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.
109112

110-
<img src="/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`.
111115

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+
<img src="/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+
<img src="/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.
114137

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+
<img src="/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.">
122139

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.
124144

125145

126146
## Monitoring our Lambda Function
127147
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.
129150

130151
<img src="/img/180420-monitor-aws-lambda/rollbar-home.jpg" width="100%" class="shot rnd outl" alt="Rollbar homepage.">
131152

@@ -144,9 +165,12 @@ a Python-based application.
144165
Press "Continue" at the bottom of the screen. The next
145166
page shows us a few instructions on how to add monitoring.
146167

147-
<img src="/img/180202-monitor-django/configure-project.jpg" width="100%" class="shot rnd outl" alt="Configure project using your server-side access token.">
168+
<img src="/img/180420-monitor-aws-lambda/configure-project.jpg" width="100%" class="shot rnd outl" alt="Configure project using your server-side access token.">
148169

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
150174
the errors that occur in our application. Add the following highlighted
151175
lines to your Lambda code:
152176

@@ -174,11 +198,46 @@ def lambda_handler(event, context):
174198
return None
175199
```
176200

177-
(explain new lines of code)
201+
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+
<img src="/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+
<img src="/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.
178236

179-
(add ROLLBAR env var)
237+
<img src="/img/180420-monitor-aws-lambda/rollbar-exceptions.png" width="100%" class="shot rnd outl" alt="Rollbar user interface with exceptions.">
180238

181-
(re-run code)
239+
Now we can track Lambda exceptions across many functions regardless
240+
of how frequently they are running.
182241

183242

184243
## What's Next?
110 KB
Loading
63.6 KB
Loading
170 KB
Loading
58.7 KB
Loading
39.8 KB
Loading
162 KB
Loading
30.2 KB
Loading

theme/templates/table-of-contents.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ <h4>4.1 <a href="/web-frameworks.html">Web Frameworks</a></h4>
9494
<div class="tc"><a href="/flask.html">Flask</a></div>
9595
<div class="tc"><a href="/bottle.html">Bottle</a></div>
9696
<div class="tc"><a href="/pyramid.html">Pyramid</a></div>
97+
<div class="tc"><a href="/turbogears.html">TurboGears</a></div>
9798
<div class="tc"><a href="/falcon.html">Falcon</a></div>
9899
<div class="tc"><a href="/morepath.html">Morepath</a></div>
99100
<div class="tc"><a href="/sanic.html">Sanic</a></div>
@@ -110,7 +111,6 @@ <h4>4.3 <a href="/web-design.html">Web design</a></h4>
110111
<h4>4.4 <a href="/css-frameworks.html">CSS Frameworks</a></h4>
111112
<div class="tc"><a href="/bootstrap-css.html">Bootstrap</a></div>
112113
<div class="tc"><a href="/foundation-css.html">Foundation</a></div>
113-
<div class="tc sn">Materialize</div>
114114
<div class="tc sn">Concise</div>
115115
<h4>4.5 <a href="/javascript.html">JavaScript</a></h4>
116116
<div class="tc sn">React</div>

0 commit comments

Comments
 (0)