简体   繁体   English

Python AWS函数无法通过Lambda返回ELB数据-在控制台上工作

[英]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: 我有以下基本的python脚本:

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. 当我在命令行上通过python脚本执行此操作时,它可以完美运行,返回所有负载平衡器的所有信息。

The CLI command also works perfectly from the command line: CLI命令也可以从命令行完美运行:

aws elb describe-load-balancers

However when I add the script into AWS's Lambda function it fails. 但是,当我将脚本添加到AWS的Lambda函数中时,它将失败。 This is how the script looks in AWS lambda: 这是该脚本在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: 需要特别注意的是,我能够在AWS lambda中使此功能正常工作:

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. 如您在上面的命令中可能注意到的,我指定了负载均衡器而不是全部负载均衡器,我也尝试了使用其他功能,但是没有运气。

I was digging same references with Jim.P's and trying on my account. 我正在与Jim.P一起检索相同的参考文献,并尝试使用我的帐户。 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)

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. 看起来它可能是特定于“ python”的,并且它可能在本地计算机上工作的原因是您有一个lambda函数未包含的python库。

Several articles describing your error: 多篇描述您的错误的文章:

StackOverflow - 455580 StackOverflow-455580

StackOverflow - 11875770 StackOverflow-11875770

And documentation on creating a deployment package for Lambda Python: 以及有关为Lambda Python创建部署程序包的文档:

Creating a Deployment Package (Python) 创建部署程序包(Python)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM