You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在IntelliJ IDEA中调试AWS CDK创建的Python Lambda遇问题

AWS CDK创建的Python Lambda无法在IntelliJ IDEA中显示调试Gutter图标

问题背景

通过AWS CDK构建了返回"hello, World!"的Python Lambda,可正常完成堆栈合成与部署,也能通过SAM本地调用,但在IntelliJ IDEA中始终无法看到Lambda handler的gutter调试图标,无法启动本地调试。已安装AWS Toolkit并关联AWS账户,且开启了Settings > Tools > AWS > Lambda中的show gutter icons for all potential AWS Lambda handlers选项。

项目目录结构:

greeting-service/
├── lib/
│   ├── greeting-service-stack.ts
│   └── greeting-service.ts
└── resources/
    └── greeting.py

相关代码片段:

greeting-service/lib/greeting-service-stack.ts

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as greeting_service from '../lib/greeting-service'

export class GreetingServiceStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new greeting_service.GreetingService(this, 'GreetingService');
  }
}

greeting-service/lib/greeting-service.ts

import * as cdk from "aws-cdk-lib";
import {Construct} from "constructs";
import * as apigateway from "aws-cdk-lib/aws-apigateway";
import * as lambda from "aws-cdk-lib/aws-lambda";

export class GreetingService extends Construct {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        const greetingApiGateway = new apigateway.RestApi(this, "Greeting API", {
            restApiName: "Greeting API",
            description: "This API says Hello"
        });

        const greetingLambdaFunction = new lambda.Function(this, 'GreetingFunction', {
            runtime: lambda.Runtime.PYTHON_3_8,
            handler: 'greeting.handler',
            code: lambda.Code.fromAsset("resources")
        });

        const greetingIntegration = new apigateway.LambdaIntegration(greetingLambdaFunction, {
            requestTemplates: {
                "application/json": '{ "statusCode": "200" }'
            }
        });

        greetingApiGateway.root.addMethod("GET", greetingIntegration);
    }
}

greeting-service/resources/greeting.py

import json


def handler(event, context):
    print('Request: {}', json.dumps(event))
    print('Context {}', context)
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": json.dumps({
            "greeting ": "hello, World!"
        })
    }

解决方案

  • 标记Python源码根目录
    右键resources目录,选择Mark Directory as > Sources Root,让IntelliJ识别该目录为Python源码目录,确保插件能正确扫描Lambda handler函数。

  • 配置匹配的Python SDK
    打开File > Project Structure:

    1. 在SDKs中添加或确认存在Python 3.8版本的SDK(与Lambda runtime版本一致);
    2. 在Project中设置该SDK为项目默认SDK;
    3. 在Modules中确保resources模块关联了此Python SDK。
  • 清除IDE缓存并重启
    打开File > Invalidate Caches...,勾选Clear file system cache and local history,点击Invalidate and Restart,解决插件缓存导致的识别异常。

  • 手动创建Lambda调试配置
    如果gutter图标仍不出现,可手动添加调试配置:

    1. 点击右上角运行配置下拉框,选择Edit Configurations...;
    2. 点击+号,选择AWS Lambda;
    3. 配置参数:
      • Handler填写greeting.handler;
      • Runtime选择Python 3.8;
      • Code path选择greeting-service/resources目录;
      • 可选:添加测试事件(比如API Gateway默认事件);
    4. 保存配置后即可直接点击调试按钮启动本地调试。
  • 通过CDK生成的SAM模板关联Lambda

    1. 在项目根目录执行命令:cdk synth > template.yaml,生成SAM兼容的CloudFormation模板;
    2. 在IntelliJ中打开template.yaml,AWS Toolkit会自动识别其中的Lambda资源,右键对应Lambda条目即可选择调试。
  • 更新AWS Toolkit到最新版本
    打开Settings > Plugins,检查AWS Toolkit是否为最新版本,旧版本可能存在Python Lambda识别的bug,更新后重启IDE重试。

内容的提问来源于stack exchange,提问作者afx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:40:38