|
1 | 1 | # Amazon CodeGuru Profiler Python Demo Lambda Application |
2 | 2 |
|
3 | | -Here's a sample code you can run in a Python Lambda in your account. Further instructions will follow soon. |
4 | | - |
5 | | -To enable Amazon CodeGuru Profiler, follow these instructions: |
| 3 | +To enable Amazon CodeGuru Profiler for a Lambda application, follow these instructions: |
6 | 4 | * For Python 3.8: Enable `Code Profiling` for your Lambda's configuration in the `Monitoring and operation tools` tab: https://docs.aws.amazon.com/codeguru/latest/profiler-ug/setting-up-short.html#setting-up-step-2 |
7 | 5 |
|
8 | 6 | * For Python 3.6+: Follow these instructions for enabling Profiler https://docs.aws.amazon.com/codeguru/latest/profiler-ug/python-lambda.html |
9 | 7 |
|
| 8 | +Here's a sample code you can run in a Python Lambda in your account. You can follow the instructions to have a Python Lambda deployed in your account with CodeGuru Profiler enabled. |
| 9 | + |
| 10 | +### Create resources |
| 11 | + |
| 12 | +First step is to create the resources needed to run the application. |
| 13 | + |
| 14 | +* Go to the S3 console, create an S3 bucket and take a note of the name. |
| 15 | + * Example of the name: profiler-python-recommendations-demo |
| 16 | + |
| 17 | +* Create a IAM role with these permissions to be used by the Lambda application |
| 18 | + * Choose Lambda to allow our lambda functions to call AWS services on your behalf. |
| 19 | + * Choose AWSLambdaBasicExecutionRole. |
| 20 | + * Create an inline policy and name it profiler-python-recommendations-demo-policy. |
| 21 | + * Name the role profiler-python-recommendations-demo-role. |
| 22 | +``` |
| 23 | +{ |
| 24 | + "Version": "2012-10-17", |
| 25 | + "Statement": [ |
| 26 | + { |
| 27 | + "Sid": "VisualEditor0", |
| 28 | + "Effect": "Allow", |
| 29 | + "Action": [ |
| 30 | + "s3:PutObject", |
| 31 | + "cloudwatch:PutMetricData" |
| 32 | + ], |
| 33 | + "Resource": "*" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | +``` |
| 38 | +* Create a Lambda with the following configuration: |
| 39 | + * Name: profiler-python-recommendations-demo. |
| 40 | + * Runtime: Python 3.8. |
| 41 | + * Change the default execution role to the newly created one. |
| 42 | + * Add environment variable S3_BUCKET with the value of the bucket name that was just created. |
| 43 | + * Update timeout to 10 seconds. |
| 44 | +* Copy-paste the code from this repository from `lambda_function.py`in the `lambda_function.py` file directly in the Lambda console in the Code tab. |
| 45 | + |
| 46 | +### Run the application |
| 47 | + |
| 48 | +Now, let’s run the application. |
| 49 | + |
| 50 | +* Run Deploy and Test. |
| 51 | + * The Lambda should be successful and print some logs. |
| 52 | + * You can also check the published metrics in the CloudWatch console. |
| 53 | +* Configure it to run every 1 minute. |
| 54 | + * Click “Add trigger”. |
| 55 | + * Choose EventBridge (Cloudwatch Events). |
| 56 | + * Create a new rule named “profiler-python-recommendations-demo-rule”. |
| 57 | + * Set schedule expression to “rate(1 minute)”. |
| 58 | + * Click “Add”. |
| 59 | +* You can check now the Monitor tab to see the CloudWatch metrics about invocations, duration, success and others. |
| 60 | +* You can also go to the Cloudwatch console and see the metrics the application is publishing in the customer namespace as part of the code. |
| 61 | + |
| 62 | +### Enable code profiling |
| 63 | + |
| 64 | +* Enable code profiling by using the [automated onboarding process for Lambda](https://aws.amazon.com/about-aws/whats-new/2021/07/amazon-codeguru-profiler-announces-automated-onboarding-process-aws-lambda-functions/). |
| 65 | + * Go to the Configuration tab. |
| 66 | + * Click Monitoring and operations tools. |
| 67 | + * Click Edit and enable “Code profiling”. |
| 68 | + |
| 69 | +### Analyzing the application |
| 70 | + |
| 71 | +Go to the Monitor tab for your Lambda and then click “View profiles in CodeGuru”. Your profiling group’s name will be the same as your Lambda’s name. |
| 72 | + |
| 73 | +Wait for 15 minutes for CodeGuru Profiler to aggregate and show the data and approximately 1 hour for the recommendations. |
0 commit comments