Amazon Web Services (AWS) Lambda is a usage-based
computing infrastructure service that can execute
Python 3 code. One of the challenges of this
environment is ensuring efficient performance of your Lambda Functions.
Application performance monitoring (APM) is particularly useful in these
situations because you are billed based on how long you use the
In this post we will install and configure
Sentry’s APM that works via a
Note that if you are looking for error monitoring rather than performance
monitoring, take a look at
How to Monitor Python Functions on AWS Lambda with Sentry
rather than following this post.
First steps with AWS Lambda
A local development environment is not
required to follow this tutorial because all of the coding and configuration
can happen in a web browser through the
Sign into your existing AWS account
or sign up for a new account. Lambda
gives you the first 1 million requests for free so that you can execute
basic applications without no or low cost.
When you log into your account, use the search box to enter
“lambda” and select “Lambda” when it appears to get to the right
If you have already used Lambda before, you will see your existing Lambda
functions in a searchable table. We’re going to create a new function so
click the “Create function” button.
The create function page will give you several options for building a
Click the “Browse Serverless App Repository” selection box, then choose
the “hello-world-python3” starter app from within the
“Public applications” section.
The hello-world-python3 starter app details page should look something
like the following screen:
Fill in some example text such as “test” under IdentityNameParameter
and click the “Deploy” button:
The function will now be deployed. As soon as it is ready we can
customize it and test it out before adding Sentry to capture any errors
that occur during execution.
Go back to the Lambda functions main page and select your new deployed
starter app from the list.
Find the orange “Test” button with a down arrow next to it like you
see in the image below, and then click the down arrow. Select
“Configure Test Event”.
Fill in the Event name as “FirstTest” or something similar, then
press the “Create” button at the bottom of the modal window.
Click the “Test” button and it will run the Lambda function with
the parameters from that new test event. You should see something
like the following output:
START RequestId: 62fa2f25–669c–47b7–b4e7–47353b0bd914 Version: $LATEST
value1 = value1
value2 = value2
value3 = value3
END RequestId: 62fa2f25–669c–47b7–b4e7–47353b0bd914
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
The code was successfully executed, so let’s add Sentry’s performance
monitoring and test some code that uses it.
Performance monitoring with Sentry
Go to Sentry.io’s homepage.
Sign into your account or sign up for a new free account. You will be at
the main account dashboard after logging in or completing the Sentry sign
Select “Performance” on the left navigation bar, it will take you to the
performance monitoring page.
Click “Start Setup” then go back over to AWS Lambda to complete the
steps for adding Sentry’s Python layer to your Lambda function.
The easiest way to add Sentry to Lambda for this application
is to configure an
AWS Lambda Layer
with the necessary dependency for Sentry. Sentry has concise
documentation on adding via Lambda Layers
so we will walk through that way to configure it and test it
Scroll down to the “Layers” section while in your Lambda
function configuration. Click the “Add a layer” button”:
In the “Add layer” screen, select the “Specify an ARN” option.
Now to specify the Amazon Resource Name (ARN), we need to use
the Sentry documentation to get the right configuration string.
US-East-1 is the oldest and most commonly-used region so I’ll
use that here in this tutorial but you should check which one
you are in if you are not certain.
Copy that value into the Lambda Layer configuration, like this:
Then press the “Add” button. You now have the Sentry dependency
in your environment so code that relies upon that library can be
used in the Lambda function.
Testing performance monitoring
Let’s change our Python code in the Lambda function and test out
the APM agent.
Make sure you are signed into your Sentry account and go to
this specific AWS Lambda set up guide.
You will see a “DSN string” that we need to set as an environment
variable on AWS Lambda to finish our setup. Copy the string that
matches your project as shown on that page in the highlighted green
use environment variables on AWS Lambda
to store and access values like this Sentry DSN key.
Go into the Lambda console to create a new environment variable. To do
that, click the “Configuration” tab within Lambda like you see here:
Then click “Edit” and add a new environment variable with the key of SENTRY_DSN
and the value of the DSN string that you copied from the Sentry screen.
Click the “Save” button and go back to your Lambda function code.
Update your Lambda function with the following highlighted new lines of code
to send errors to Sentry.
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
from sentry_sdk import start_transaction
SENTRY_DSN = os.environ.get(‘SENTRY_DSN’)
def lambda_handler(event, context):
calc = 1000
# this is custom instrumentation, see docs: https://bit.ly/2WjT3AY
with start_transaction(op=“task”, name=“big calculation”):
for i in range(1, 1000):
calc = calc * i
return event[‘key1’] # Echo back the first key value
The above code imports the Sentry dependencies, and then runs both
and custom instrumentation on the
code. Click the “Deploy” button and then “Test”. The code will
successfully execute and when we go back to our Sentry performance
monitoring dashboard we will see some initial results, like this
Looks good, you have both the default and the specified transaction
performance recordings in the dashboard, and you can toggle between
them (or other transactions you record) through the user interface.
We just wrote and executed a Python 3 function on AWS Lambda that
used the basics of Sentry APM to get some initial performance
Check out the AWS Lambda section for
more tutorials by other developers.
Further questions? Contact me on Twitter
or @mattmakai. I am also on GitHub with
the username mattmakai.
Something wrong with this post? Fork
this page’s source on GitHub
and submit a pull request.
Permanent link to this post here