0

我已经使用 zappa 在 AWS 中部署了 api(Django 应用程序)。我正面临冷启动问题。启动应用程序需要将近 7-8 秒(代码将近 25 MB)。如何克服这个问题?

在 zappa settings.json 中,我保留了 keep_warm=true但没有用。我已经编写了 lambda 函数来使用调度 cloudwatch 事件触发 api,它正在触发(我可以在 zappa 日志中看到)但问题没有解决。

我的处理程序的示例代码是:

import json
def lambda_handler(event, context):
# TODO implement
return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!')
}

我的 zappa 配置是:

{
    "dev": {
        "aws_region": "ap-south-1",
        "django_settings": "api.settings",
        "profile_name": "default",
        "project_name": "api-public",
        "runtime": "python3.6",
        "s3_bucket": "api-public",
        "slim_handler": true,
        "vpc_config" : {
            "SubnetIds": [ "subnet-052347e86b94b75d3" ], // use the private subnet
            "SecurityGroupIds": [ "sg-0ba3a644d413a2b00","sg-0db0b6de5b14cda33"]
        },
        "xray_tracing": true,// Optional, enable AWS X-Ray tracing on your lambda function.
        "memory_size": 1024, // Lambda function memory in MB. Default 512.
        "log_level": "DEBUG", // Set the Zappa log level. Can be one of CRITICAL, ERROR, WARNING, INFO and DEBUG. Default: DEBUG
        "keep_warm": true, // Create CloudWatch events to keep the server warm. Default true. To remove, set to false and then `unschedule`.
        "timeout_seconds": 300,
        "keep_warm_expression": "rate(3 minutes)", // How often to execute the keep-warm, in cron and rate format. Default 4 minutes.
        "exclude": [
            ".git/*",
            ".gitignore",
            "boto3*",
            "*botocore*",
            "django-debug-toolbar*",
            "sqlparse*",
            "zappa_settings.json",
            "README.md"
        ],
        "lambda_description": "zappa deployment public", // However you want to describe your project for the AWS console. Default "Zappa Deployment".
        "extra_permissions": [{ // Attach any extra permissions to this policy. Default None
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction", 
            "Resource": ["arn:aws:lambda:ap-east-1:940180048916:function:api-public-dev"],// AWS Service ARN
        }],
    }
}
4

4 回答 4

0

The periodic hits is simple techniques, yet it would not help with the case of simultaneous requests. One lamdba instance can handle only one request at time. I am not sure how exactly to do it with zappa. Yet the promising solution is using docker checkpoints

https://www.imperial.ac.uk/media/imperial-college/faculty-of-engineering/computing/public/1819-ug-projects/StenbomO-Refunction-Eliminating-Serverless-Cold-Starts-Through-Container-Reuse.pdf

Of course ou can double memory. It would not double to bill because requests would be processed faster.

I also a bit fantasized of adding to zappa fancy strategies like running cheep 1M instances by default but detect cold start and redirect to 3M instances or beans, but checkpoints seems more promising for oversized frameworks like django.

于 2019-11-04T21:49:41.377 回答
0

从技术上讲,如果我们可以接受这是 AWS lambda 函数限制之一的事实,这不是问题。

这里的主要问题是,由于这种限制(延迟),我们强迫自己使用显然不符合要求的 lambda。

如果我们在这种情况下使用 lambda,问题是这样的,

  • 保持 lambda 函数存活一段时间会很昂贵!超级昂贵,显然 lambda(container) 并不是为像那个家伙那样工作而设计的!

我建议不要使用 lambda 通常的工作方式并花费你很多钱,而是使用 EC2 作为你的 Web serer (API),并在其上进行自动缩放和负载平衡。

在这种方法中,从 API 返回的响应会更快,因为您的 api 处于唤醒状态并等待任何请求 - 比在 lambda 上使用它更便宜,因为 lambda 对您的 Lambda 执行的计算时间每 GB 秒收取 0.00001667 美元,想象一下你的 lambda 醒了 10 分钟 :)

希望这可以帮助!:)

干杯! 猴子

于 2019-07-10T08:15:44.977 回答
0

你能包括你的 Zappa 配置吗?keep_warm这是在设置文件的上下文中应如何使用的示例,其中包含更多设置:

{
    "production": {
        "aws_region": "us-east-1",
        "django_settings": "config.zappa",
        "profile_name": "zappa",
        "project_name": "mydomain",
        "runtime": "python3.6",
        "s3_bucket": "zappa-mydomain",
        "certificate_arn": "arn:aws:acm:us-east-1:272727272727:certificate/eeeeeeee-dddd-cccc-bbbb-aaaaaaaaaaaa",
        "domain": "mydomain.com",
        "exclude": [
                ".git/*",
                ".gitignore",
            "boto3*",
            "*botocore*",
            "django-debug-toolbar*",
            "sqlparse*",
            "zappa_settings.json",
            "README.md"
        ],
        "keep_warm": true,
        "timeout_seconds": 300
    }
}

祝你好运!

于 2019-04-18T10:25:34.490 回答
0

如果您需要为 Django 应用程序保温,那么您就以错误的方式使用 AWS Lambda 函数,首先。(相信我,个人经验)

创建 AWS Lambda 函数是为了将轻量级函数部署到世界各地。您的 Django 应用程序是有史以来最重(最)的功能。

AWS Lambda 专为生命周期只有几分之一秒的应用程序而设计。25 MB 本身对 Lambda 函数来说是一个巨大的负载。

考虑改用轻量级框架作为 Flask。不要克服 Lambda 函数。他们不是为此而生的。

请改用 AWS ECS。

于 2020-11-13T01:33:24.350 回答