Déploiement sur AWS Lambda avec Zappa & Flask
dans Bloc-notesMise en place d'un bac à sable. L'idée est de jouer avec l'approche server-less dans un contexte 100% python en utilisant Zappa associé a une application Flask dans le but de déployer le code sur AWS Lambda. Les essais de code et les différants déploiements réalisés sont disponibles sur le dépôt Gitlab.
AWS
- créer un compte AWS.
- créer une stratégie IAM
- s'inspirer de la discussion et du template du dépôt
zappa
- s'inspirer de la discussion et du template du dépôt
- ajouter cette stratégie à un groupe
- avoir un fichier local contenant un accès AWS conforme. Le CLI AWS peut aider.
Python
- un virtualenv avec
python3.6
oupython2.7
- sous Debian testing/buster, la version de virtualenv fournie par
apt
n'est plus utilisable
- sous Debian testing/buster, la version de virtualenv fournie par
source .venv/bin/activate
pip install flask zappa
- faire une petite application Flask
from flask import Flask
app = Flask(__name__)
@app.route('/', defaults={'url_param': 'world'})
@app.route('/<string:url_param>')
def index(url_param):
return "Hello {}. Zappa rocks!".format(url_param)
La magie Zappa
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.
We found the following profiles: adminuser, fred-cli-lambda, and default. Which would you like us to use? (default '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 to call your bucket? (default 'zappa-664wolx7u'):
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: run.app
Where is your app's function? (default 'run.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]:
Okay, here's your zappa_settings.json:
{
"dev": {
"app_function": "run.app",
"aws_region": "eu-west-3",
"profile_name": "default",
"project_name": "hellozappa",
"runtime": "python3.6",
"s3_bucket": "zappa-664wolx7u"
}
}
Does this look okay? (default 'y') [y/n]:
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!
zappa deploy dev
=> En ligne !
Plus de code et d'info sur le dépôt Gitlab.