Skip to content

Commit 85e98bc

Browse files
committed
new blog post and update changelog
1 parent 7851a3f commit 85e98bc

9 files changed

+277
-0
lines changed

content/pages/05-deployment/04-cdns.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ servers to improve web app loading speed.
1616
is a fantastic post that goes into great technical detail on how CDNs and
1717
caching work.
1818

19+
* [The 5 hour CDN](https://fly.io/blog/the-5-hour-content-delivery-network/)
20+
explains the basics of what CDNs are and how they are a combination of
21+
many standard web server components, but used globally and at scale.
22+
1923
* [MaxCDN vs CloudFlare vs Amazon CloudFront vs Akamai Edge vs Fastly](https://www.codeinwp.com/blog/maxcdn-vs-cloudflare-vs-cloudfront-vs-akamai-edge-vs-fastly/)
2024
compares and contrasts the most popular CDN services based on features,
2125
performance and pricing. Note

content/pages/meta/00-change-log.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ view commit-level changes via the
1414
on GitHub.
1515

1616
## 2021
17+
### August
18+
* New blog post on
19+
[Application Performance Monitoring AWS Lambda Functions with Sentry](/blog/application-performance-monitoring-aws-lambda-functions-sentry.html).
20+
1721
### June
1822
* Fix broken links across the site. It's a real shame people and companies don't use
1923
301 redirects more often rather than just breaking their URL structures.
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
title: Application Performance Monitoring AWS Lambda Functions with Sentry
2+
slug: application-performance-monitoring-aws-lambda-functions-sentry
3+
meta: Learn how to use Sentry Application Performance Monitoring on AWS Lambda.
4+
category: post
5+
date: 2021-08-23
6+
modified: 2021-08-25
7+
newsletter: False
8+
headerimage: /img/headers/python-lambda-sentry.jpg
9+
headeralt: The Python, AWS Lambda and Sentry logos are copyright their respective owners.
10+
11+
12+
[Amazon Web Services (AWS) Lambda](/aws-lambda.html) is a usage-based
13+
computing infrastructure service that can execute
14+
[Python 3](/why-use-python.html) code. One of the challenges of this
15+
environment is ensuring efficient performance of your Lambda Functions.
16+
Application performance monitoring (APM) is particularly useful in these
17+
situations because you are billed based on how long you use the
18+
resources.
19+
20+
In this post we will install and configure
21+
[Sentry's APM](https://sentry.io/for/performance/) that works via a
22+
[Lambda layer](https://docs.aws.amazon.com/lambda/latest/dg/invocation-layers.html).
23+
Note that if you are looking for error monitoring rather than performance
24+
monitoring, take a look at
25+
[How to Monitor Python Functions on AWS Lambda with Sentry](/blog/monitor-python-functions-aws-lambda-sentry.html)
26+
rather than following this post.
27+
28+
29+
## First steps with AWS Lambda
30+
A local [development environment](/development-environments.html) is not
31+
required to follow this tutorial because all of the coding and configuration
32+
can happen in a web browser through the
33+
[AWS Console](https://console.aws.amazon.com/console/).
34+
35+
[Sign into your existing AWS account](https://aws.amazon.com/console)
36+
or sign up for a [new account](https://aws.amazon.com/). Lambda
37+
gives you the first 1 million requests for free so that you can execute
38+
basic applications without no or low cost.
39+
40+
<img src="/img/210406-python-sentry-aws-lambda/aws-lambda-landing.jpg" width="100%" class="shot rnd outl" alt="The AWS Lambda landing page.">
41+
42+
When you log into your account, use the search box to enter
43+
"lambda" and select "Lambda" when it appears to get to the right
44+
page.
45+
46+
<img src="/img/210406-python-sentry-aws-lambda/lambda-search-bar.png" width="100%" class="shot rnd outl" alt="Use the search bar to find AWS Lambda.">
47+
48+
If you have already used Lambda before, you will see your existing Lambda
49+
functions in a searchable table. We're going to create a new function so
50+
click the "Create function" button.
51+
52+
<img src="/img/210406-python-sentry-aws-lambda/create-function.png" width="100%" class="shot rnd outl" alt="Click the create function button.">
53+
54+
The create function page will give you several options for building a
55+
Lambda function.
56+
57+
<img src="/img/210406-python-sentry-aws-lambda/create-function-detail.png" width="100%" class="shot rnd outl" alt="The create function details page.">
58+
59+
Click the "Browse Serverless App Repository" selection box, then choose
60+
the "hello-world-python3" starter app from within the
61+
"Public applications" section.
62+
63+
<img src="/img/210406-python-sentry-aws-lambda/create-function-detail.png" width="100%" class="shot rnd outl" alt="The create function details page.">
64+
65+
The hello-world-python3 starter app details page should look something
66+
like the following screen:
67+
68+
<img src="/img/210406-python-sentry-aws-lambda/hello-world-python3.png" width="100%" class="shot rnd outl" alt="Hello world Python3 example app and Lambda function.">
69+
70+
Fill in some example text such as "test" under `IdentityNameParameter`
71+
and click the "Deploy" button:
72+
73+
<img src="/img/210406-python-sentry-aws-lambda/deploy-starter-app.png" width="100%" class="shot rnd outl" alt="Click the deploy button to use the starter app.">
74+
75+
The function will now be deployed. As soon as it is ready we can
76+
customize it and test it out before adding Sentry to capture any errors
77+
that occur during execution.
78+
79+
Go back to the Lambda functions main page and select your new deployed
80+
starter app from the list.
81+
82+
<img src="/img/210406-python-sentry-aws-lambda/functions-list.jpg" width="100%" class="shot rnd outl" alt="List of AWS Lambda functions you have created.">
83+
84+
Find the orange "Test" button with a down arrow next to it like you
85+
see in the image below, and then click the down arrow. Select
86+
"Configure Test Event".
87+
88+
<img src="/img/210406-python-sentry-aws-lambda/configure-test.jpg" width="100%" class="shot rnd outl" alt="Configure the test event.">
89+
90+
Fill in the Event name as "FirstTest" or something similar, then
91+
press the "Create" button at the bottom of the modal window.
92+
93+
Click the "Test" button and it will run the Lambda function with
94+
the parameters from that new test event. You should see something
95+
like the following output:
96+
97+
```python
98+
Response
99+
"value1"
100+
101+
Function Logs
102+
START RequestId: 62fa2f25-669c-47b7-b4e7-47353b0bd914 Version: $LATEST
103+
value1 = value1
104+
value2 = value2
105+
value3 = value3
106+
END RequestId: 62fa2f25-669c-47b7-b4e7-47353b0bd914
107+
REPORT RequestId: 62fa2f25-669c-47b7-b4e7-47353b0bd914 Duration: 0.30 ms Billed Duration: 1 ms Memory Size: 128 MB Max Memory Used: 43 MB Init Duration: 1.34 ms
108+
109+
Request ID
110+
62fa2f25-669c-47b7-b4e7-47353b0bd914
111+
```
112+
113+
The code was successfully executed, so let's add Sentry's performance
114+
monitoring and test some code that uses it.
115+
116+
117+
## Performance monitoring with Sentry
118+
Go to [Sentry.io's homepage](https://sentry.io).
119+
120+
<img src="/img/210406-python-sentry-aws-lambda/sentry-homepage.jpg" width="100%" class="shot rnd outl" alt="Sentry.io homepage where you can sign up for a free account.">
121+
122+
Sign into your account or sign up for a new free account. You will be at
123+
the main account dashboard after logging in or completing the Sentry sign
124+
up process.
125+
126+
Select "Performance" on the left navigation bar, it will take you to the
127+
performance monitoring page.
128+
129+
<img src="/img/210823-sentry-apm-lambda/performance.jpg" width="100%" class="shot rnd outl" alt="Click the 'performance' button on the left side nav.">
130+
131+
Click "Start Setup" then go back over to AWS Lambda to complete the
132+
steps for adding Sentry's Python layer to your Lambda function.
133+
134+
The easiest way to add Sentry to Lambda for this application
135+
is to configure an
136+
[AWS Lambda Layer](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)
137+
with the necessary dependency for Sentry. Sentry has concise
138+
[documentation on adding via Lambda Layers](https://docs.sentry.io/platforms/python/guides/aws-lambda/layer/)
139+
so we will walk through that way to configure it and test it
140+
out.
141+
142+
Scroll down to the "Layers" section while in your Lambda
143+
function configuration. Click the "Add a layer" button":
144+
145+
<img src="/img/210406-python-sentry-aws-lambda/add-lambda-layer.png" width="100%" class="shot rnd outl" alt="Add Lambda layer.">
146+
147+
In the "Add layer" screen, select the "Specify an ARN" option.
148+
149+
<img src="/img/210406-python-sentry-aws-lambda/add-layer-specify-arn.jpg" width="100%" class="shot rnd outl" alt="Select Specify ARN in the Add Layer screen.">
150+
151+
Now to specify the Amazon Resource Name (ARN), we need to use
152+
the Sentry documentation to get the right configuration string.
153+
154+
US-East-1 is the oldest and most commonly-used region so I'll
155+
use that here in this tutorial but you should check which one
156+
you are in if you are not certain.
157+
158+
<img src="/img/210823-sentry-apm-lambda/arn-region.png" width="100%" class="shot rnd outl" alt="Select the AWS for the ARN string.">
159+
160+
Copy that value into the Lambda Layer configuration, like this:
161+
162+
<img src="/img/210823-sentry-apm-lambda/layer-with-arn.png" width="100%" class="shot rnd outl" alt="Select the AWS for the ARN string.">
163+
164+
Then press the "Add" button. You now have the Sentry dependency
165+
in your environment so code that relies upon that library can be
166+
used in the Lambda function.
167+
168+
169+
## Testing performance monitoring
170+
Let's change our Python code in the Lambda function and test out
171+
the APM agent.
172+
173+
Make sure you are signed into your Sentry account and go to
174+
[this specific AWS Lambda set up guide](https://docs.sentry.io/platforms/python/guides/aws-lambda/).
175+
176+
You will see a "DSN string" that we need to set as an environment
177+
variable on AWS Lambda to finish our setup. Copy the string that
178+
matches your project as shown on that page in the highlighted green
179+
section:
180+
181+
<img src="/img/210823-sentry-apm-lambda//sentry-dsn-string.png" width="100%" class="shot rnd outl" alt="Copy the Sentry DSN string so we can export it as an environment variable.">
182+
183+
We will
184+
[use environment variables on AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html)
185+
to store and access values like this Sentry DSN key.
186+
187+
Go into the Lambda console to create a new environment variable. To do
188+
that, click the "Configuration" tab within Lambda like you see here:
189+
190+
<img src="/img/210406-python-sentry-aws-lambda/aws-lambda-configuration.jpg" width="100%" class="shot rnd outl" alt="Click the Lambda Configuration tab.">
191+
192+
Then click "Edit" and add a new environment variable with the key of `SENTRY_DSN`
193+
and the value of the DSN string that you copied from the Sentry screen.
194+
195+
<img src="/img/210406-python-sentry-aws-lambda/add-env-var.jpg" width="100%" class="shot rnd outl" alt="Add the environment variable in AWS Lambda.">
196+
197+
Click the "Save" button and go back to your Lambda function code.
198+
199+
Update your Lambda function with the following highlighted new lines of code
200+
to send errors to Sentry.
201+
202+
```python
203+
import json
204+
import os
205+
import sentry_sdk
206+
import time
207+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
208+
from sentry_sdk import start_transaction
209+
210+
SENTRY_DSN = os.environ.get('SENTRY_DSN')
211+
sentry_sdk.init(
212+
dsn=SENTRY_DSN,
213+
traces_sample_rate=1.0,
214+
integrations=[AwsLambdaIntegration()]
215+
)
216+
217+
print('Loading function')
218+
219+
220+
def lambda_handler(event, context):
221+
calc = 1000
222+
223+
# this is custom instrumentation, see docs: https://bit.ly/2WjT3AY
224+
with start_transaction(op="task", name="big calculation"):
225+
for i in range(1, 1000):
226+
calc = calc * i
227+
228+
print(calc)
229+
return event['key1'] # Echo back the first key value
230+
```
231+
232+
The above code imports the Sentry dependencies, and then runs both
233+
[automatic instrumentation](https://docs.sentry.io/platforms/python/guides/aws-lambda/performance/instrumentation/automatic-instrumentation/)
234+
and [custom instrumentation](https://bit.ly/2WjT3AY) on the
235+
code. Click the "Deploy" button and then "Test". The code will
236+
successfully execute and when we go back to our Sentry performance
237+
monitoring dashboard we will see some initial results, like this
238+
following screenshot.
239+
240+
<img src="/img/210823-sentry-apm-lambda/performance-results.jpg" width="100%" class="shot rnd outl" alt="APM results shown in the Sentry dashboard.">
241+
242+
Looks good, you have both the default and the specified transaction
243+
performance recordings in the dashboard, and you can toggle between
244+
them (or other transactions you record) through the user interface.
245+
246+
247+
## What's Next?
248+
We just wrote and executed a Python 3 function on AWS Lambda that
249+
used the basics of Sentry APM to get some initial performance
250+
monitoring data.
251+
252+
Check out the [AWS Lambda section](/aws-lambda.html) for
253+
more tutorials by other developers.
254+
255+
Further questions? Contact me on Twitter
256+
[@fullstackpython](https://twitter.com/fullstackpython)
257+
or [@mattmakai](https://twitter.com/mattmakai). I am also on GitHub with
258+
the username [mattmakai](https://github.com/mattmakai).
259+
260+
Something wrong with this post? Fork
261+
[this page's source on GitHub](https://github.com/mattmakai/fullstackpython.com/blob/master/content/posts/210823-performance-monitoring-aws-lambda-sentry.markdown)
262+
and submit a pull request.
Loading
Loading
Loading
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
<a href="/learning-programming.html" class="lgi">Learning Programming</a>
3+
<a href="/web-development.html" class="lgi">Web development</a>
4+
<a href="/aws-lambda.html" class="lgi">AWS Lambda</a>
5+
<a href="/sentry.html" class="lgi">Sentry</a>
6+
<a href="https://sentry.io" class="lgi">Sentry homepage {% include "blog/external-link.html" %}</a>
7+
<a href="https://docs.sentry.io/error-reporting/quickstart/?platform=python" class="lgi">Sentry Python Quickstart docs {% include "blog/external-link.html" %}</a>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy