Question

Difficulty: MediumDeploying Serverless Applications with Cloud Run and Cloud Functions

A cloud engineer needs to deploy a Python microservice as a Cloud Functions (2nd gen) function triggered by messages published to a Cloud Pub/Sub topic. The deployment must adhere to least-privilege security by utilizing a dedicated user-managed service account instead of the default compute service account. Arrange the following deployment tasks in the correct chronological order from start to finish.

  1. 1Enable the Cloud Functions, Cloud Build, Eventarc, and Pub/Sub APIs in the GCP project.
  2. 2Create a custom user-managed service account to serve as the runtime identity for the function.
  3. 3Grant the required granular IAM roles to the custom service account for resource access.
  4. 4Run `gcloud functions deploy` specifying `--gen2`, `--trigger-topic`, and `--service-account` flags.
  5. 5Publish a message to the target Cloud Pub/Sub topic using `gcloud pubsub topics publish` to test execution.

Answer

The correct sequence starts with enabling required GCP service APIs, followed by creating the custom service account, binding the necessary IAM roles to that service account, executing the `gcloud functions deploy` command with the Pub/Sub trigger and service account flags, and finally publishing a test message to the Pub/Sub topic to verify execution.
The correct order follows the standard Cloud GCP resource dependency lifecycle: baseline API enablement must precede resource creation. Next, the execution identity (service account) must be created and granted necessary permissions prior to deployment so that the runtime environment is properly secured. The `gcloud functions deploy` command attaches the custom service account and provisions the Eventarc Pub/Sub trigger. Finally, triggering a test Pub/Sub message verifies that the deployed architecture functions end-to-end.

Step-by-Step Solution

1
Enable required project APIs
Cloud Functions, Cloud Build, Eventarc, and Pub/Sub APIs are active in the project.
Resource creation and deployment commands will fail if underlying service APIs are disabled.
2
Create user-managed Service Account
A dedicated identity is created for function runtime execution.
Enforces least-privilege identity instead of relying on default primitive service accounts.
3
Assign IAM roles to the Service Account
The identity receives specific required permissions for downstream service integration.
Permissions must exist before the function attempts to execute code requiring access.
4
Deploy Cloud Function (2nd gen) with flags
The Cloud Function and Eventarc Pub/Sub trigger subscription are provisioned.
`gcloud functions deploy` links the code, trigger topic, and execution identity.
5
Publish test event to Pub/Sub topic
Cloud Function execution is triggered and validated via logs.
End-to-end event pipeline testing can only take place after infrastructure provisioning is complete.

Key Concept

Deploying 2nd gen Cloud Functions with Pub/Sub Event Triggers and Custom Service Accounts
Rate this question