question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(lambda): profiling python support

See original GitHub issue

I am proposing adding a Resource for CodeGuru Lambda Layer. Currently when setting up Code Guru layer you will need to do something like this:

import lambda = require('@aws-cdk/aws-lambda');
const layerArn = `arn:aws:lambda:us-west-1:580247275435:layer:AWSCodeGuruProfilerJavaAgentLayer:3`;
const layer = lambda.LayerVersion.fromLayerVersionArn(this, `LayerFromArn`, layerArn);

I propose it should be more like:

new CodeGuruLayer(this, "CodeGuruLayer', {});

Use Case

  1. We wouldn’t need to hardcode arns anymore which leaves room for error
  2. The resource could by default use the latest version so that consumers would not need to keep track of what version they are using(we should still provide option to use any version through props)
  3. We can add the supported runtimes to the resource so that issues can be found at build time
  4. We can also add supported regions to the resource, to find issues at build time

Proposed Solution

Proposed solution could roughly look like this. This would require exporting LayerVersionBase from lambda module. Or maybe another abstract class is warranted. That abstract class could be used for other layers as well, such as the lambda insights layer.

export interface CodeGuruLayerProps {
  readonly region: string;
  readonly version?: number;
  readonly compatibleRuntimes?: Runtime[];
}

export class CodeGuruLayer extends LayerVersionBase {
  public static LATEST_VERSION = 3; // eslint-disable-line
  public readonly layerVersionArn: string;
  public readonly compatibleRuntimes?: Runtime[] = Runtime.ALL;

  public constructor(scope: Construct, id: string, props: CodeGuruLayerProps) {
    super(scope, id);

    const version = props.version != undefined ? props.version : CodeGuruLayer.LATEST_VERSION;

    if (version > CodeGuruLayer.LATEST_VERSION || version < 1) {
      throw new Error(`Not a valid CodeGuru version: ${version}`);
    }

    this.layerVersionArn = `arn:aws:lambda:${props.region}:157417159150:layer:AWSCodeGuruProfilerJavaAgentLayer:${version}`;
  }
}
  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nija-atcommented, Feb 17, 2021

I would propose that we go one step further. If the runtime is Python and the the profiling property is set, we should just add the layer to the function.

@jonnekaunisto - would that satisfy your requirement as well?

0reactions
greg5123334commented, Mar 10, 2022

I seem to be experiencing a similar problem. CDK provisions the resources and configurations as per the prerequisites, and after confirming that the layer, env vars, and permissions are in place, the ProfilingGroup still remains in a Setup Required state. For reference, please find a snippet of my code below:

CDK

        let region = Stack.of(this).region
        const CodeGuruLayer = LayerVersion.fromLayerVersionArn(this, "CodeGuruLayer",
            'arn:aws:lambda:' + region + ':157417159150:layer:AWSCodeGuruProfilerPythonAgentLambdaLayer:11',
        )

        const fn = new lambda.Function(this, 'MyFunction', {
            runtime: lambda.Runtime.PYTHON_3_9,
            handler: 'base.main',
            code: Code.fromAsset("./assets"),
            profiling: true,
            environment: {
                'AWS_LAMBDA_EXEC_WRAPPER': '/opt/codeguru_profiler_lambda_exec',
                'AWS_CODEGURU_PROFILER_TARGET_REGION': region,
            },
            layers: [CodeGuruLayer],
        });
        fn.role?.addManagedPolicy(
            iam.ManagedPolicy.fromAwsManagedPolicyName(
                'AmazonCodeGuruProfilerAgentAccess',
            ),
        );

lambda handler: base.py

import logging

logging.basicConfig(level=logging.DEBUG)

def main(event, context):
    # save event to logs
    logging.info(event)

    return {
        'statusCode': 200,
        'body': event
    }

As far i can can deduce, it seems almost as if the agent in the layer is not running correctly, but please correct me if i am wrong. Otherwise, please let me know if there is any more information i can provide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Profiling your applications that run on AWS Lambda
You can profile your Lambda functions running in Python if they are called often enough for CodeGuru Profiler to gather enough samples. CodeGuru...
Read more >
AWS CDK — How to code profiling your Lambda functions ...
AWS CDK — How to code profiling your Lambda functions by enabling AWS CodeGuru for Python. If you want to code profiling on...
Read more >
Improve the performance of Lambda applications ... - Noise
Afterwards, CodeGuru Profiler provides recommendations to help resolve ... CodeGuru Profiler on an AWS Lambda function written in Python.
Read more >
How to Profile AWS Lambda Functions - Pyroscope
AWS lambda is a popular serverless computing service that allows you to write code in any language and run it on AWS.
Read more >
Serverless Python profiler for AWS Lambda using AWS X-Ray
Last week at AWS re:Invest 2017, I decided to give a try to AWS X-Ray, the AWS Distributed tracing system. This service has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found