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

PyCharm中基于Event Bridge的Lambda缺失AWSEvent类导入报错求助

Fixing "cannot import name 'AWSEvent' from 'schema.payload_report_event'" in AWS Lambda

Looks like the root issue here is that your generated Event Bridge schema code didn’t include the top-level AWSEvent wrapper class—this is a common gotcha when generating schema models, especially if you only targeted the detail portion of the Event Bridge event instead of the full event structure. Here’s how to fix it step by step:

1. Regenerate the Schema Code with Full AWSEvent Structure

First, go back to your schema generation step in PyCharm:

  • When selecting the Event Bridge schema to generate from, make sure you’re using a complete Event Bridge event example (not just the detail payload). This should include top-level fields like version, id, detail_type, source, etc.
  • Look for a configuration option (usually a checkbox) labeled "Include AWSEvent wrapper" or similar—enable this before generating the code. This will ensure the tool creates the AWSEvent class alongside your PayrollReportRequest detail model.

2. Manually Add the AWSEvent Class (If Regeneration Fails)

If you can’t get the tool to generate it automatically, update schema/payload_report_event/__init__.py with the appropriate AWSEvent definition. Since you’re using Marshaller from AWS Lambda Powertools, use this version that extends BaseModel for compatibility:

from aws_lambda_powertools.utilities.parser import BaseModel
from typing import Optional, List, TypeVar
from .your_detail_model import PayrollReportRequest  # Replace with your actual detail model import

T = TypeVar("T")

class AWSEvent(BaseModel, Generic[T]):
    version: Optional[str] = None
    id: Optional[str] = None
    detail_type: Optional[str] = None
    source: Optional[str] = None
    account: Optional[str] = None
    time: Optional[str] = None
    region: Optional[str] = None
    resources: List[str] = []
    detail: T

Make sure your PayrollReportRequest class is properly defined in the same directory (or imported correctly) and also extends BaseModel.

3. Verify Deployment Package & Import Paths

  • Double-check your local directory structure: the schema folder should sit at the same level as your hello_world Lambda handler folder. When deploying to Lambda, include the entire schema directory in your deployment zip so it’s available at /var/task/schema.
  • Confirm your import statement in hello_world/app.py is correct (no typos!):
    from schema.payload_report_event import AWSEvent, PayrollReportRequest
    from aws_lambda_powertools.utilities.parser import Marshaller
    

4. Validate Marshaller Usage

Ensure your Lambda code uses the typed models correctly with Marshaller:

def lambda_handler(event, context):
    # Deserialize event into strongly typed object
    aws_event: AWSEvent[PayrollReportRequest] = Marshaller.unmarshall(event, AWSEvent[PayrollReportRequest])
    detail: PayrollReportRequest = aws_event.detail

    # Execute business logic
    # Make updates to event payload, if desired
    aws_event.detail_type = f"HelloWorldFunction updated event of {aws_event.detail_type}"

    # Return event for further processing
    return Marshaller.marshall(aws_event)

After making these changes, redeploy your Lambda function and test it again—this should resolve the import error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:49:48