Zappa icon indicating copy to clipboard operation
Zappa copied to clipboard

[Migrated] Cannot invoke task: TypeError: Object of type 'LambdaContext' is not JSON serializable

Open jneves opened this issue 4 years ago • 2 comments

Originally from: https://github.com/Miserlou/Zappa/issues/1097 by robwatkiss

Context

When invoking tasks a TypeError is raised as zappa tries to JSON encode the LambdaContext object.

Expected Behavior

The task should be invoked

Actual Behavior

A TypeError is raised as context.identity (CognitoIdenity instance) cannot be serialized as it has no '__dict__' attribute and hence cannot be serialized.

Object of type 'LambdaContext' is not JSON serializable: TypeError
Traceback (most recent call last):
  File "/var/task/handler.py", line 505, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 242, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 341, in handler
    result = self.run_function(app_function, event, context)
  File "/var/task/handler.py", line 272, in run_function
    result = app_function(event, context) if varargs else app_function()
  File "/var/task/zappa/async.py", line 344, in _run_async
    aws_region=aws_region).send(task_path, args, kwargs)
  File "/var/task/zappa/async.py", line 143, in send
    self._send(message)
  File "/var/task/zappa/async.py", line 151, in _send
    payload = json.dumps(message).encode('utf-8')
  File "/var/lang/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/var/lang/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'LambdaContext' is not JSON serializable

Possible Fix

Remove unserializable types before serialization or define custom encoder. It's a horrible solution, I admit.

# create dict from context with serializable types (this list almost certainly missing many valid types!)
context_serializable = {k:v for k, v in context.__dict__.items() if type(v) in [int, float, bool, str, list, dict]}
json.dumps(context_serializable)

Steps to Reproduce

  1. Create test task
from zappa.async import task

@task
def my_func():
    print("All good!")
  1. Deploy it
  2. Invoke it zappa invoke bugtest 'test.my_func'

Your Environment

  • Zappa version used: 0.43.2
  • Operating System and Python version: Ubuntu, Python 3.6
  • The output of pip freeze:
argcomplete==1.8.2
base58==0.2.4
boto3==1.4.5
botocore==1.5.40
certifi==2017.7.27.1
chardet==3.0.4
click==6.7
docutils==0.14
durationpy==0.5
future==0.16.0
futures==3.1.1
hjson==3.0.0
idna==2.6
jmespath==0.9.3
kappa==0.6.0
lambda-packages==0.16.1
placebo==0.8.1
python-dateutil==2.6.1
python-slugify==1.2.4
PyYAML==3.12
requests==2.18.4
s3transfer==0.1.11
six==1.10.0
toml==0.9.2
tqdm==4.15.0
troposphere==1.9.5
Unidecode==0.4.21
urllib3==1.22
Werkzeug==0.12
wsgi-request-logger==0.4.6
zappa==0.43.2
  • Your zappa_settings.py:
{
    "bugtest": {
        "app_function": "test.my_func",
        "aws_region": "eu-west-2",
        "profile_name": "turbogram",
        "s3_bucket": "zappa-bug-test"
    }
}

jneves avatar Feb 20 '21 08:02 jneves

Reported as an issue in linked closed task. Marking as next-release-candidate for consideration of inclusion in next release.

monkut avatar Jul 19 '22 05:07 monkut

Functions decorated with asynchronous.task are not intended to be invoked externally, as they are not an entry point into the code base.

One workaround would be to have a generic entry point that would then be entered and then call the desired decorated task. Though I'm not sure if the use-case for this is that prevalent.

Documenting how invoke interacts with @task decorated functions works and how to get around it would be a first step.

monkut avatar Nov 19 '22 06:11 monkut

Hi there! Unfortunately, this Issue has not seen any activity for at least 90 days. If the Issue is still relevant to the latest version of Zappa, please comment within the next 10 days if you wish to keep it open. Otherwise, it will be automatically closed.

github-actions[bot] avatar Apr 03 '24 15:04 github-actions[bot]

Hi there! Unfortunately, this Issue was automatically closed as it had not seen any activity in at least 100 days. If the Issue is still relevant to the latest version of Zappa, please open a new Issue.

github-actions[bot] avatar Apr 13 '24 16:04 github-actions[bot]