I spent a little time with Zappa today
which is an AWS Lambda (aka “serverless”) framework for Python. Its not hard to
create a very basic Flask application, then invoke Zappa to perform the many
manual steps of creating a Lambda function, resulting in a URL where your
application is running.
First, I verified I was using Python 3.6, then I created a Python Virtual
environment and installed Zappa and then Flask.
(venv)[user1@hostname hello]$ zappa init
███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
Welcome to Zappa!
Zappa is a system for running server-less Python web applications on AWS Lambda and AWS API Gateway.
This `init`command will help you create and configure your new Zappa deployment.
Let's get started!
Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
What do you want to call this environment (default 'dev'):
AWS Lambda and API Gateway are only available in certain regions. Let's check to make sure you have a profile
set up in one that will work.
Okay, using profile default!
Your Zappa deployments will need to be uploaded to a private S3 bucket.
If you don't have a bucket yet, we'll create one for you too.
What do you want call your bucket? (default 'zappa-fysh7qlsu'):
It looks like this is a Flask application.
What's the modular path to your app's function?
This will likely be something like 'your_module.app'.
We discovered: app.app
Where is your app's function? (default 'app.app'):
You can optionally deploy to all available regions in order to provide fast global service.
If you are using Zappa for the first time, you probably don't want to do this!
Would you like to deploy this application globally? (default 'n')[y/n/(p)rimary]: n
Okay, here's your zappa_settings.json:
{
"dev": {
"app_function": "app.app",
"aws_region": "us-east-1",
"profile_name": "default",
"project_name": "hello",
"runtime": "python3.6",
"s3_bucket": "zappa-fysh7qlsu"
}
}
Does this look okay? (default 'y')[y/n]: y
Done! Now you can deploy your Zappa application by executing:
$ zappa deploy dev
After that, you can update your application code with:
$ zappa update dev
To learn more, check out our project page on GitHub here: https://github.com/Miserlou/Zappa
and stop by our Slack channel here: https://slack.zappa.io
Enjoy!,
~ Team Zappa!
Simple, right? After that I deployed the application:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(venv)[user1@hostname hello]$ zappa deploy dev
Calling deploy for stage dev..
Creating hello-dev-ZappaLambdaExecutionRole IAM Role..
Creating zappa-permissions policy on hello-dev-ZappaLambdaExecutionRole IAM Role.
Downloading and installing dependencies..
- sqlite==python36: Using precompiled lambda package
Packaging project as zip.
Uploading hello-dev-1512173647.zip (5.7MiB)..
100%|██████████████████████████████████████████████████████████████████████████████████████████| 6.02M/6.02M [00:01<00:00, 3.63MB/s]
Scheduling..
Scheduled hello-dev-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Uploading hello-dev-template-1512173657.json (1.6KiB)..
100%|██████████████████████████████████████████████████████████████████████████████████████████| 1.59K/1.59K [00:00<00:00, 10.7KB/s]
Waiting for stack hello-dev to create (this can take a bit)..
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:15<00:00, 5.89s/res]
Deploying API Gateway..
Deployment complete!: https://te5p0mowh3.execute-api.us-east-1.amazonaws.com/dev
And here’s the AWS console showing the invocation count of the lambda function:
Lambda console (image)
To remove the test Lambda I created, use “zappa undeploy”:
1
2
3
4
5
6
7
8
9
(venv)[user1@hostname hello]$ zappa undeploy dev
Calling undeploy for stage dev..
Are you sure you want to undeploy? [y/n] y
Deleting API Gateway..
Waiting for stack hello-dev to be deleted..
Unscheduling..
Unscheduled hello-dev-zappa-keep-warm-handler.keep_warm_callback.
Deleting Lambda function..
Done!