AWS CDK CodeGuru Profiler (v1)
The `aws-cdk-aws-codeguruprofiler` library provides AWS CDK (v1) constructs for provisioning AWS CodeGuru Profiler resources. It simplifies the creation and management of profiling groups and related configurations within your CDK applications. This entry is for CDK v1, with the current version being 1.204.0. AWS CDK typically follows a rapid release cadence, often aligning with AWS service updates.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.lib'
cause Attempting to use AWS CDK v2 import patterns (e.g., `from aws_cdk.lib import Stack`) while `aws-cdk-aws-codeguruprofiler` (a v1 package) is installed, or `aws-cdk-lib` is not installed.fixIf targeting CDK v1, ensure you install `aws-cdk.core` and use v1 import patterns (e.g., `from aws_cdk import Stack`). If targeting CDK v2, uninstall all v1 `aws-cdk-*` packages and install `aws-cdk-lib`. -
Resource handler returned message: "The specified Profiling Group name already exists."
cause An AWS CodeGuru Profiling Group with the same name already exists in your AWS account and region, preventing the CDK from creating a new one.fixChange the `profiling_group_name` property in your CDK code to a unique name, or manually delete the existing profiling group in the AWS console before redeploying. -
User: arn:aws:iam::123456789012:user/myuser is not authorized to perform: codeguru-profiler:CreateProfilingGroup on resource: arn:aws:codeguru-profiler:us-east-1:123456789012:profilingGroup/MyProfilingGroup
cause The IAM role or user deploying the CDK stack lacks the necessary permissions to create CodeGuru Profiler resources.fixGrant `codeguru-profiler:CreateProfilingGroup`, `codeguru-profiler:TagResource`, and other relevant CodeGuru Profiler permissions to the IAM identity used for CDK deployment.
Warnings
- breaking This package (`aws-cdk-aws-codeguruprofiler`) is part of AWS CDK v1's federated package model. It is not compatible with AWS CDK v2, which uses a single monolithic package (`aws-cdk-lib`). Trying to mix v1 and v2 packages or import patterns will lead to `ModuleNotFoundError` or deployment issues.
- gotcha AWS CodeGuru Profiler requires appropriate IAM permissions for the application being profiled and the CDK deployment role. Ensure your execution roles have `codeguru-profiler:ConfigureAgent`, `codeguru-profiler:PostAgentProfile`, and other necessary permissions (e.g., `codeguru-profiler:CreateProfilingGroup` for the deployment role).
- gotcha Profiling Group names must be unique within an AWS account and region. If you attempt to deploy a stack with a name that already exists (even from a previous failed deployment or manual creation), the deployment will fail.
Install
-
pip install aws-cdk.core aws-cdk-aws-codeguruprofiler
Imports
- ProfilingGroup
from aws_cdk.lib.aws_codeguruprofiler import ProfilingGroup
from aws_cdk.aws_codeguruprofiler import ProfilingGroup
- App
from aws_cdk.App import App
from aws_cdk import App
Quickstart
import os
from aws_cdk import App, Stack, Environment
from aws_cdk import aws_codeguruprofiler as codeguruprofiler
from constructs import Construct
class CodeGuruProfilerStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
profiling_group_name = os.environ.get("CODEGURU_PROFILING_GROUP_NAME", "MyDefaultProfilingGroup")
# Create a CodeGuru Profiling Group
profiling_group = codeguruprofiler.ProfilingGroup(
self,
"MyApplicationProfilingGroup",
profiling_group_name=profiling_group_name,
# Compute Platform is typically configured via agent or defaults to 'Default'
# For AWS Lambda, explicit configuration may be needed or a different construct.
# You can add tags, notifications, etc. here.
)
# Output the ARN of the created profiling group
# cdk.CfnOutput(self, "ProfilingGroupArn", value=profiling_group.profiling_group_arn)
app = App()
CodeGuruProfilerStack(
app,
"CodeGuruProfilerStack",
env=Environment(account=os.environ.get("CDK_DEFAULT_ACCOUNT"), region=os.environ.get("CDK_DEFAULT_REGION"))
)
app.synth()