2024-04-26 18:11:02 发布
网友
我在做AWS Lambda函数。 现在我想isinstance(context,LambdaContext),我希望它能在AWS Lambda中工作。 但我在本地运行单元测试。在
isinstance(context,LambdaContext)
那么如何导入LambdaContext。在
最佳
在/var/runtime/awslambda中定义的LambdaContext类/鞋带用于启动用户功能,具有以下结构:
class LambdaContext(object): def __init__(self, invokeid, context_objs, client_context, invoked_function_arn=None): self.aws_request_id = invokeid self.log_group_name = os.environ['AWS_LAMBDA_LOG_GROUP_NAME'] self.log_stream_name = os.environ['AWS_LAMBDA_LOG_STREAM_NAME'] self.function_name = os.environ["AWS_LAMBDA_FUNCTION_NAME"] self.memory_limit_in_mb = os.environ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] self.function_version = os.environ['AWS_LAMBDA_FUNCTION_VERSION'] self.invoked_function_arn = invoked_function_arn self.client_context = make_obj_from_dict(ClientContext, client_context) if self.client_context is not None: self.client_context.client = make_obj_from_dict(Client, self.client_context.client) self.identity = make_obj_from_dict(CognitoIdentity, context_objs) def get_remaining_time_in_millis(self): return lambda_runtime.get_remaining_time() def log(self, msg): str_msg = str(msg) lambda_runtime.send_console_message(str_msg, byte_len(str_msg))
如果要在本地环境中模拟它,只需将其添加到脚本中:
这不是严格意义上的回答,如果不合适,请删除。在
我尝试过@Dmitry Masanov的答案,并为pytest准备了一个python fixture,它既可以在测试脚本中使用,也可以在conftest.py文件中使用。在
conftest.py
@pytest.fixture def mock_lambda_context(): class ClientContext: """ Class for mocking Context Has `custom`, `env`, and `client` `__slots__` """ __slots__ = ["custom", "env", "client"] def make_obj_from_dict(_class, _dict, fields=None): """ Makes an object of `_class` from a `dict` :param _class: A class representing the context :type _class: `ContextClass` :param _dict: A dictionary of data :type _dict: `dict` :param fields: [description], defaults to None :type fields: [type], optional :return: An object :rtype: `ClientContext` class """ if _dict is None: return None obj = _class() set_obj_from_dict(obj, _dict) return obj def set_obj_from_dict(obj, _dict, fields=None): if fields is None: fields = obj.__class__.__slots__ for field in fields: setattr(obj, field, _dict.get(field, None)) class LambdaContext(object): """ Create a Lambda Context Class object """ def __init__(self, invokeid, client_context, invoked_function_arn=None): self.aws_request_id = invokeid self.log_group_name = "AWS_LAMBDA_LOG_GROUP_NAME" self.log_stream_name = "AWS_LAMBDA_LOG_STREAM_NAME" self.function_name = "AWS_LAMBDA_FUNCTION_NAME" self.memory_limit_in_mb = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" self.function_version = "AWS_LAMBDA_FUNCTION_VERSION" self.invoked_function_arn = invoked_function_arn self.client_context = make_obj_from_dict(ClientContext, client_context) if self.client_context is not None: self.client_context.client = None self.identity = None def get_remaining_time_in_millis(self): return None def log(self, msg): str_msg = str(msg) print(str_msg) lambda_context = LambdaContext("AWS_ID", {}) return lambda_context
我几乎可以肯定我的实现是可以改进的(OO python不是我的强项),但这符合我的目的:
其使用方法与此类似:
再次感谢德米特里,你的回答帮我省了不少麻烦。在
您可以尝试使用LocalStack:
LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications.Currently, the focus is primarily on supporting the AWS cloud stack.LocalStack spins up the following core Cloud APIs on your local machine:API Gateway at http://localhost:4567Kinesis at http://localhost:4568DynamoDB at http://localhost:4569DynamoDB Streams at http://localhost:4570Elasticsearch at http://localhost:4571S3 at http://localhost:4572Firehose at http://localhost:4573Lambda at http://localhost:4574SNS at http://localhost:4575SQS at http://localhost:4576Redshift at http://localhost:4577ES (Elasticsearch Service) at http://localhost:4578SES at http://localhost:4579Route53 at http://localhost:4580CloudFormation at http://localhost:4581CloudWatch at http://localhost:4582
LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications.
Currently, the focus is primarily on supporting the AWS cloud stack.
LocalStack spins up the following core Cloud APIs on your local machine:
API Gateway at http://localhost:4567Kinesis at http://localhost:4568DynamoDB at http://localhost:4569DynamoDB Streams at http://localhost:4570Elasticsearch at http://localhost:4571S3 at http://localhost:4572Firehose at http://localhost:4573Lambda at http://localhost:4574SNS at http://localhost:4575SQS at http://localhost:4576Redshift at http://localhost:4577ES (Elasticsearch Service) at http://localhost:4578SES at http://localhost:4579Route53 at http://localhost:4580CloudFormation at http://localhost:4581CloudWatch at http://localhost:4582
API Gateway at http://localhost:4567
Kinesis at http://localhost:4568
DynamoDB at http://localhost:4569
DynamoDB Streams at http://localhost:4570
Elasticsearch at http://localhost:4571
S3 at http://localhost:4572
Firehose at http://localhost:4573
Lambda at http://localhost:4574
SNS at http://localhost:4575
SQS at http://localhost:4576
Redshift at http://localhost:4577
ES (Elasticsearch Service) at http://localhost:4578
SES at http://localhost:4579
Route53 at http://localhost:4580
CloudFormation at http://localhost:4581
CloudWatch at http://localhost:4582
在/var/runtime/awslambda中定义的LambdaContext类/鞋带用于启动用户功能,具有以下结构:
如果要在本地环境中模拟它,只需将其添加到脚本中:
^{pr2}$这不是严格意义上的回答,如果不合适,请删除。在
我尝试过@Dmitry Masanov的答案,并为pytest准备了一个python fixture,它既可以在测试脚本中使用,也可以在
conftest.py
文件中使用。在我几乎可以肯定我的实现是可以改进的(OO python不是我的强项),但这符合我的目的:
其使用方法与此类似:
^{pr2}$再次感谢德米特里,你的回答帮我省了不少麻烦。在
您可以尝试使用LocalStack:
相关问题 更多 >
编程相关推荐