Déploiement sur AWS Lambda avec Zappa & Flask

Mise 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

  1. créer un compte AWS.
  2. créer une stratégie IAM
  3. ajouter cette stratégie à un groupe
  4. avoir un fichier local contenant un accès AWS conforme. Le CLI AWS peut aider.

Python

  1. un virtualenv avec python3.6 ou python2.7
  2. source .venv/bin/activate
  3. pip install flask zappa
  4. 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.