[A-00089]gcloudでGoogleCloudFunctionsを作成・削除する

・Functionsを作成する

まずはpythonでfunctionsにアップロードするfeatureを作成します。

ファイル名は[main.py]としてください。gcloud経由でfunctionsに登録する際に、main.pyでないと受け付けてくれません。

import functions_framework


@functions_framework.http
def greeting(request):
    
    return "Greeting!" 

Terminalを開き、main.pyがあるディレクトリに移動します。

cd <main.pyへのpath>
pwd
/Users/anonymous/Documents/python_folder/test2_project

次に下記のコマンドを実行します。

gcloud functions deploy <function-name> \
--gen2 \
--runtime=python311 \
--region=<region-name> \
--source=. \
--entry-point=<method-name> \
--trigger-http \
--allow-unauthenticated
(.venv)MacBook-Pro:test2_project$ gcloud functions deploy test-function \
> --gen2 \
> --runtime=python311 \
> --region=asia-northeast1 \
> --source=. \
> --entry-point=greeting \
> --trigger-http \
> --allow-unauthenticated
Preparing function...done.                                                                                           
X Updating function (may take a while)...                                                                            
  ✓ [Build] Logs are available at [https://console.cloud.google.com/cloud-build/builds;region=asia-northeast1/]                                                                
    [Service]                                                                                                        
  . [ArtifactRegistry]                                                                                               
  . [Healthcheck]                                                                                                    
  . [Triggercheck]                                                                                                   
Completed with warnings:                                                                                             
  [WARNING] Cloud Run service projects/xxxxxx/locations/asia-northeast1/services/test-function for the function was not found. The service was redeployed with default values.
You can view your function in the Cloud Console here: https://console.cloud.google.com/functions/details/asia-northeast1/test-function?project=xxxxxxxxxx

次にfunctionのURLを取得します。

gcloud functions describe <function-name> --gen2 --region <region-name> --format="value(serviceConfig.uri)"
(.venv)MacBook-Pro:test2_project$ gcloud functions describe test-function --gen2 --region asia-northeast1 --format="value(serviceConfig.uri)"
https://test-function-pyqz2j74za-an.a.run.app

GCPコンソールからCloudRuns,CloudFunctionsを確認するとそれぞれサービスアカウントが作成されている事が確認できます。

次にFunctionsをcurlで実行します。

curl -m 70 -X POST https://<function-url> \
    -H "Authorization: bearer $(gcloud auth print-identity-token)" \
    -H "Content-Type: application/json" \
    -d '{}'
MacBook-Pro:python_folder$ curl -m 70 -X POST https://test-function-pyqz2j74za-an.a.run.app \
>     -H "Authorization: bearer $(gcloud auth print-identity-token)" \
>     -H "Content-Type: application/json" \
>     -d '{}'
Greeting!

上記の通り動作確認できました。

・Functionsを削除する

下記のコマンドで削除します。

gcloud functions delete <function-name> --gen2 --region <region-name> 
MacBook-Pro:python_folder$ gcloud functions delete test-function --gen2 --region asia-northeast1 
2nd gen function [projects/xxxxxx/locations/asia-northeast1/functions/test-function] will be deleted.

Do you want to continue (Y/n)?  Y

Preparing function...done.                                                                                                                                  
✓ Deleting function...                                                                                                                                      
  ✓ [Artifact Registry]                                                                                                                                     
  ✓ [Service]                                                                                                                                               
Done.                                                                                                                                                       
Deleted [projects/xxxxxx/locations/asia-northeast1/functions/test-function].

GCPコンソールからも削除されている事を確認してください。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*