Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as "serverless" web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance - and at a fraction of the cost of your current deployments!
If you've got a Python web app (including Django and Flask apps), it's as easy as:
$ pip install zappa
$ zappa init
$ zappa deploy
and now you're server-less! Wow!
What do you mean "serverless"?
Okay, so there still is a server - but it only has a 40 millisecond life cycle! Serverless in this case means "without any permanent infrastructure."
With a traditional HTTP server, the server is online 24/7, processing requests one by one as they come in. If the queue of incoming requests grows too large, some requests will time out. With Zappa, each request is given its own virtual HTTP "server" by Amazon API Gateway. AWS handles the horizontal scaling automatically, so no requests ever time out. Each request then calls your application from a memory cache in AWS Lambda and returns the response via Python's WSGI interface. After your app returns, the "server" dies.
Better still, with Zappa you only pay for the milliseconds of server time that you use, so it's many orders of magnitude cheaper than VPS/PaaS hosts like Linode or Heroku - and in most cases, it's completely free. Plus, there's no need to worry about load balancing or keeping servers online ever again.
It's great for deploying serverless microservices with frameworks like Flask and Bottle, and for hosting larger web apps and CMSes with Django. Or, you can use any WSGI-compatible app you like! You probably don't need to change your existing applications to use it, and you're not locked into using it.
Zappa also lets you build hybrid event-driven applications that can scale to trillions of events a year with no additional effort on your part! You also get free SSL certificates, global app deployment, API access management, automatic security policy generation, precompiled C-extensions, auto keep-warms, oversized Lambda packages, and many other exclusive features!
And finally, Zappa is super easy to use. You can deploy your application with a single command out of the box!
Awesome!
<p align="center"> <img src="http://i.imgur.com/f1PJxCQ.gif" alt="Zappa Demo Gif"/> </p>Before you begin, make sure you are running Python 3.8/3.9/3.10/3.11/3.12 and you have a valid AWS account and your AWS credentials file is properly installed.
Zappa can easily be installed through pip, like so:
$ pip install zappa
Please note that Zappa must be installed into your project's virtual environment. The virtual environment name should not be the same as the Zappa project name, as this may cause errors.
(If you use pyenv and love to manage virtualenvs with pyenv-virtualenv, you just have to call pyenv local [your_venv_name]
and it's ready. Conda users should comment here.)
Next, you'll need to define your local and server-side settings.
Zappa can automatically set up your deployment settings for you with the init
command:
$ zappa init
This will automatically detect your application type (Flask/Django - Pyramid users see here) and help you define your deployment configuration settings. Once you finish initialization, you'll have a file named zappa_settings.json in your project directory defining your basic deployment settings. It will probably look something like this for most WSGI apps:
{ // The name of your stage "dev": { // The name of your S3 bucket "s3_bucket": "lambda", // The modular python path to your WSGI application function. // In Flask and Bottle, this is your 'app' object. // Flask (your_module.py): // app = Flask() // Bottle (your_module.py): // app = bottle.default_app() "app_function": "your_module.app" } }
or for Django:
{ "dev": { // The name of your stage "s3_bucket": "lambda", // The name of your S3 bucket "django_settings": "your_project.settings" // The python path to your Django settings. } }
Psst: If you're deploying a Django application with Zappa for the first time, you might want to read Edgar Roman's Django Zappa Guide.
You can define as many stages as your like - we recommend having dev, staging, and production.
Now, you're ready to deploy!
Once your settings are configured, you can package and deploy your application to a stage called "production" with a single command:
$ zappa deploy production
Deploying..
Your application is now live at: https://7k6anj0k99.execute-api.us-east-1.amazonaws.com/production
And now your app is live! How cool is that?!
To explain what's going on, when you call deploy
, Zappa will automatically package up your application and local virtual environment into a Lambda-compatible archive, replace any dependencies with versions with wheels compatible with lambda, set up the function handler and necessary WSGI Middleware, upload the archive to S3, create and manage the necessary Amazon IAM policies and roles, register it as a new Lambda function, create a new API Gateway resource, create WSGI-compatible routes for it, link it to the new Lambda function, and finally delete the archive from your S3 bucket. Handy!
Be aware that the default IAM role and policy created for executing Lambda applies a liberal set of permissions. These are most likely not appropriate for production deployment of important applications. See the section Custom AWS IAM Roles and Policies for Execution for more detail.
If your application has already been deployed and you only need to upload new Python code, but not touch the underlying routes, you can simply:
$ zappa update production
Updating..
Your application is now live at: https://7k6anj0k99.execute-api.us-east-1.amazonaws.com/production
This creates a new archive, uploads it to S3 and updates the Lambda function to use the new code, but doesn't touch the API Gateway routes.
In version 0.53.0, support was added to deploy & update Lambda functions using Docker.
You can specify an ECR image using the --docker-image-uri
option to the zappa command on deploy
and update
.
Zappa expects that the image is built and pushed to a Amazon ECR repository.
Deploy Example:
$ zappa deploy --docker-image-uri {AWS ACCOUNT ID}.dkr.ecr.{REGION}.amazonaws.com/{REPOSITORY NAME}:latest
Update Example:
$ zappa update --docker-image-uri {AWS ACCOUNT ID}.dkr.ecr.{REGION}.amazonaws.com/{REPOSITORY NAME}:latest
Refer to the blog post for more details about how to leverage this functionality, and when you may want to.
If you are using a custom Docker image for your Lambda runtime (e.g. if you want to use a newer version of Python that is not yet supported by Lambda out of the box) and you would like to bypass the Python version check, you can set an environment variable to do so:
$ export ZAPPA_RUNNING_IN_DOCKER=True
You can also add this to your Dockerfile like this:
ENV ZAPPA_RUNNING_IN_DOCKER=True
You can also rollback
the deployed code to a previous version by supplying the number of revisions to return to. For instance, to rollback to the version deployed 3 versions ago:
$ zappa rollback production -n 3
Zappa can be used to easily schedule functions to occur on regular intervals. This provides a much nicer, maintenance-free alternative to Celery!
These functions will be packaged and deployed along with your app_function
and called from the handler automatically.
Just list your functions and the expression to schedule them using cron or rate syntax in your zappa_settings.json file:
{ "production": { ... "events": [{ "function": "your_module.your_function", // The function to execute "expression": "rate(1 minute)" // When to execute it (in cron or rate format) }], ... } }
And then:
$ zappa schedule production
And now your function will execute every minute!
If you want to cancel these, you can simply use the unschedule
command:
$ zappa unschedule production
And now your scheduled event rules are deleted.
See the example for more details.
Sometimes a function needs multiple expressions to describe its schedule. To set multiple expressions, simply list your functions, and the list of expressions to schedule them using cron or rate syntax in your zappa_settings.json file:
{ "production": { ... "events": [{ "function": "your_module.your_function", // The function to execute "expressions": ["cron(0 20-23 ? * SUN-THU *)", "cron(0 0-8 ? * MON-FRI *)"] // When to execute it (in cron or rate format) }], ... } }
This can be used to deal with issues arising from the UTC timezone crossing midnight during business hours in your local timezone.
It should be noted that overlapping expressions will not throw a warning, and should be checked for, to prevent duplicate triggering of functions.
Sometimes an event should be scheduled, yet disabled. For example, perhaps an event should only run in your production environment, but not sandbox. You may still want to deploy it to sandbox to ensure there is no issue with your expression(s) before deploying to production.
In this case, you can disable it from running by setting enabled
to false
in the event definition:
{ "sandbox": { ... "events": [{ "function": "your_module.your_function", // The function to execute "expression": "rate(1
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模 型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。