Google Cloud Functions Operators

Prerequisite Tasks

To use these operators, you must do a few things:

CloudFunctionDeleteFunctionOperator

Use the operator to delete a function from Google Cloud Functions.

For parameter definition, take a look at CloudFunctionDeleteFunctionOperator.

Using the operator

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

delete_function = CloudFunctionDeleteFunctionOperator(task_id="delete_function", name=FUNCTION_NAME)

Templating

template_fields: Sequence[str] = (
    "name",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

More information

See Google Cloud Functions API documentation to delete a function.

CloudFunctionDeployFunctionOperator

Use the operator to deploy a function to Google Cloud Functions. If a function with this name already exists, it will be updated.

For parameter definition, take a look at CloudFunctionDeployFunctionOperator.

Arguments

When a DAG is created, the default_args dictionary can be used to pass arguments common with other tasks:

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

default_args: dict[str, Any] = {"retries": 3}

Note that the neither the body nor the default args are complete in the above examples. Depending on the variables set, there might be different variants on how to pass source code related fields. Currently, you can pass either sourceArchiveUrl, sourceRepository or sourceUploadUrl as described in the Cloud Functions API specification.

Additionally, default_args or direct operator args might contain zip_path parameter to run the extra step of uploading the source code before deploying it. In this case, you also need to provide an empty sourceUploadUrl parameter in the body.

Using the operator

Depending on the combination of parameters, the Function’s source code can be obtained from different sources:

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

body = {"name": FUNCTION_NAME, "entryPoint": ENTRYPOINT, "runtime": RUNTIME, "httpsTrigger": {}}

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

if SOURCE_ARCHIVE_URL:
    body["sourceArchiveUrl"] = SOURCE_ARCHIVE_URL
elif SOURCE_REPOSITORY:
    body["sourceRepository"] = {"url": SOURCE_REPOSITORY}
elif ZIP_PATH:
    body["sourceUploadUrl"] = ""
    default_args["zip_path"] = ZIP_PATH
elif SOURCE_UPLOAD_URL:
    body["sourceUploadUrl"] = SOURCE_UPLOAD_URL
else:
    raise Exception("Please provide one of the source_code parameters")

The code to create the operator:

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

deploy_function = CloudFunctionDeployFunctionOperator(
    task_id="deploy_function",
    project_id=PROJECT_ID,
    location=LOCATION,
    body=body,
    validate_body=VALIDATE_BODY,
)

You can also create the operator without project id - project id will be retrieved from the Google Cloud connection used:

tests/system/providers/google/cloud/cloud_functions/example_functions.py[source]

deploy_function_no_project = CloudFunctionDeployFunctionOperator(
    task_id="deploy_function_no_project", location=LOCATION, body=body, validate_body=VALIDATE_BODY
)

Templating

template_fields: Sequence[str] = (
    "body",
    "project_id",
    "location",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

Troubleshooting

If during the deploy you see an error similar to:

“HttpError 403: Missing necessary permission iam.serviceAccounts.actAs for on resource project-name@appspot.gserviceaccount.com. Please grant the roles/iam.serviceAccountUser role.”

it means that your service account does not have the correct Cloud IAM permissions.

  1. Assign your Service Account the Cloud Functions Developer role.

  2. Grant the user the Cloud IAM Service Account User role on the Cloud Functions runtime service account.

The typical way of assigning Cloud IAM permissions with gcloud is shown below. Just replace PROJECT_ID with ID of your Google Cloud project and SERVICE_ACCOUNT_EMAIL with the email ID of your service account.

gcloud iam service-accounts add-iam-policy-binding \
  PROJECT_ID@appspot.gserviceaccount.com \
  --member="serviceAccount:[SERVICE_ACCOUNT_EMAIL]" \
  --role="roles/iam.serviceAccountUser"

You can also do that via the Google Cloud Console.

See Adding the IAM service agent user role to the runtime service for details.

If the source code for your function is in Google Source Repository, make sure that your service account has the Source Repository Viewer role so that the source code can be downloaded if necessary.

More information

See Google Cloud API documentation to create a function.

Reference

For further information, look at:

Was this entry helpful?