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.
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?
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.
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.
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.
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. ..
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 with Zappa init
$ zappa init
By executing this command, a file called zappa_settings.js will be created.
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
}
}
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. .. ..
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