Zanmato Zanmato - 3 months ago 84
Python Question

Python AWS function failing to return ELB data through Lambda - works on console

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.

Answer

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)