[PYTHON] The first step to creating a serverless application with Zappa

I will be in charge of the 8th day of Kinki University Advent Calendar 2019, and Qiita will be the second post since CSG's summer Advent Calendar. Become. This time, I'm talking about how you can easily operate AWS Lambda-based serverless applications using Zappa. It is written for students, so please understand that there are some differences.

What is a serverless application?

Until a long time ago, when developing a client-server type application such as a web application, a server machine was required, and in order to run a server program in it, a web server such as Apache, for example, It was common to set up middleware called a Java Servlet container like Tomcat (is it a bit old?). A serverless application (also called serverless computing) is an application that runs on a platform that runs immediately when a program is thrown in, without worrying about the infrastructure of such a server. It's not that there is no server. Will Google's GAE (Google App Engine) be the one?

What is Lambda

Lambda is a PaaS (Platform as a Service) that realizes serverless applications, and is one of the AWS services. It supports languages such as Node.js, Python, Ruby, Java, Go, and C #, and can execute code in response to various triggers. To be precise, in order for the code on Lambda to work as a web application, it needs to work with a service called AWS API Gateway. Therefore, in order to realize serverless applications with Lambda, it is necessary to understand the mechanism of services provided by AWS to some extent. Lambda's pricing is basically just for the code you run. If you don't move it, you won't be charged (the code is stored in S3, so you will be charged), and there is a free tier (1,000,000 requests / month), which I personally recommend Lambda.

What is Zappa?

There are several web application frameworks in Python, one of which is Flask. Flask is considered a lightweight framework with minimal functionality, but Flask is suitable for small applications because it comes standard with a template engine called Jinja2.

And the main subject Zappa is a framework that realizes Flask-based serverless applications using services such as AWS Lambda and API Gateway. Using Zappa has the advantage that you don't have to worry about the description of the part that links Lambda and API Gateway.

So, I would like to say that it is a crispy development with Zappa, but if you do it without knowledge, it will be difficult to improve the environment, so I will list the minimum information and keywords. Try to find out the unknown keywords yourself.

First of all, AWS settings

Since AWS is used, minimum environment settings are required. Here's what to do. If you don't know, look elsewhere. (1) Register (sign up) as a user so that you can use AWS (it's a big premise) (2) Create an account that uses Lambda or API Gateway using IAM. When using AWS services, you need to create an account for it using a management tool called IAM (accessible from the AWS web management screen). After creating it, make a note of the Access Key ID, Secret Access Key, etc. (3) Install aws-cli. AWS service settings are basically done on the web, but this is a group of tools for operating AWS services from the command line. Zappa automates the integration between API Gateway and Lambda, but internally it uses aws-cli. (4) Execute aws configure. This is the default setting for using aws-cli. In addition to entering the Access Key ID and Secret Access Key obtained above, it is necessary to set the region name and output format.

(Supplement) About account role setting in IAM

There is very little information about this, and I may have misunderstood it, but it is necessary to set the authority (role) to access Lambda, API Gateway, etc. for the account created by IAM. If this setting is insufficient, you will get a permission-related error when deploying with zappa. zappa Maybe I'm misunderstanding, but it is necessary to set the authority (role) to access Lambda, API Gateway, etc. for the account created by IAM. If this setting is insufficient, you will get a permission-related error when deploying with zappa. I felt that zappa set it automatically, but for some reason it didn't work, so in that case, if you add the following role for the time being, there will be no error.

With the above, I wonder if AWS is OK. ..

Install Zappa

Before that, zappa seems to work on the premise of virtualenv, so let's create a virtual environment with python and install it in that environment. In my case, I created a virtual environment with Anaconda (GUI), but it was OK even under that environment. The installation of zappa is as follows.

$ pip install flask
$ pip install zappa

Create an initialization file

Create an initialization file with Zappa init

$ zappa init

By executing this command, a file called zappa_settings.js will be created.

Error in zappa init

I will write down what to do when the following error occurs.

"Zappa requires an active virtual environment

```→ virtual with environment variables_Please set env.

```Error: zappa init error'utf8' codec can't decode byte 0x**
```→ This error seems to be just a directory location (or permission?) Problem. Try navigating to the python project file folder (which you are about to create) and then run init again.

#### About the zappa_settings.json file
 The zappa command deploys the project according to the information contained in this file. Modify the file as needed. The point here is that by changing the variable called project_name, it will be treated like another project. In other words, it will be a different Lambda function, and the URL in API Gateway will also change. Also, it seems that the parameter of app_function determines the file of the main program to be executed.


#### **`zappa_settings.json`**
```json

{
    "dev": {
        "app_function": "test.app", 
        "profile_name": "default",← AWS profile
        "project_name": "test-app7", 
        "aws_region": "us-east-1",
        "runtime": "python3.7",
        "apigateway_enabled": true,
        "s3_bucket": "*******"← Please change to an appropriate name
    }
}

Sample code

HTML file to output. Please note that the location of the file is under templete.

template/index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HELLO</title>
</head>
<body>
    <p>Merry Christmas</p>
</body>
</html>

Python code

test.py


from flask import Flask, render_template
app = Flask(__name__)
 
@app.route('/')
def hello():
    html = render_template('index.html')
    return html
 
if __name__ == "__main__":
    app.run()

In addition, it is a boring sample. .. ..

zappa command (deploy, update, delete)

After preparing the above file, the rest of the operation will be as follows.

(When deploying a project for the first time)
$ zappa deploy dev
(When updating)
$ zappa update dev
(When deleting)
$zappa undeploy dev

When you execute it, the URL will appear, so you can execute it by accessing it.

Well, how about? If you take the above steps, all you have to do is implement the app. You can easily publish it, so please try it!

Recommended Posts

The first step to creating a serverless application with Zappa
[Python] The first step to making a game with Pyxel
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Save the object to a file with pickle
AWS Step Functions to learn with a sample
[Introduction to Udemy Python3 + Application] 9. First, print with print
How to create a submenu with the [Blender] plugin
The first step to getting Blender available from Python
Transit to the update screen with the Django a tag
How to convert an array to a dictionary with Python [Application]
Finding a solution to the N-Queen problem with a genetic algorithm (2)
Probably the easiest way to create a pdf with Python3
Python> List> partitions = [0] * len (all_filepaths) / partitions [: test_set_size] = [1] * After creating a list with test_set_size> 0, set the front part to 1.
To write a test in Go, first design the interface
A story about how to deal with the CORS problem
The usual way to add a Kernel with Jupyter Notebook
Road to the Wizard: Step 8 (Interpret How to become a Hacker)
Finding a solution to the N-Queen problem with a genetic algorithm (1)
A story that I had a hard time trying to create an "app that converts images like paintings" with the first web application
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
Let's turn PES analysis software into a WEB application! First step!
Creating a Tensorflow Sequential model with original images added to MNIST
Precautions when creating a two-dimensional array with all the same values
The first step in speeding up inference with TensorFlow 2.X & TensorRT
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
How to make a command to read the configuration file with pyramid
A note I was addicted to when creating a table with SQLAlchemy
I tried to make a 2channel post notification application with Python
I wanted to solve the ABC164 A ~ D problem with Python
Write a script to calculate the distance with Elasticsearch 5 system painless
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
I tried to make a todo application using bottle with python
A script that returns 0, 1 attached to the first Python prime number
The road to web application development is a long way off
How to send a request to the DMM (FANZA) API with python
Create a REST API to operate dynamodb with the Django REST Framework
How to create a serverless machine learning API with AWS Lambda
Road to the Wizard: 4th Step
Step by Step for creating a Dockerfile
Creating a decision tree with scikit-learn
Creating a Flask server with Docker
Web scraping with Python First step
Deploy a Django application with Docker
Creating a simple app with flask
Build a web application with Django
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
The first step in Python Matplotlib
Road to the Wizard: 7th Step
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
I tried to introduce a serverless chatbot linked with Rakuten API to Teams
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Think about how to write a filter with the Shotgun API-Contact Versions
I was addicted to creating a Python venv environment with VS Code
[Django] I made a field to enter the date with 4 digit numbers
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Deb package development with Git: (Part 1) Creating a stray package with the minimum configuration
I tried to discriminate a 6-digit number with a number discrimination application made with python