Google Cloud Functions Operators

Prerequisite Tasks

CloudFunctionDeleteFunctionOperator

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

For parameter definition, take a look at CloudFunctionDeleteFunctionOperator.

Using the operator

airflow/providers/google/cloud/example_dags/example_functions.py[source]

delete_task = CloudFunctionDeleteFunctionOperator(task_id="gcf_delete_task", 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:

airflow/providers/google/cloud/example_dags/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:

airflow/providers/google/cloud/example_dags/example_functions.py[source]

body = {"name": FUNCTION_NAME, "entryPoint": GCF_ENTRYPOINT, "runtime": GCF_RUNTIME, "httpsTrigger": {}}

airflow/providers/google/cloud/example_dags/example_functions.py[source]

if GCF_SOURCE_ARCHIVE_URL:
    body['sourceArchiveUrl'] = GCF_SOURCE_ARCHIVE_URL
elif GCF_SOURCE_REPOSITORY:
    body['sourceRepository'] = {'url': GCF_SOURCE_REPOSITORY}
elif GCF_ZIP_PATH:
    body['sourceUploadUrl'] = ''
    default_args['zip_path'] = GCF_ZIP_PATH
elif GCF_SOURCE_UPLOAD_URL:
    body['sourceUploadUrl'] = GCF_SOURCE_UPLOAD_URL
else:
    raise Exception("Please provide one of the source_code parameters")

The code to create the operator:

airflow/providers/google/cloud/example_dags/example_functions.py[source]

deploy_task = CloudFunctionDeployFunctionOperator(
    task_id="gcf_deploy_task",
    project_id=GCP_PROJECT_ID,
    location=GCP_LOCATION,
    body=body,
    validate_body=GCP_VALIDATE_BODY,
)

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

airflow/providers/google/cloud/example_dags/example_functions.py[source]

deploy2_task = CloudFunctionDeployFunctionOperator(
    task_id="gcf_deploy2_task", location=GCP_LOCATION, body=body, validate_body=GCP_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?