For those who touch Flask, we have summarized how to create, publish, and delete ultra-simple web applications using Cloud Functions.
Go to https://console.cloud.google.com and click the prompt icon (second from the left in the figure below) at the top right of the screen.
After that, Cloud Shell will be displayed. Execute the following command.
gcloud config set project <Project name>
The project name will be added to the prompt.
username@cloudshell:~ (Project name)$
If you are unsure about the project, please see below. https://cloud.google.com/resource-manager/docs/creating-managing-projects?hl=ja
Create with the following file structure in Cloud Shell.
root/
|--main.py
|--templates/
|--index.html
Create main.py
.
Even if I changed the part of port = 80
, I could only connect on port 80.
main.py
from flask import render_template, Flask
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def webapp(request):
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=False, host='0.0.0.0', port=80)
Create ʻindex.html`.
<html>
<head>
</head>
<body>
hello
</body>
</html>
After moving to the same folder as main.py
in Cloud Shell, execute the following command.
After deploy
, enter the name of the function you want to call first in main.py
.
This time, the function name is webapp
, so the command is as follows.
gcloud beta functions deploy webapp --runtime python37 --trigger-http
To specify a region, add --region = asia-northeast1
.
Details of the deploy command are given below. https://cloud.google.com/functions/docs/deploying/filesystem?hl=ja
When the deployment is complete, a message will be displayed in Cloud Shell, which says httpsTrigger
.
The URL of the published web application will be displayed here, so please connect with your browser.
httpsTrigger:
url: https://us-central1-<Project name>.cloudfunctions.net/webapp
Then, in this case, you can see the page displayed as "hello".
Go to https://console.cloud.google.com and click Cloud Functions.
Then, the web application you published earlier will be displayed. Check the check box on the left and click [Delete] at the top of the screen.
This completes the deletion.
Recommended Posts