我已经使用 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
}],
}
}