2017-08-06 219 views
0

我一直試圖連接EC2 mysql serverAWS Lambda這是沒有發生。我發現Lambda function是完美的工作,當我只是評論數據庫獲取代碼。我正在使用Django framework並使用zappa將代碼部署到Lambda並在AWS API GateWay中添加設置。AWS lambda無法連接到EC2 Mysql服務器

事情我做:

  1. 創建了一個用戶在Mysql與AWS Lambda主機地址
  2. 雖然與zappa新增Subnet IdSecurity Group Id在設置文件部署。
  3. 未創建新的VPC,因爲EC2實例已創建默認VPC。所以我只是用它。
  4. 我看到它Lambda function工作真的很好,當我只是評論數據庫代碼(EC2的Mysql數據庫,試圖從aws lambda獲取結果)。

代碼:

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:

雲watch日誌:

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 
+0

請提供分配給EC2實例的安全組的詳細信息。另請提供適用於Lambda函數的CloudWatch日誌,其中應包含實際的錯誤消息。 –

+0

增加了Securty組的EC2細節。讓我添加雲手表的詳細信息.. – Naroju

+0

EC2安全組規則的源值是什麼?請提供**所有**的詳細信息。 –

回答

0

添加一個try /除了你的處理程序,並記錄任何錯誤,這可能會幫助你解決這個問題。使用Lambda代理集成時,任何未處理的異常都將導致錯誤網關(502)響應。