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/170723-monitor-flask-apps.markdown
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,7 +217,7 @@ errors that occur for our users.
217
217
Head to [Rollbar's homepage](https://rollbar.com/) so we can add their
218
218
hosted monitoring tools to our oft-erroring Flask app.
219
219
220
-
<imgsrc="/img/170723-monitor-flask-apps/rollbar-homepage.png"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Rollbar homepage in the web browser.">
220
+
<imgsrc="/img/170723-monitor-flask-apps/rollbar-homepage.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Rollbar homepage in the web browser.">
221
221
222
222
Click the "Sign Up" button in the upper right-hand corner. Enter your
223
223
email address, a username and the password you want on the sign up page.
<imgsrc="/img/180420-monitor-aws-lambda/monitorpython3.png"width="100%"class="shot rnd outl"alt="Blank AWS Lambda function named monitorPython3.">
69
65
70
-
Ok, finally we arrive at the "Configure function" screen where we can write
66
+
Ok, finally we have arrived at the configuration screen where we can write
71
67
our code.
72
68
73
69
74
-
## Lambda Function Python Example Code
75
-
Enter a name for the Lambda function, such as "python_3_6_lambda_test",
76
-
as well as a description. A description is optional but it is useful
77
-
when you have a dozens or hundreds of different Lambda functions and
78
-
need to keep them straight. In the Runtime drop-down, select Python 3.6 for
79
-
the programming language.
70
+
## Coding a Python Function
71
+
Scroll down to the "Function code" user interface section.
80
72
81
-
<imgsrc="/img/170429-aws-lambda-python-3-6/python-3-6-lambda.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Enter a name, description and use Python 3.6 for the Lambda.">
82
-
83
-
Beneath the Runtime drop-down there is a large text box for code,
84
-
prepopulated with a `lambda_handler` function definition. The
85
-
"Code entry type" drop-down can also be changed to allow uploading a ZIP
86
-
file or inputing a file from an S3 bucket. For our simple first
87
-
Lambda function we will stick to the "Edit code inline" option. Copy or type
88
-
in the following code, replacing what is already in the text box. This
89
-
code is also available on [this open source GitHub repository](https://github.com/fullstackpython/blog-code-examples/blob/master/aws-lambda-python-3-6/).
73
+
Paste or type in the following code, replacing what is already in the
#make sure what_to_print and how_many_times values exist
101
-
ifwhat_to_printandhow_many_times>0:
102
-
for i inrange(0, how_many_times):
91
+
#check if message exists and how many times to print it
92
+
ifmessageandprint_count>0:
93
+
for i inrange(0, print_count):
103
94
# formatted string literals are new in Python 3.6
104
-
print(f"what_to_print: {what_to_print}.")
105
-
returnwhat_to_print
95
+
print(f"message: {message}.")
96
+
returnprint_count
106
97
returnNone
107
98
```
108
99
109
-
The code above contains a required `lambda_handler` function, which is
110
-
AWS Lambda's defined hook so it knows where to begin execution. Think of
111
-
`lambda_handler` as a `main` function, like the
112
-
`if __name__ == "__main__":` conditional line commonly used in Python files
113
-
to ensure a block of code is executed when a script is run from the
114
-
command line.
100
+
The code contains the required `lambda_handler` function. `lambda_handler`
101
+
is Lambda's hook for where to start execution the code.
115
102
116
103
The Python code expects two environment variables that are read by the
117
-
`os` module with the `environ.get` function. With the `what_to_print` and
118
-
`how_many_times` variables set by the environment variables, our code then
119
-
then prints a message zero or more times, based on the amount defined in
120
-
the `how_many_times` variable. If a message is printed at least once then
121
-
the function returns the `what_to_print` string, if nothing is printed
122
-
then `None` is returned.
104
+
`os` module with the `getenv` function. The `message` and
105
+
`print_count` variables are set by the environment variables.
123
106
124
107
Below the code input text box on this function configuration screen there
125
108
is a section to set environment variable key-value pairs.
126
109
127
-
Enter the keys named `what_to_print` and `how_many_times` then enter their
128
-
values. Use a string message for `what_to_print`'s value and an integer
129
-
whole number above 0 for `how_many_times`. Our Python code's error handling
130
-
is not very robust so a value other than a number in the `how_many_times`
131
-
variable will cause the script to throw an error when it is executed due
132
-
to the forced casting of `how_many_times` via the `int()` function.
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.">
133
111
134
-
<imgsrc="/img/170429-aws-lambda-python-3-6/environment-variables.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Section to set environment variables for the Lambda function.">
112
+
Next we need to input two environment variables and then we can run
113
+
our code.
135
114
136
-
The Python 3.6 code and the environment variables are now in place. We
137
-
just need to handle a few more AWS-specific settings before we can test the
138
-
Lambda function.
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.
139
122
123
+
(execute, show with errors)
140
124
141
-
## Executing our Lambda Function
142
-
Scroll past the environment variables to the
143
-
"Lambda function handler and role" section, which contains a few more
144
-
required function configuration items.
145
125
146
-
Keep the default handler set to `lambda_function.lambda_handler`. Select
147
-
"Create a new Role from template(s)" from the drop-down then for the
148
-
"Role name" field enter "dynamodb_access". Under "Policy templates"
149
-
select the "Simple Microservice permissions".
126
+
## Monitoring our Lambda Function
127
+
Head over to the [Rollbar homepage](https://rollbar.com/)
128
+
to add their monitoring serving into our Lambda application.
150
129
151
-
The "Simple Microservice permissions" allows our Lambda to access
152
-
[AWS DynamoDB](https://aws.amazon.com/dynamodb/). We will not use DynamoDB in
153
-
this tutorial but the service is commonly used either as permanent or
<imgsrc="/img/170429-aws-lambda-python-3-6/lambda-handler-and-role.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="For the final configuration, keep the default handler, create a new role from a template for Simple Microservice permissions and save it with a unique name.">
132
+
Click "Sign Up" in the upper right-hand corner. Enter your
133
+
email address, username and desired password.
157
134
158
-
Our code and configuration is in place so click the "Next" button
159
-
at the bottom right corner of the page.
135
+
<imgsrc="/img/180420-monitor-aws-lambda/sign-up-rollbar.jpg"width="100%"class="shot rnd outl"alt="Signing up for a Rollbar account in your browser.">
After the sign up page you will see the onboarding flow where you can
138
+
enter a project name and select a programming language. For the project
139
+
name type in "Full Stack Python" and then select that you are monitoring
140
+
a Python-based application.
162
141
163
-
The review screen shows us our configuration settings to make sure we
164
-
selected the appropriate values for our new Lambda function. Scroll down
165
-
press "Create function".
142
+
<imgsrc="/img/180420-monitor-aws-lambda/create-project.jpg"width="100%"class="shot rnd outl"alt="Name your project 'Full Stack Python' and select Python as your language.">
166
143
167
-
<imgsrc="/img/170429-aws-lambda-python-3-6/create-function.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Click the create function button to continue.">
144
+
Press "Continue" at the bottom of the screen. The next
145
+
page shows us a few instructions on how to add monitoring.
168
146
169
-
Success message should appear on the next page below the "Test" button.
147
+
<imgsrc="/img/180202-monitor-django/configure-project.jpg"width="100%"class="shot rnd outl"alt="Configure project using your server-side access token.">
170
148
171
-
<imgsrc="/img/170429-aws-lambda-python-3-6/test.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Test button on the execution screen.">
149
+
We can now update our AWS Lambda Pyton function to collect and aggregate
150
+
the errors that occur in our application. Add the following highlighted
151
+
lines to your Lambda code:
172
152
173
-
Click the "Test" button to execute the Lambda. Lambda will prompt us for
174
-
some data to simulate an event that would kick off our function. Select
175
-
the "Hello World" sample event template, which contains some keys but our
176
-
Lambda will not use that in its execution. Click the "Save and test" button
177
-
at the bottom of the modal.
178
153
179
-
<imgsrc="/img/170429-aws-lambda-python-3-6/sample-event-template.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Sample event template for Lambda execution.">
# check if message exists and how many times to print it
169
+
if message and print_count >0:
170
+
for i inrange(0, print_count):
171
+
# formatted string literals are new in Python 3.6
172
+
print(f"message: {message}.")
173
+
return print_count
174
+
returnNone
175
+
```
180
176
181
-
Scroll down to the "Execution result" section where we can see our output.
177
+
(explain new lines of code)
182
178
183
-
<imgsrc="/img/170429-aws-lambda-python-3-6/execution-results.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Results from executing our new Lambda function.">
179
+
(add ROLLBAR env var)
184
180
185
-
The log output shows us the return value of our function, which in this
186
-
execution was the string message from `what_to_print`. We can also see
187
-
our print function produced output five times as expected based on the
188
-
amount set in the `how_many_times` environment variable.
0 commit comments