[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)

Introduction

For those who touch Flask, we have summarized how to create, publish, and delete ultra-simple web applications using Cloud Functions.

procedure

1. Launch Cloud Shell

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.

image.png

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

2. File structure

Create with the following file structure in Cloud Shell.

root/
|--main.py
|--templates/
      |--index.html

3. Create main.py

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)

4. Create index.html

Create ʻindex.html`.

<html>
    <head>
    </head>
    <body>
        hello
    </body>
</html>

5. Deploy

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

6. Display of published web application

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".

7. Delete the published web app

Go to https://console.cloud.google.com and click Cloud Functions.

image.png

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.

image.png

This completes the deletion.

Recommended Posts

[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Procedure for creating a LineBot made with Python
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Creating a web application using Flask ②
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
[Python] A quick web application with Bottle!
Run a Python web application with Docker
Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Launch a web server with Python and Flask
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
[ES Lab] I tried to develop a WEB application with Python and Flask ②
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Launch a Python web application with Nginx + Gunicorn with Docker
Hobby Web engineer develops web application with Vue.js + Flask (& GCP)
Web application development with Flask
Python x Flask x Tensorflow.Keras Web application for predicting cat breeds 2
I made a simple book application with python + Flask ~ Introduction ~
Installation procedure for Python and Ansible with a specific version
[GCP] A memorandum when running a Python program on Cloud Functions
(For beginners) Try creating a simple web API with Django
Creating a Flask server with Docker
Creating a voice transcription web application
Creating a simple app with flask
Build a web application with Django
Make Flask a Cloud Native application
Application development with Docker + Python + Flask
If you know Python, you can make a web application with Django
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Build a detonation velocity website with Cloud Run and Python (Flask)
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Creating a simple PowerPoint file with Python
Daemonize a Python web app with Supervisor
[GCP] Operate Google Cloud Storage with Python
Create a simple web app with flask
[Python] Web application design for machine learning
Easy web app with Python + Flask + Heroku
Let's make a web framework with Python! (1)
Create a web service with Docker + Flask
Let's make a web framework with Python! (2)
[For beginners] Try web scraping with Python
I made a WEB application with Django
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
[Streamlit] I hate JavaScript, so I make a web application only with Python
Flask + PyPy I took a speed benchmark with Blueprint for large-scale Web
Explanation of creating an application for displaying images and drawing with Python
I want to make a web application using React and Python flask
Publish a web application for viewing data created with Streamlit on heroku
Implement a simple application with Python full scratch without using a web framework.
Let's make an A to B conversion web application with Flask! From scratch ...
Data acquisition from analytics API with Google API Client for python Part 2 Web application
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask