【问题标题】:AWS lambda could not connect to EC2 Mysql ServerAWS lambda 无法连接到 EC2 Mysql Server
【发布时间】:2018-01-13 22:21:26
【问题描述】:

我一直在尝试从AWS Lambda 连接EC2 mysql server,但没有发生。我发现Lambda function 在我只是评论数据库获取代码时工作得很好。我正在使用Django framework 并使用zappa 将代码部署到Lambda 并在AWS API GateWay 中添加设置。

我做过的事情:

  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:

云端观看日志:

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

【问题讨论】:

  • 请提供分配给 EC2 实例的安全组的详细信息。另请提供 Lambda 函数的 CloudWatch 日志,其中应包含实际错误消息。
  • 添加了 EC2 详细信息的安全组。让我也添加云手表详细信息..
  • EC2 安全组规则的源值是什么?请提供所有详细信息。
  • > 在 Mysql 中使用 aws Lambda 主机地址创建了一个用户。 您是否在只允许使用一个 IP 地址的配置中运行 lambda?这将是一个非标准的实现,您需要不遗余力地做到这一点。您是否尝试过创建相同的user@% 并查看是否可以连接?最常见的是我在 RDS 中创建 @% 用户并允许安全组正确过滤流量。
  • 是的,我错过了关于使用 “aws Lambda 主机地址” 的部分。您甚至如何提前获取 Lambda 主机地址?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-12-13
    • 2018-05-13
    • 2021-09-18
    • 2020-10-19
    • 2019-11-10
    • 2019-09-01
    • 2017-10-12
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多