I'm making AWS Lambda Function.
Now I want to isinstance(context,LambdaContext)
, I'm expecting It works in AWS Lambda.
But I'm running unit test in local.
So How can I import LambdaContext.
Best
When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment.
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.
Using the Lambda consoleYou can use the Lambda console to view log output after you invoke a Lambda function. For more information, see Accessing Amazon CloudWatch logs for AWS Lambda.
Just found this package, it seems that someone made a pip package using the concepts from the other answers in this question.
Had the same issue, hope it helps whoever comes across this question in the future.
This is not in the strictest sense an answer, please remove if unsuitable.
I have experimented with @Dmitry-Masanov 's answer, and have bodged a python fixture for pytest, which can be used either in the test script itself, or as I am doing, in the conftest.py
file.
@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
I'm nearly certain my implementation can be improved (OO python isn't a strong skill of mine), but this serves my purpose of:
This can be used in a way similar to this:
@pytest.mark.smoke
def test_lambda_handler(apigw_event, mock_lambda_context):
ret = app.lambda_handler(apigw_event, mock_lambda_context)
assert "log_stream_name" in ret["body"]
Thanks again Dmitry, your answer saved me a bunch of hassle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With