Performance with AWS CDK
Discussed in https://github.com/microsoft/pylance-release/discussions/6984
Originally posted by arjentraas February 24, 2025 Developing Python in VS Code with Windows Subsystem for Linux (WSL) is the best Python development experience for me so far.
The only exception is writing infrastructure as code using the Cloud Development Kit (CDK) from Amazon Web Services (AWS). Pylance is struggling a lot to load type hints, auto imports and syntax highlighting. It may take up to 10 seconds to load that kind of stuff. It even crashes if I write to much code in a short time window.
Switching from WSL to Windows helps a bit. Pylance speeds up a little, while other things slow down, so I end up accepting the CDK performance hit and reverting to WSL.
These log entries show up a lot:
[BG(1)] Long operation: analyzing: file:///home/user/sources/my_application/stacks/my_stack.py (2541ms)
Anyone have the same experience? Or even better, a solution?
Here's some example code that reproduces the issue:
from aws_cdk import Stack, aws_lambda as _lambda
from aws_cdk import aws_sqs as sqs
from aws_cdk import aws_lambda_event_sources as eventsources
from aws_cdk import aws_iam as iam
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_ecr as ecr
from aws_cdk import aws_rds as rds
from constructs import Construct
class LambdaSqsStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create a VPC with public subnets
vpc = ec2.Vpc(
self, "MyVPC",
max_azs=2,
subnet_configuration=[
ec2.SubnetConfiguration(
name="PublicSubnet",
subnet_type=ec2.SubnetType.PUBLIC
)
]
)
# Create an ECR repository for storing the Lambda container image
ecr_repository = ecr.Repository(self, "MyLambdaRepository")
# Create an SQS queue
queue = sqs.Queue(self, "MyQueue")
# Create an IAM role for the Lambda function
lambda_role = iam.Role(
self, "LambdaExecutionRole",
assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),
managed_policies=[
iam.ManagedPolicy.from_aws_managed_policy_name("service-role/AWSLambdaBasicExecutionRole"),
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonSQSFullAccess"),
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonRDSFullAccess")
]
)
# Create an Aurora database
db_cluster = rds.DatabaseCluster(
self, "MyAuroraCluster",
engine=rds.DatabaseClusterEngine.AURORA_MYSQL(),
instances=1,
vpc=vpc,
credentials=rds.Credentials.from_generated_secret("admin"),
default_database_name="mydatabase"
)
# Create a Lambda function that processes messages from the queue using a container image
lambda_function = _lambda.DockerImageFunction(
self, "MyLambda",
code=_lambda.DockerImageCode.from_ecr(ecr_repository),
environment={
"QUEUE_URL": queue.queue_url
},
role=lambda_role,
vpc=vpc
)
# Grant the Lambda function permissions to read messages from the queue
queue.grant_consume_messages(lambda_function)
# Configure the Lambda function to be triggered by the SQS queue
lambda_function.add_event_source(eventsources.SqsEventSource(queue))
# Create a Lambda function that publishes messages to the queue and accesses the Aurora database
publisher_lambda = _lambda.Function(
self, "PublisherLambda",
runtime=_lambda.Runtime.PYTHON_3_9,
handler="publisher_lambda.handler",
code=_lambda.Code.from_asset("lambda"),
environment={
"QUEUE_URL": queue.queue_url,
"DB_ENDPOINT": db_cluster.cluster_endpoint.hostname
},
role=lambda_role,
vpc=vpc
)
# Grant the publisher Lambda permissions to send messages to the queue and access the database
queue.grant_send_messages(publisher_lambda)
db_cluster.grant_connect(publisher_lambda)
app = core.App()
LambdaSqsStack(app, "LambdaSqsStack")
app.synth()
Hover over _lambda.Function. Analysis will take 3 seconds for me.
Is this the issue that @heejaechang was addressing with https://github.com/microsoft/pyright/pull/9993?
No, I don't believe so. This is not a memory problem but rather a performance issue.
Update:
- a colleague has the same issue with Windows, WSL, Pyright, but without Pylance and VSCode (using neovim). He has the exact same machine as me.
- another colleague does not have the issue, using MacOS, Pylance and VSCode.
It makes me think that it's a issue in the Pyright implementation in Windows/WSL.
I am experiencing similar latencies with auto complete on Linux.
This performance issue doesn't seem to be limited to Windows/WSL. My PC Config is: Ubuntu 24.04, AMD Ryzen 5 5500U, 24GB RAM and 1TB SSD.
The experience of using CDK with TypeScript is much better.