AWS lambda无法连接到EC2 Mysql服务器
问题描述:
我一直试图连接EC2 mysql server
AWS Lambda
这是没有发生。我发现Lambda function
是完美的工作,当我只是评论数据库获取代码。我正在使用Django framework
并使用zappa
将代码部署到Lambda并在AWS API GateWay
中添加设置。AWS lambda无法连接到EC2 Mysql服务器
事情我做:
- 创建了一个用户在
Mysql
与AWSLambda
主机地址 - 虽然与
zappa
新增Subnet Id
和Security Group Id
在设置文件部署。 - 未创建新的
VPC
,因为EC2
实例已创建默认VPC
。所以我只是用它。 - 我看到它
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
答
添加一个try /除了你的处理程序,并记录任何错误,这可能会帮助你解决这个问题。使用Lambda代理集成时,任何未处理的异常都将导致错误网关(502)响应。
请提供分配给EC2实例的安全组的详细信息。另请提供适用于Lambda函数的CloudWatch日志,其中应包含实际的错误消息。 –
增加了Securty组的EC2细节。让我添加云手表的详细信息.. – Naroju
EC2安全组规则的源值是什么?请提供**所有**的详细信息。 –