简体   繁体   English

如何在 lambda 函数中查看上下文变量

[英]How to see context variable in lambda function

I set sample lambda function as follows to detect what is context .我按如下方式设置示例 lambda 函数以检测什么是context This function is hooked by API gateway.这个函数被 API 网关 hook。

import json

def lambda_handler(event, context):
    return {
        'isBase64Encoded': False,
        'statusCode': 200,
        'headers': {},
        'body': json.dumps(context)
    }

And when I send GET request from API gateway it returned as follows当我从 API 网关发送 GET 请求时,它返回如下

{
  "message": "Internal server error"
}
Mon May 24 07:20:58 UTC 2021 : Lambda execution failed with status 200 due to customer function error: Object of type LambdaContext is not JSON serializable. Lambda request id: 32d4e450-576b-4bd6-abb9-d1bd893077ed
Mon May 24 07:20:58 UTC 2021 : Method completed with status: 502

context is not json format? context不是json格式? How can I see context in handler?如何在处理程序中查看context

If someone has opinion,please let me know如果有人有意见,请告诉我

Thanks谢谢

You can print it and see if you want.您可以打印它,看看是否需要。 It would be something like this:它会是这样的:

LambdaContext([aws_request_id=7d78a745-bb86-42b8-89f0-389e9bdc3dcfe,log_group_name=/aws/lambda/mldx-devops-v1-TimeLambda-hINHLpDGpaNR,log_stream_name=2022/09/01/[$LATEST]0553dabc81194a57adf2a96775f88d02,function_name=mldx-devops-v1-TimeLambda-hINHLpDGpaNR,memory_limit_in_mb=128,function_version=$LATEST,invoked_function_arn=arn:aws:lambda:eu-west-1:187276065257:function:mldx-devops-v1-TimeLambda-hINHLpDGpaNR,client_context=None,identity=CognitoIdentity([cognito_identity_id=None,cognito_identity_pool_id=None])])

Print it with this code:)使用此代码打印它:)

def lambda_handler(event: Dict[str, Optional[Any]], context: LambdaContext) -> Dict[str, Optional[Any]]:
print(event)
print('print context variables')
print("context.function_name:", context.function_name)
print("context.function_version:", context.function_version)
print("context.invoked_function_arn:", context.invoked_function_arn)
print("context.memory_limit_in_mb", context.memory_limit_in_mb)
print("context.aws_request_id", context.aws_request_id)
print("context.log_group_name", context.log_group_name)
print("context.log_stream_name", context.log_stream_name)

print("print context.identity variables")
print("context.identity.cognito_identity_id", context.identity.cognito_identity_id)
print("context.identity.cognito_identity_pool_id", context.identity.cognito_identity_pool_id)

print("print context.client_context variables")
print("context.client_context.client.app_package_name", context.client_context.client.app_package_name)
print("context.client_context.client.app_version_name", context.client_context.client.app_version_name)
print("context.client_context.client.app_version_code", context.client_context.client.app_version_code)
print("context.client_context.client.app_title", context.client_context.client.app_title)
print("context.client_context.client.installation_id", context.client_context.client.installation_id)
print("context.client_context.custom", context.client_context.custom)
print("context.client_context.env", context.client_context.env)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在lambda function中,如何在HTML中显示python变量 - In the lambda function, how to display python variable in HTML Lambda function 将其变量值存储多长时间? - How long does Lambda function store its variable value? 从 Spring 云访问 AWS Lambda 上下文 Function - Accessing AWS Lambda Context from Spring Cloud Function 在 python 中更新一个 AWS Lambda function 的环境变量 - Update an environment variable of an AWS Lambda function in python 如何查看s3到lambda的事件通知 - How to see the event notification from s3 to lambda 如何退出 NodeJs lambda function? - How to exit a NodeJs lambda function? 如何在lambda function中使用pyhive? - how to use pyhive in lambda function? AWS Lex 机器人在 lex 机器人的履行部分调用 lambda function,我看不到可以调用 lambda function 的地方 - AWS Lex bot calling a lambda function in fulfilment section of the lex bot, I don't see a place to call the lambda function aws-lambda中使用APIGatewayProxyRequestEvent时如何获取日志的Context - How to get Context for logging when using APIGatewayProxyRequestEvent in aws-lambda 为什么我在创建 AWS Lambda function 后看不到编辑内联代码和处理程序信息的选项 - why I can't see the option to edit code inline and handler Info on after creating the AWS Lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM