AWS lambda无法连接到EC2 Mysql Server

时间:2017-08-06 14:15:06

标签: django python-2.7 amazon-web-services amazon-ec2 aws-lambda

我一直在尝试连接EC2 mysql server来自AWS Lambda但未发生的情况。当我只评论数据库提取代码时,我发现Lambda function完全正常。我正在使用Django framework并使用zappa将代码部署到Lambda并在AWS API GateWay中添加设置。

我做的事情:

  1. 使用aws Mysql主机地址
  2. Lambda中创建了一个用户
  3. 在设置文件中使用zappa部署Subnet IdSecurity Group Id进行部署。
  4. 未创建新的VPC,因为EC2实例已创建默认VPC。所以我只是用它。
  5. 我看到Lambda function在我只是评论数据库代码(EC2 Mysql数据库,试图从aws lambda中获取结果)时工作得非常好。
  6. 代码:

    class GetData(APIView):
    def post(self, request, format=None):
        if request.method == "POST":
            item_id = request.data.get('item_id')
            if item_id not in [None, '', ' ']:
    
                    item = Item.objects.get(item_id=item_id) # Comment 1
                    item_name = item.item_name #  Comment 2
    
                    return Response({"return": "OK OK OK OK"})
            else:
                return Response({"return": "ITEM NOT OK "})
        else:
            return Response({"return": "NOT OK "})
    

    在上面的代码中如果我只评论评论1并评论2行,我收到status code 200并返回以下回复

    {"return": "OK OK OK OK"}
    

    这告诉Lambda函数工作正常但是如果我不评论这两行我得到的错误

    Sun Aug 06 13:48:33 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
    
    Sun Aug 06 13:48:33 UTC 2017 : Method completed with status: 502
    

    我在API gateway中观察到上述错误。我不明白我哪里出错了。帮我调试这个问题

    修改

    EC2安全组详情:

    入站:

    HTTP (80) (0.0.0.0/0 and ::/0)HTTPS(443) (0.0.0.0/0 and ::/0)MYSQL/Aurora(3306)(0.0.0.0/0 and ::/0)SSH(22)(0.0.0.0/0)

    出站:

    All trafic

    EDIT2:

    云监视日志:

    START RequestId: 29f0907a-7ab5-11e7-8f0c-6586a26797eb Version: $LATEST
    [DEBUG] 2017-08-06T14:40:17.154Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing BmpImagePlugin
    [DEBUG] 2017-08-06T14:40:17.170Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing BufrStubImagePlugin
    [DEBUG] 2017-08-06T14:40:17.170Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing CurImagePlugin
    [DEBUG] 2017-08-06T14:40:17.171Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing DcxImagePlugin
    [DEBUG] 2017-08-06T14:40:17.184Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing DdsImagePlugin
    [DEBUG] 2017-08-06T14:40:17.185Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing EpsImagePlugin
    [DEBUG] 2017-08-06T14:40:17.187Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing FitsStubImagePlugin
    [DEBUG] 2017-08-06T14:40:17.188Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing FliImagePlugin
    [DEBUG] 2017-08-06T14:40:17.189Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing FpxImagePlugin
    [DEBUG] 2017-08-06T14:40:17.206Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing FtexImagePlugin
    [DEBUG] 2017-08-06T14:40:17.206Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing GbrImagePlugin
    [DEBUG] 2017-08-06T14:40:17.207Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing GifImagePlugin
    [DEBUG] 2017-08-06T14:40:17.211Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing GribStubImagePlugin
    [DEBUG] 2017-08-06T14:40:17.212Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing Hdf5StubImagePlugin
    [DEBUG] 2017-08-06T14:40:17.212Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing IcnsImagePlugin
    [DEBUG] 2017-08-06T14:40:17.227Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing IcoImagePlugin
    [DEBUG] 2017-08-06T14:40:17.229Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing ImImagePlugin
    [DEBUG] 2017-08-06T14:40:17.230Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing ImtImagePlugin
    [DEBUG] 2017-08-06T14:40:17.231Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing IptcImagePlugin
    [DEBUG] 2017-08-06T14:40:17.244Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing JpegImagePlugin
    [DEBUG] 2017-08-06T14:40:17.272Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing Jpeg2KImagePlugin
    [DEBUG] 2017-08-06T14:40:17.272Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing McIdasImagePlugin
    [DEBUG] 2017-08-06T14:40:17.272Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing MicImagePlugin
    [DEBUG] 2017-08-06T14:40:17.273Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing MpegImagePlugin
    [DEBUG] 2017-08-06T14:40:17.273Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing MpoImagePlugin
    [DEBUG] 2017-08-06T14:40:17.275Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing MspImagePlugin
    [DEBUG] 2017-08-06T14:40:17.276Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PalmImagePlugin
    [DEBUG] 2017-08-06T14:40:17.286Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PcdImagePlugin
    [DEBUG] 2017-08-06T14:40:17.286Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PcxImagePlugin
    [DEBUG] 2017-08-06T14:40:17.286Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PdfImagePlugin
    [DEBUG] 2017-08-06T14:40:17.288Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PixarImagePlugin
    [DEBUG] 2017-08-06T14:40:17.288Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PngImagePlugin
    [DEBUG] 2017-08-06T14:40:17.288Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PpmImagePlugin
    [DEBUG] 2017-08-06T14:40:17.289Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing PsdImagePlugin
    [DEBUG] 2017-08-06T14:40:17.290Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing SgiImagePlugin
    [DEBUG] 2017-08-06T14:40:17.291Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing SpiderImagePlugin
    [DEBUG] 2017-08-06T14:40:17.293Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing SunImagePlugin
    [DEBUG] 2017-08-06T14:40:17.293Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing TgaImagePlugin
    [DEBUG] 2017-08-06T14:40:17.294Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing TiffImagePlugin
    [DEBUG] 2017-08-06T14:40:17.294Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing WebPImagePlugin
    [DEBUG] 2017-08-06T14:40:17.295Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing WmfImagePlugin
    [DEBUG] 2017-08-06T14:40:17.296Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing XbmImagePlugin
    [DEBUG] 2017-08-06T14:40:17.305Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing XpmImagePlugin
    [DEBUG] 2017-08-06T14:40:17.306Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Importing XVThumbImagePlugin
    [DEBUG] 2017-08-06T14:40:17.469Z    29f0907a-7ab5-11e7-8f0c-6586a26797eb    Zappa Event: {u'body': u'{\n\t\n\t"item_id" : "345488692474"\n}', u'resource': u'/{proxy+}', u'requestContext': {u'resourceId': u'z2csqk', u'apiId': u'crmb4ybwrk', u'resourcePath': u'/{proxy+}', u'httpMethod': u'POST', u'requestId': u'test-invoke-request', u'path': u'/{proxy+}', u'accountId': u'xxxxxxxxx', u'identity': {u'apiKey': u'test-invoke-api-key', u'userArn': u'arn:aws:iam::6315xxxxxxx:root', u'cognitoAuthenticationType': None, u'accessKey': u'ASIAxxxxxxxLR4UYLYXEQ', u'caller': u'xxxxxxxxxxxxx', u'userAgent': u'Apache-HttpClient/4.5.x (Java/1.8.0_112)', u'user': u'6xxxxxxxxxxxxx', u'cognitoIdentityPoolId': None, u'cognitoIdentityId': None, u'cognitoAuthenticationProvider': None, u'sourceIp': u'test-invoke-source-ip', u'accountId': u'xxxxxxxxx9'}, u'stage': u'test-invoke-stage'}, u'queryStringParameters': None, u'httpMethod': u'POST', u'pathParameters': None, u'headers': {u'Content-Type': u'application/json'}, u'stageVariables': None, u'path': u'/', u'isBase64Encoded': False}
     END RequestId: 29f0907a-7ab5-11e7-8f0c-6586a26797eb
     REPORT RequestId: 29f0907a-7ab5-11e7-8f0c-6586a26797eb Duration: 20003.70 ms   Billed Duration: 20000 ms Memory Size: 1024 MB  Max Memory Used: 63 MB  
     2017-08-06T14:40:37.000Z 29f0907a-7ab5-11e7-8f0c-6586a26797eb Task timed out after 20.00 seconds
    

1 个答案:

答案 0 :(得分:0)

在处理程序中添加try / except并记录任何错误,这可能有助于解决此问题。使用Lambda代理集成时,任何未处理的异常都将导致Bad Gateway(502)响应。