02 Python 基礎教學10 所有文章

[QuickStart] Python CloudFunction deploy with gcloud sdk

1. Download & Install Cloud SDK

Step 1 – download gcloud to your desktop

Q: How to check for your Mac is 32-bit or 64-bit?

A: Normally, if you have anything newer than 2008 Macbook, you will have  64 bit processor. You can run this on command line:

$ getconf LONG_BIT

>>> 64

Step 2 – run google-cloud-sdk/install.sh

Step 3 – run gcloud init

If you see this on your terminal, it means you install it successfully!

Your Google Cloud SDK is configured and ready to use!

If you see the gcloud not found error msg:

>>> gcloud init              
zsh: command not found: gcloud

then try to restart your config:

>>> source /Users/max/.zshrc

Other gcloud commands

  1. gcloud auth list
    gcloud displays a list of your accounts.
  2. gcloud config list
    gcloud displays the list of your properties.
  3. gcloud project list
    gcloud displays the projects of your properties.
  4. gcloud config set project "project_ID"
    switch to other project.

2. Prepare CloudFuncion folder

Folder structure

.
├── deploy.sh
├── main.py
├── requirements.txt
└── templates
    └── submit.html
  1. main.py
def hello_world(request):
    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 "Hello World!"
  1. deploy.sh
#!/bin/bash
gcloud functions deploy demo --entry-point hello_world --runtime python39 --trigger-http --allow-unauthenticated

Deploy CloudFunction with gcloud SDK

bash deploy.sh

After deployment, you can check the GCP, enter the URL, can see the “Hello World!” response.

References:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *