0

This should be simple so I'm hoping some can help with this quite quickly.

I have the following basic python script:

import boto3
elb = boto3.client('elb')
print(elb.describe_load_balancers())

When I execute this via a python script on the command line it works perfectly, returning all information for all load balancers.

The CLI command also works perfectly from the command line:

aws elb describe-load-balancers

However when I add the script into AWS's Lambda function it fails. This is how the script looks in AWS lambda:

import boto3

def lambda_handler(event, context):
    elb = boto3.client('elb')
    return elb.describe_load_balancers()

Which should work like the others returning all the load balancers data, however it returns this error:

{
  "stackTrace": [
    [
      "/usr/lib64/python2.7/json/__init__.py",
      250,
      "dumps",
      "sort_keys=sort_keys, **kw).encode(obj)"
    ],
    [
      "/usr/lib64/python2.7/json/encoder.py",
      207,
      "encode",
      "chunks = self.iterencode(o, _one_shot=True)"
    ],
    [
      "/usr/lib64/python2.7/json/encoder.py",
      270,
      "iterencode",
      "return _iterencode(o, 0)"
    ],
    [
      "/var/runtime/awslambda/bootstrap.py",
      41,
      "decimal_serializer",
      "raise TypeError(repr(o) + \" is not JSON serializable\")"
    ]
  ],
  "errorType": "TypeError",
  "errorMessage": "datetime.datetime(2013, 7, 26, 15, 35, 57, 690000, tzinfo=tzlocal()) is not JSON serializable"
}

I have been pulling my hair out all day with this so far and can't work out what is wrong, so any help would be appreciated.

As an extra note I was able to get this function working in AWS lambda just fine:

import boto3

def lambda_handler(event, context):
    elb = boto3.client('elb')
    return elb.describe_tags(LoadBalancerNames=[event['loadBalancer']])

As you may notice in the above command I have specifed the load balancer instead of all of them, I have tried this with the other function too but with no luck.

Zanmato
  • 235
  • 8
  • 13
  • It looks like it just can't serialize the return value into JSON. It runs fine locally since you aren't converting the final response into JSON like Lambda does. You might need to add some code to convert the response into something that can be serialized as JSON (it looks like it's failing on datetime values). – Mark B May 10 '16 at 15:44

2 Answers2

3

I was digging same references with Jim.P's and trying on my account. this answer gave me the correct result with proper implementation:

import boto3
import json
import datetime
from time import mktime

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return int(mktime(obj.timetuple()))
        return json.JSONEncoder.default(self, obj)

def lambda_handler(event, context):
    elb = boto3.client('elb')
    return json.dumps(elb.describe_load_balancers(), cls = MyEncoder)
Community
  • 1
  • 1
Ergun Ozyurt
  • 512
  • 3
  • 17
2

This looks like it may be "python" specific, and the reason it probably works on your local machine is you have a python library that is not included in your lambda function.

Several articles describing your error:

StackOverflow - 455580

StackOverflow - 11875770

And documentation on creating a deployment package for Lambda Python:

Creating a Deployment Package (Python)

Community
  • 1
  • 1
Jim P.
  • 1,087
  • 3
  • 9
  • 24