[PYTHON] Run Google Cloud Functions locally with Cloud Native Build packs

Introduction

Create a local development environment for Google Cloud Functions and build a service that outputs Hello World. Also, the local development environment uses Cloud Native Build packs. Basically, we will follow Google's document. Please take it as a supplement.

Premise

My PC has the following environment.

--Build Ubuntu on Windows 10 using wsl2 --Install docker on Ubuntu --Install python3.8 on windows

This time, we will create a local development environment for Google Cloud Functions on Ubuntu.

It's time to build

We will build by referring to Local Development. According to the material, there are two options for developing Google Cloud Funcitons locally.

--Use Function Frameworks -Use Cloud Native Buildpacks

This time, I would like to use Cloud Native Build packs to build a local environment. In addition, in order to use Cloud Native Build packs, the following is required.

-pack tool --docker → Omitted this time

Therefore, we will proceed according to the following flow.

  1. Install the pack tool
  2. Create a Python Cloud function for testing
  3. Run Cloud Native Build packs

Install the pack tool

Install the pack tool by referring to the Official Document. The pack tool is provided as a Command Line Interface (CLI) and Go library, this time installing the CLI.

Since the environment will be Ubuntu, install it by executing the following command.

$sudo add-apt-repository ppa:cncf-buildpacks/pack-cli
$sudo apt-get update
$sudo apt-get install pack-cli

And add the following to .bash_profile

$echo '. $(pack completion)' >> ~/.bash_profile

Creating a Python Cloud function for testing

Create a Python Cloud function that outputs Hello World for testing. Python Cloud functions are created by referring to Python Quick Start ~~ round copy ~~.

Save the following as hello_world.py.

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'
    ```

###Run Cloud Native Build packs

Cloud Native BuildpacksFollow the steps below.

pack build --builder gcr.io/buildpacks/builder:v1 \
--env GOOGLE_RUNTIME=python \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=hello_world hello

Then, the following error occurs.

=== Python - Functions Framework ([email protected]) ===
Failure: (ID: 5c04ec9c) missing main.py and GOOGLE_FUNCTION_SOURCE not specified. Either create the function in main.py or specify GOOGLE_FUNCTION_SOURCE to point to the file that contains the function

Apparently the name of the Python Cloud function is main.Since it is other than py, the source(hello_world.py)It seems that it has failed to link with. Therefore, GOOGLE is used as an argument when executing the pack command._FUNCTION_Add SOURCE and run again

pack build --builder gcr.io/buildpacks/builder:v1 \
--env GOOGLE_RUNTIME=python \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=hello_world \
--env GOOGLE_FUNCTION_SOURCE=hello_world.py hello

Then the image was successfully built. Try running the container using the created hello image.

$ docker run --rm -p 8080:8080 hello

After execution, use the curl command to access the Python Cloud function and confirm that hello world is returned.

$ curl http://localhost:8080/hello_world
Hello World!

I was able to build a service that outputs Hello World in a local development environment.

#Summary -I was able to create a local development environment using Cloud Native Build packs. -I was able to build a service that outputs Hello World in the local environment.

#reference

Recommended Posts

Run Google Cloud Functions locally with Cloud Native Build packs
Try using Python with Google Cloud Functions
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Emulate GCP Cloud functions locally
Run XGBoost with Cloud Dataflow (Python)
How to connect to Cloud Firestore from Google Cloud Functions with python code
Run the IDCF cloud CLI with Docker
Streaming speech recognition with Google Cloud Speech API
Use Tabpy with Cloud Run (on GKE)
Deploy functions with Cloud Pak for Data
[GCP] Operate Google Cloud Storage with Python
Google Cloud Functions log output (and addicted)
Automatically generate Google Cloud Build status badges
Automatic voice transcription with Google Cloud Speech API
Easily realize microservices with Cloud Run x Flask
Serverless LINE bot made with IBM Cloud Functions