在本地运行 AWS SAM 会抛出 aws_lambda_powertools not found

问题描述 投票:0回答:1

我正在尝试使用

aws-lambda-powertools
设置 AWS SAM python 项目。我也将
aws-lambda-powertools
添加到了要求中。当我将其部署到 AWS 时,
aws-lambda-powertools
的导入工作正常。 但是,在本地运行时
sam local start-api -t template.yaml
会抛出

{
  "errorMessage": "Unable to import module 'app' No module named 'aws_lambda_powertools'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "996dae2b-7708-4693-9657-6e3aaef4fc2d",
  "stackTrace": []
}

python aws lambda 代码是:

from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools import Logger
from aws_lambda_powertools import Tracer
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

app = APIGatewayRestResolver()
tracer = Tracer()
logger = Logger()
metrics = Metrics(namespace="Powertools")

@app.get("/hello")
@tracer.capture_method
def hello():
    # adding custom metrics
    # See: https://awslabs.github.io/aws-lambda-powertools-python/latest/core/metrics/
    metrics.add_metric(name="HelloWorldInvocations", unit=MetricUnit.Count, value=1)

    # structured log
    # See: https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/
    logger.info("Hello world API - HTTP 200")
    return {"message": "hello world"}

# Enrich logging with contextual information from Lambda
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
# Adding tracer
# See: https://awslabs.github.io/aws-lambda-powertools-python/latest/core/tracer/
@tracer.capture_lambda_handler
# ensures metrics are flushed upon request completion/failure and capturing ColdStart metric
@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

我的 SAM 模板有,

Globals: # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html
  Function:
    Timeout: 5
    MemorySize: 128
    Runtime: python3.12

    Tracing: Active
    # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig.
    LoggingConfig:
      LogFormat: JSON
  Api:
    TracingEnabled: true
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function     # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:
      Handler: app.lambda_handler
      CodeUri: hello_world
      Description: Hello World function
      Architectures:
      - x86_64
      Tracing: Active
      Events:
        HelloPath:
          Type: Api           # More info about API Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html
          Properties:
            Path: /hello
            Method: GET

我需要遵循一些额外的配置吗?

python amazon-web-services aws-lambda aws-sam aws-sam-cli
1个回答
0
投票

在启动调试命令之前尝试安装依赖项。

看看你的代码试试

  • pip install aws_lambda_powertools
    安装
  • pip list
    来验证

我正在使用 venv虚拟环境中运行我的 python 代码,因此我需要使用 VS Code 终端将 deps 安装到虚拟环境中,而不是全局环境中。

PS 可能需要安装的另一个常见的 AWS 依赖项是 boto3,因此执行

  • pip install boto3 aws_lambda_powertools
© www.soinside.com 2019 - 2024. All rights reserved.