Build Python3 + flask environment on GCP Compute Engine

Introduction

I wanted to check the operation of the GCP load balancer, so I decided to set up a web application on Compute Engine. Originally, the purpose was to verify the load balancer, but I thought it would be better to record the work in the process properly, so I decided to write this article this time.


As the title says, "Build a web application in Python3 + flask environment using Compute Engine". If you are new to GCP, it may be helpful, so please read on if you like: thumbs up:


Compute Engine construction

Building a Compute Engine environment is easy, isn't it? A Linux server can be easily set up by clicking the button.

1. Create a VM instance

Create a Compute Engine VM machine from here on the GCP console. Shot 2.png


Here ↓ is a reference example of instance setting. Shot.png

The point is to check "Allow HTTP traffic". Since a web server (flask) is set up, you cannot check the operation from the outside unless you check this. Shot 1.png

After that, press the "Create" button and wait a few minutes to start the VM.


2. Operation check (VM instance startup)

Let's SSH to see if it started properly. Shot 3.png

Yes, it started successfully: smile: Shot 4.png


Preparation of Python3 system

Now that the VM has started up safely, it's time to set up the Python environment.

1. apt update

First, update apt.

sudo apt update
Shot 6.png

2. Check Python version

Python2 series is installed by default in the set up VM. Shot 5.png

Python3 series is also installed in this Debian 9 package. Shot 7.png

However, pip3 is not installed, so you need to prepare it.


3. Install pip3

Enable pip3 with the following command.

sudo apt install python3-pip
Shot 8.png

You can now use pip3. Next, we will install flask with pip3.


flask installation

pip3 install flask

Install flask with pip3. it's simple. Shot 9.png

You can now use flask. Let's check the operation.


Operation check

Let's prepare a Python script that uses flask to check the operation.

1. Python scripting

First, create a file with nano (text editor) from the following command.

nano main.py

When the editor opens, copy and paste the script below.

main.py


from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Flask World!!'

if __name__ == '__main__':
    app.run(debug=False, host='127.0.0.1', port=5000)

It looks like this ↓ Shot 11.png

If you can copy

Exit the nano editor by typing.


2. Flask operation check

Start the script created by the following command.

python3 main.py

It is OK if the screen looks like the one below. flask is working.

Shot 12.png

So far, we have confirmed the operation, but just in case, let's check the response of the web application with the curl command. Make another SSH connection and enter the following command to check the response.

curl 127.0.0.1:5000

Since the port number is 5000, type curl with the above command.

Shot 13.png

The string "Hello world" was responded properly, so you can confirm that the web application is working.


in conclusion

What did you think? Neither task is too difficult, so once you get used to it, you can start Compute Engine and launch the flask web application in minutes! If you like, try it: relaxed:

Recommended Posts

Build Python3 + flask environment on GCP Compute Engine
Build a Python execution environment using GPU with GCP Compute engine
Build Python environment on Windows
Build python environment on windows
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Build a python3 environment on CentOS7
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
Build Python environment with Anaconda on Mac
Build a Python + OpenCV environment on Cloud9
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Build a python environment with ansible on centos6
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
build Python on Ubuntu
Build a Python development environment on Raspberry Pi
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Install Python3 on Mac and build environment [Definitive Edition]
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Build a machine learning Python environment on Mac OS
Build a GVim-based Python development environment on Windows 10 (1) Installation
How to build a Django (python) environment on docker
Build a Python development environment on Mac OS X
Build a Python environment on your Mac using pyenv
Build a Python development environment using pyenv on MacOS
How to build a Python environment on amazon linux 2
Build python3 environment with ubuntu 16.04
Build python environment with direnv
[ev3dev × Python] Build ev3dev development environment
Build a Python environment offline
Build a python machine learning study environment on macOS sierra
Build an environment for machine learning using Python on MacOSX
Build a python data analysis environment on Mac (El Capitan)
Build a python environment on CentOS 7.7 for your home server
docker build python based on alpine
Building a Python environment on Mac
Prepare Python development environment on Ubuntu
Anaconda python environment construction on Windows 10
Build python virtual environment with virtualenv
Building a Python environment on Ubuntu
Kivy + Python3 on OSX environment maintenance
Create a Python environment on Mac (2017/4)
Build Mysql + Python environment with docker
Python environment construction memo on Mac
Build Flask environment with Dockerfile + docker-compose.yml
Python development environment construction on macOS
Set up Python environment on CentOS
Create a python environment on centos
Environment construction of python3.8 on mac
Install Python development environment on Windows 10
Introduce Python 3.5.2 environment on Amazon Linux
OpenCV3 & Python3 environment construction on Ubuntu
[Docker] Build an environment of python (Flask) + GraphQL (graphene) + MySQL (sqlalchemy)
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Play with GCP free frame ② ~ Airflow (on Compute Engine), Cloud Functions ~
Build Python environment on Ubuntu (when pip is not the default)