如何克服aws中的冷启动问题?

问题描述 投票:0回答:1

我使用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
        }],
    }
}
python django amazon-web-services zappa cold-start
1个回答
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
    }
}

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.