Zappaを使ってFlask appをAWS Lambda と API Gatewayにデプロイしてみた

2018.11.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

前回、Flask appを作ってみたので、Zappaを使って、AWS環境にデプロイしてみました。

Flaskで英単語を数えるアプリケーションを動かしてみた

Zappaとは

ZAPPAを使うと、Pythonアプリケーションを AWS Lambda と API Gatewayに構築、デプロイしてくれます。

Zappaはプロジェクトの仮想環境にインストールしますので、pyenvpyenv-virtualenvをインストールしておきます。

仮想環境を用意してZappaを使ってみましょう。

$ pyenv local [your_venv_name]

Zappaを使ってみる

インストール

仮想環境でインストールします。

$ pip install 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'):

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: default, dev, prd, and stg. Which would you like us to use? (default 'default'):

profileが複数ある場合は選択します。

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-*********'):

S3Bucket名はdefaultにしました。

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: hello.app, test.app, application.app
Where is your app's function? (default 'hello.app'):

アプリケーションを選択します。application.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": "application.app",
        "aws_region": "us-west-2",
        "profile_name": "default",
        "project_name": "en-words-count-",
        "runtime": "python3.6",
        "s3_bucket": "zappa-*********"
    }
}

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!

初期設定完了です。

Deploy

デプロイします

$ zappa deploy dev

Deployment complete!: https://**********.execute-api.us-west-2.amazonaws.com/dev

curlで確認

$ curl -I https://**********.execute-api.us-west-2.amazonaws.com/dev
HTTP/2 200

ドメインでアクセスできるようにする

AWS Certificate Managerを使用してドメインでアクセスできるようにします。
note: us-east-1で作成したものを使用します。

zappa_settings.json を修正します。

domaincertificate_arnを追加します

$ vi zappa_settings.json
{
    "dev": {
        "app_function": "application.app",
        "aws_region": "us-west-2",
        "profile_name": "default",
        "project_name": "en-words-count-",
        "runtime": "python3.6",
        "s3_bucket": "zappa-*********",
        "domain": "your_domian",
        "certificate_arn": "your_acm_arn"
    }
}

zappa certify コマンドで 証明書をAPI Gatewayへデプロイします。

$ zappa certify
Calling certify for stage dev..
Are you sure you want to certify? [y/n] y
Certifying domain wordcounter.your_domian..
Created a new domain name with supplied certificate. Please note that it can take up to 40 minutes for this domain to be created and propagated through AWS, but it requires no further work on your part.
Certificate updated!

しばらく待って、アクセスします。

画像

ドメインでアクセスしてアプリケーションが動作することが確認できました。

まとめ

zappaを使って、サーバレス、イベントドリブンのPythonアプリケーションをAWS Lambda + API Gatewayへデプロイする方法をご紹介しました。

参考URL