Skip to content

Latest commit

 

History

History

service-chaining

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Service chaining

In this sample, you will orchestrate multiple Cloud Functions, Cloud Run and external services in a workflow.

Cloud Function - Random number

Inside randomgen folder, deploy a function that generates a random number:

gcloud functions deploy randomgen \
    --gen2 \
    --runtime python39 \
    --trigger-http \
    --entry-point randomgen \
    --source . \
    --allow-unauthenticated

Test:

curl https://us-central1-workflows-atamel.cloudfunctions.net/randomgen

Cloud Function - Multiply

Inside multiply folder, deploy a function that multiplies a given number:

gcloud functions deploy multiply \
    --gen2 \
    --runtime python39 \
    --trigger-http \
    --entry-point multiply \
    --source . \
    --allow-unauthenticated

Test:

curl -X POST https://us-central1-workflows-atamel.cloudfunctions.net/multiply \
    -H "content-type: application/json" \
    -d '{"input":5}'

External Function - MathJS

For an external function, use MathJS.

Test:

curl https://api.mathjs.org/v4/?expr=log(56)

Cloud Run - Floor

Inside floor folder, deploy an authenticated Cloud Run service that floors a number.

Build the container:

export SERVICE_NAME=floor
gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME}

Deploy:

gcloud run deploy ${SERVICE_NAME} \
  --image gcr.io/${PROJECT_ID}/${${SERVICE_NAME}} \
  --platform managed \
  --no-allow-unauthenticated

Test:

curl -X POST https://floor-wvdg6hhtla-ew.a.run.app \
    -H "content-type: application/json" \
    -d '{"input": "6.86"}'

Service account for Workflows

Create a service account for Workflows:

export SERVICE_ACCOUNT=workflows-sa
gcloud iam service-accounts create ${SERVICE_ACCOUNT}

Grant run.invoker role to the service account:

export PROJECT_ID=$(gcloud config get-value project)
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member "serviceAccount:${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role "roles/run.invoker"

Workflow

Deploy workflow:

gcloud workflows deploy workflow \
    --source=workflow.yaml \
    --service-account=${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com

Execute workflow:

gcloud workflows execute workflow

This is not an official Google product.