Skip to content

Commit 1f2c751

Browse files
committed
work on next post. close
1 parent 1037eaf commit 1f2c751

14 files changed

+91
-98
lines changed

content/posts/170429-python-3-6-aws-lambda.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ slug: aws-lambda-python-3-6
33
meta: Code, create and execute your first Amazon Web Services (AWS) Lambda function with Python 3.6.
44
category: post
55
date: 2017-04-29
6-
modified: 2017-04-30
6+
modified: 2018-04-25
77
newsletter: False
88
headerimage: /img/170429-aws-lambda-python-3-6/header.jpg
99
headeralt: AWS, AWS Lambda and Python logos are copyright their respective owners.
@@ -122,7 +122,7 @@ command line.
122122
The Python code expects two environment variables that are read by the
123123
`os` module with the `environ.get` function. With the `what_to_print` and
124124
`how_many_times` variables set by the environment variables, our code then
125-
then prints a message zero or more times, based on the amount defined in
125+
prints a message zero or more times, based on the amount defined in
126126
the `how_many_times` variable. If a message is printed at least once then
127127
the function returns the `what_to_print` string, if nothing is printed
128128
then `None` is returned.

content/posts/170723-monitor-flask-apps.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ errors that occur for our users.
217217
Head to [Rollbar's homepage](https://rollbar.com/) so we can add their
218218
hosted monitoring tools to our oft-erroring Flask app.
219219

220-
<img src="/img/170723-monitor-flask-apps/rollbar-homepage.png" width="100%" class="technical-diagram img-rounded" style="border:1px solid #ccc" alt="Rollbar homepage in the web browser.">
220+
<img src="/img/170723-monitor-flask-apps/rollbar-homepage.jpg" width="100%" class="technical-diagram img-rounded" style="border:1px solid #ccc" alt="Rollbar homepage in the web browser.">
221221

222222
Click the "Sign Up" button in the upper right-hand corner. Enter your
223223
email address, a username and the password you want on the sign up page.

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

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ slug: monitor-python-3-6-example-code-aws-lambda-rollbar
33
meta: Monitor your Python 3.6 application code on Amazon Web Services (AWS) Lambda functions using Rollbar.
44
category: post
55
date: 2018-04-20
6-
modified: 2018-04-20
6+
modified: 2018-04-25
77
newsletter: False
88
headerimage: /img/180420-monitor-aws-lambda/header.jpg
99
headeralt: Python, AWS Lambda and Rollbar logos are copyright their respective owners.
@@ -47,145 +47,138 @@ starting page.
4747

4848
<img src="/img/180420-monitor-aws-lambda/search-for-lambda.jpg" width="100%" class="shot rnd outl" alt="Search for lambda in the dashboard text box.">
4949

50-
Click "Create a Lambda function" and the "Select Blueprint" page should
51-
appear.
50+
Click the "Create function" button.
5251

53-
<img src="/img/180420-monitor-aws-lambda/select-blueprint.jpg" width="100%" class="shot rnd outl" alt="The Select Blueprint Lambda screen.">
52+
<img src="/img/180420-monitor-aws-lambda/create-function.png" width="100%" class="shot rnd outl" alt="The create Lambda function screen.">
5453

55-
Select "Blank Function". The "Configure triggers" page should appear next.
56-
A trigger is how the Lambda function typically knows
57-
when to execute based on an event from another AWS service like
58-
[Cloudwatch](https://aws.amazon.com/cloudwatch/) or
59-
[API Gateway](https://aws.amazon.com/api-gateway/).
54+
Select "Author from Scratch". Fill in a name so you can easily recognize this
55+
function for future reference. I chose "monitorPython3". Select "Python 3.6"
56+
for Runtime.
6057

61-
<img src="/img/180420-monitor-aws-lambda/configure-triggers.jpg" width="100%" class="shot rnd outl" alt="Screen for configuring the AWS Lambda trigger.">
58+
Select "Create new role from template(s)", input a Role name, for example
59+
"basicEdgeLambdaRole". For Policy templates choose "Basic Edge Lambda
60+
Permissions".
6261

63-
You do not need to configure a trigger to move to the next screen so
64-
we will not configure a trigger for this function. We can manually
65-
kick off the Lambda to test it when we are done with configuring it. Leave
66-
the trigger icon blank and click "Next" to continue.
62+
Then click "Create function."
6763

68-
<img src="/img/180420-monitor-aws-lambda/blank-lambda.jpg" width="100%" class="shot rnd outl" alt="Blank AWS Lambda function.">
64+
<img src="/img/180420-monitor-aws-lambda/monitorpython3.png" width="100%" class="shot rnd outl" alt="Blank AWS Lambda function named monitorPython3.">
6965

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
7167
our code.
7268

7369

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

81-
<img src="/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
74+
text box.
9075

9176

9277
```python
9378
import os
79+
import rollbar
80+
9481

82+
ROLLBAR_KEY = os.getenv('ROLLBAR_SECRET_KEY', 'missing Rollbar secret key')
83+
rollbar.init(ROLLBAR_KEY, 'production')
9584

85+
86+
@rollbar.lambda_function
9687
def lambda_handler(event, context):
97-
what_to_print = os.environ.get("what_to_print")
98-
how_many_times = int(os.environ.get("how_many_times"))
88+
message = os.getenv("message")
89+
print_count = int(os.getenv("print_count"))
9990

100-
# make sure what_to_print and how_many_times values exist
101-
if what_to_print and how_many_times > 0:
102-
for i in range(0, how_many_times):
91+
# check if message exists and how many times to print it
92+
if message and print_count > 0:
93+
for i in range(0, print_count):
10394
# formatted string literals are new in Python 3.6
104-
print(f"what_to_print: {what_to_print}.")
105-
return what_to_print
95+
print(f"message: {message}.")
96+
return print_count
10697
return None
10798
```
10899

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

116103
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.
123106

124107
Below the code input text box on this function configuration screen there
125108
is a section to set environment variable key-value pairs.
126109

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+
<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.">
133111

134-
<img src="/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.
135114

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

123+
(execute, show with errors)
140124

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

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

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
154-
temporary storage for Lambda functions.
130+
<img src="/img/180420-monitor-aws-lambda/rollbar-home.jpg" width="100%" class="shot rnd outl" alt="Rollbar homepage.">
155131

156-
<img src="/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.
157134

158-
Our code and configuration is in place so click the "Next" button
159-
at the bottom right corner of the page.
135+
<img src="/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.">
160136

161-
<img src="/img/170429-aws-lambda-python-3-6/review-lambda.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Review Lambda configuration.">
137+
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.
162141

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

167-
<img src="/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.
168146

169-
Success message should appear on the next page below the "Test" button.
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.">
170148

171-
<img src="/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:
172152

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

179-
<img src="/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.">
154+
```python
155+
import os
156+
~~import rollbar
157+
~~
158+
~~
159+
~~ROLLBAR_KEY = os.getenv('ROLLBAR_SECRET_KEY', 'missing Rollbar secret key')
160+
~~rollbar.init(ROLLBAR_KEY, 'production')
161+
162+
163+
~~@rollbar.lambda_function
164+
def lambda_handler(event, context):
165+
message = os.getenv("message")
166+
print_count = int(os.getenv("print_count"))
167+
168+
# check if message exists and how many times to print it
169+
if message and print_count > 0:
170+
for i in range(0, print_count):
171+
# formatted string literals are new in Python 3.6
172+
print(f"message: {message}.")
173+
return print_count
174+
return None
175+
```
180176

181-
Scroll down to the "Execution result" section where we can see our output.
177+
(explain new lines of code)
182178

183-
<img src="/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)
184180

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.
181+
(re-run code)
189182

190183

191184
## What's Next?
106 KB
Loading
-239 KB
Binary file not shown.
-47.9 KB
Binary file not shown.
143 KB
Loading
64.1 KB
Loading
125 KB
Loading
137 KB
Loading

0 commit comments

Comments
 (0)