PyCharm中基于Event Bridge的Lambda缺失AWSEvent类导入报错求助
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
detailpayload). This should include top-level fields likeversion,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
AWSEventclass alongside yourPayrollReportRequestdetail 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
schemafolder should sit at the same level as yourhello_worldLambda handler folder. When deploying to Lambda, include the entireschemadirectory in your deployment zip so it’s available at/var/task/schema. - Confirm your import statement in
hello_world/app.pyis 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

