AWS Lambda
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

Python 中的 AWS Lambda 上下文对象

当 Lambda 运行您的函数时,它会将上下文对象传递到处理程序。此对象提供的方法和属性包含有关调用、函数和执行环境的信息。


  • get_remaining_time_in_millis – Returns the number of milliseconds left before the execution times out.


  • function_name – The name of the Lambda function.

  • function_version – The version of the function.

  • invoked_function_arn – The Amazon Resource Name (ARN) used to invoke the function. Indicates if the invoker specified a version number or alias.

  • memory_limit_in_mb – The amount of memory configured on the function.

  • aws_request_id – The identifier of the invocation request.

  • log_group_name – The log group for the function.

  • log_stream_name – The log stream for the function instance.

  • identity – (mobile apps) Information about the Amazon Cognito identity that authorized the request.

    • cognito_identity_id – The authenticated Amazon Cognito identity.

    • cognito_identity_pool_id – The Amazon Cognito identity pool that authorized the invocation.

  • client_context – (mobile apps) Client context provided to the Lambda invoker by the client application.

    • client.installation_id

    • client.app_title

    • client.app_version_name

    • client.app_version_code

    • cclient.app_package_name

    • custom – 由移动客户端应用程序设置的自定义值的 dict

    • env – 由 AWS 开发工具包提供的环境信息的 dict



import time def get_my_log_stream(event, context):     print("Log stream name:", context.log_stream_name)    print("Log group name:", context.log_group_name)    print("Request ID:",context.aws_request_id)    print("Mem. limits(MB):", context.memory_limit_in_mb) # Code will execute quickly, so we add a 1 second intentional delay so you can see that in time remaining value. time.sleep(1)    print("Time remaining (MS):", context.get_remaining_time_in_millis())

除了上面列出的选项,您还可以使用适用于 在 AWS Lambda 中检测 Python 代码 的 AWS X-Ray 开发工具包来识别关键代码路径、跟踪其性能并收集数据以用于分析。