阅读背景:

AWS lambda无法连接到EC2 Mysql Server

来源:互联网 

I have been trying to connect EC2 mysql server from AWS Lambda which is not happening. I find Lambda function is perfectly working when I just comment database fetching code. I am using Django framework and using zappa to deploy code to Lambda and add setting in AWS API GateWay.

我一直在尝试从AWS Lambda连接EC2 mysql服务器,这是没有发生的。当我只是评论数据库提取代码时,我发现Lambda函数完全正常工作。我正在使用Django框架并使用zappa将代码部署到Lambda并在AWS API GateWay中添加设置。

Things I did:

我做的事情:

  1. Created a user in Mysql with the aws Lambda host address
  2. 使用aws Lambda主机地址在Mysql中创建用户

  3. While deploying with zappa Added Subnet Id and Security Group Id in settings file.
  4. 使用zappa部署时在设置文件中添加了子网ID和安全组ID。

  5. Did not create new VPC as the EC2 instance has default VPC created. So I just used it.
  6. 未创建新的VPC,因为EC2实例已创建默认VPC。所以我只是用它。

  7. I see it Lambda function is working really fine when I just comment Database code(EC2 Mysql database, trying to fetch results from aws lambda).
  8. 我看到它Lambda函数工作得很好我只是评论数据库代码(EC2 Mysql数据库,试图从aws lambda获取结果)。

Code:

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 "})

In the above code If I just comment out comment 1 and comment 2 lines I am receiving status code 200 and returning below response

在上面的代码中,如果我只注释掉注释1和注释2行,我将收到状态代码200并返回到响应之下

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

Which tells Lambda function is working fine but If I do not comment those two lines I am getting below error

这告诉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

I observed above error in API gateway. I don't understand where I am going wrong. Help me to debug this issue

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

EDIT:

EC2 security group details:

EC2安全组详细信息:

Inbound:

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)

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

Outbound:

All trafic

EDIT2:

Cloud watch logs:

云观察日志:

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 个解决方案

#1


0  

Add a try/except in your handler and log any error, that may help you address this issue. When using Lambda proxy integration, any exception that is not handled will result in a Bad Gateway(502) response.

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


分享到: