Prepare and test your skills
Prepare and test your skills
CI/CD pipeline integration and secure secret retrieval is the practice of running custom automation scripts as versioned build steps that bootstrap environments, pull credentials at runtime, and validate results. In Google Cloud, Cloud Build (opens in a new tab) executes the pipeline, Artifact Registry stores versioned outputs, Cloud Deploy promotes releases through environments, and Secret Manager supplies operational credentials without embedding them in code.
Cloud Build is the service that executes builds on Google Cloud to your specifications. Source code flows from Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket into Cloud Build, and built artifacts such as Docker containers or Java archives flow from Cloud Build to Artifact Registry or runtime environments. Custom Python and Go scripts run inside those build steps to call Google Cloud APIs, orchestrate resource provisioning with Terraform, and automate operational workflows.
Bootstrapping a new organization uses the same pattern with modular Terraform code. The bootstrap Terraform module bootstraps Terraform usage and related CI/CD in a new Google Cloud organization, while Cluster Toolkit provides Google-curated Terraform blueprints for ready-to-use HPC, AI, and ML clusters that teams customize and keep in version control. Repository automation depends on a cloudbuild.yaml orchestration file, an imagebuilder.yaml recipe file where OS images are built, and any referenced build scripts, plus a connection to GitHub, GitLab, or Bitbucket through Cloud Build repositories (2nd gen) or Developer Connect connection links. The identity that creates repository event triggers needs the Cloud Build Editor role (roles/cloudbuild.builds.editor) and the Service Account User role (roles/iam.serviceAccountUser), and local Terraform use requires Terraform CLI version 1.3 or later.
Artifact storage and progressive delivery separates building software once from releasing it safely many times. Artifact Registry provides a single location for managing packages and Docker container images and integrates with CI/CD tools and Google Cloud runtime environments so teams can manage the full artifact lifecycle. Cloud Deploy is a managed service that automates delivery of applications to a series of target environments in a defined promotion sequence, with containerized workloads targeting Google Kubernetes Engine (GKE) and serverless workloads targeting the App Engine flexible environment, Cloud Run functions, and Cloud Run.
The three delivery services divide work by stage in the pipeline:
| Service | Input it receives | Output it produces |
|---|---|---|
| Cloud Build | Source from Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket plus build specifications | Built artifacts such as Docker containers or Java archives |
| Artifact Registry | Built packages and container images from CI/CD tools | Versioned, scannable artifacts consumed by runtimes |
| Cloud Deploy | Versioned artifacts and release definitions | Promoted releases across target environments in a defined sequence |
A build therefore flows from repository source to Cloud Build to Artifact Registry, and only then does Cloud Deploy promote that stored artifact through dev, staging, and prod targets.
Secret Manager is the service for creating and managing operational credentials as centrally controlled secrets. The secret-manager Terraform module creates one or more Google Secret Manager secrets and manages basic permissions for them, so access is granted through Identity and Access Management (IAM) rather than through values checked into repositories. During a pipeline run, the Cloud Build service account retrieves the secret value from Secret Manager at execution time and passes it only to the step that needs it, which keeps database passwords, API keys, and deployment tokens out of source code, build logs, and container layers.
This model creates a clear trust boundary between code and credentials. Developers own the script logic in version control, while Secret Manager owns the sensitive values and IAM owns who can read them. A build step fails closed when its service account lacks permission on the secret, so missing access is detected as a pipeline error rather than silently using a default credential.
Validation testing on deployed infrastructure proves that infrastructure code and deployed systems behave as expected before promotion. The pipeline first runs static checks such as Terraform validate and plan from an automated script, then stores the tested configuration as a versioned artifact so the exact tested input is what gets deployed. Separate dev, staging, and prod configurations are kept in version control with automated environment promotion, so differences between environments are explicit and reviewable.
Data and image validation extend the same check-then-promote pattern after deployment:
validate and plan.The Data Validation Tool, an open source Python command-line tool based on the Ibis framework, compares heterogeneous data source tables with multi-leveled validation functions for warehouse, database, or lake migrations. For container images (opens in a new tab), Artifact Registry works with container scanning to surface vulnerabilities, and automated rebuild pipelines pull in base-image security updates and patches before republishing custom images.
Repository-triggered and scheduled rebuilds keep bootstrapped environments current without manual rebuilds. A push to a branch such as main triggers Cloud Build to immediately validate, build, and publish custom OS images (opens in a new tab) to Compute Engine and Artifact Registry upon code review or merge. A separate schedule triggers the same build definition on a regular basis to synchronize base-image updates even when no configuration changed.
Building that automated image pipeline follows an ordered sequence:
Exam tip: Use a repository push trigger to validate and publish immediately after a merge to main, and use Cloud Scheduler to rebuild the same image on a schedule to pull in base-image fixes when no code changed.
Programmatic resource management via GCP client libraries is the practice of using language-specific libraries in Python and Go to call Google Cloud APIs for provisioning and operating resources. The script runs either on a local workstation or on a Google Cloud compute resource, and Application Default Credentials (ADC) (opens in a new tab) supply the identity that the API request carries. Authentication flows from a user credential or attached service account through ADC into the client library, and then from the client library to the target Google Cloud API.
Installing client libraries (opens in a new tab) for Python and Go gives scripts direct access to Google Cloud APIs without hand-building REST requests. For Go, installation uses go get cloud.google.com/go/language/apiv1, and for Python it uses pip install --upgrade google-cloud-language after preparing the Python development environment. Before the library can succeed, the target project must have billing verified and the relevant API enabled with gcloud services enable, such as language.googleapis.com. Enabling APIs requires the serviceusage.services.enable permission, granted through the Owner role (roles/owner) or the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Local development authentication uses ADC to let Python and Go client libraries act with developer or impersonated credentials. The developer creates local ADC with gcloud auth application-default login, which stores credentials that the client library discovers automatically at runtime. If the command warns about a missing serviceusage.services.use permission, the administrator must grant the Service Usage Consumer role (roles/serviceusage.serviceUsageConsumer) and then set the billing context with gcloud auth application-default set-quota-project PROJECT_ID. Local ADC credentials are separate from gcloud CLI credentials, so a REST call that uses gcloud auth print-access-token in an Authorization: Bearer header does not change what the client library will use.
Service account impersonation lets a developer run code locally while testing the exact permissions of a service account. Impersonation requires the iam.serviceAccounts.getAccessToken permission, which is included in the Service Account Token Creator role (roles/iam.serviceAccountTokenCreator). The gcloud CLI can impersonate for its own calls with gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL, while client libraries use an impersonated ADC file created by gcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL. That ADC-file approach is supported only for the Go, Java, Node.js, and Python client libraries and is not supported for other languages.
Exam tip: An impersonated ADC file for client libraries works only for Go, Java, Node.js, and Python; use the gcloud config impersonation setting when you only need the gcloud CLI itself to impersonate.
Workload authentication on Google Cloud uses the credentials of the service account attached to the compute resource where the code runs, such as a Compute Engine virtual machine (VM) instance. This attached-service-account model is the preferred authentication method for code running on a Google Cloud compute resource because no local keys are stored or distributed. The trust boundary sits between the compute resource and the Google Cloud API: the control plane provides the attached identity to code inside the resource, and the API trusts requests that carry it. For most services the service account must be attached when the hosting resource is created and cannot be added or replaced later, while Compute Engine is the exception that allows a service account to be attached to a VM instance at any time.
Provisioning service accounts for automation creates a dedicated machine identity, grants it least-privilege access to resources, and attaches it to the resource that runs the script. The automation workflow depends on the service account, the IAM policy binding that grants access, and the compute resource that presents the identity. Setup follows an ordered sequence where each step depends on the previous identity configuration:
gcloud init to select the project that will own the service account.gcloud iam service-accounts create SERVICE_ACCOUNT_NAME, which requires the Create Service Accounts role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin).gcloud projects add-iam-policy-binding PROJECT_ID, where the binding flows from the role to the serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com member.roles/iam.serviceAccountUser) on the service account with gcloud iam service-accounts add-iam-policy-binding, so that principal is allowed to attach the service account to other resources.--zone and --service-account set to the service account email.Creating a project for this work requires the Project Creator role (roles/resourcemanager.projectCreator) with resourcemanager.projects.create, while selecting an existing project requires only a role already granted on that project. In production, the service account must receive a predefined role or custom role that meets its needs and must not receive the basic Owner, Editor, or Viewer roles.
Choosing where code authenticates determines which credential the Python or Go client library presents to Google Cloud APIs. Local ADC is suited for development and permission testing, while an attached service account is suited for unattended workloads running inside Google Cloud.
| Location | Credential source | Setup mechanism |
|---|---|---|
| Local workstation | User credential or impersonated service account loaded through ADC | gcloud auth application-default login, with --impersonate-service-account when testing a service account |
| Workload on Google Cloud | Service account attached to the hosting compute resource | Attach the service account when the resource is created, except Compute Engine which also allows later attachment |
Traffic flows from the script to the client library to the Google Cloud API in both cases, but the source of trust differs across the boundary. Local runs depend on a file created on the workstation, so access ends when that file is revoked or replaced. Cloud runs depend on the attachment between the compute resource and the service account, so access follows the lifecycle of the hosting resource and its IAM bindings.
Event-driven automation and infrastructure remediation is the practice of running small, reactive scripts that detect unwanted changes and fix them without human action. In Google Cloud, Cloud Functions (opens in a new tab) or Cloud Run (opens in a new tab) services written in languages such as Python and Go subscribe to signals from Pub/Sub or Cloud Logging events, evaluate the event against the desired state, and then call Google Cloud APIs to reprovision resources or enforce compliance.
How detection flows from logs to remediation code is an ordered pipeline that moves an API activity from observation to automated response. The pipeline depends on Cloud Logging as the source of truth, Pub/Sub as the delivery bus, and serverless compute as the actor, so a break in any link stops remediation.
First the log must be routed, then Pub/Sub must deliver it, and only after delivery can the script act. Because audit logs flow from the control plane to the sink to the topic to the compute target, filtering at the sink controls which drifts trigger work and limits cost and noise.
Where remediation code runs is on serverless platforms that start on demand, scale instantly with event volume, and can scale down to zero resources when idle. Both Cloud Functions (2nd gen) and Cloud Run support event handlers written in Python, Go, Java, Node.js, and other runtimes, and both can process Pub/Sub messages and Cloud Audit Log events.
| Option | Primary unit | Choose when |
|---|---|---|
| Cloud Functions | Single-purpose function triggered by an event | The task is a lightweight handler that parses one log or message and calls one or two APIs, such as processing audit logs or publishing to Pub/Sub |
| Cloud Run | Containerized service or job deployed from Artifact Registry | The task needs custom dependencies, longer processing, or an HTTP push handler, such as an image-processing controller that receives Cloud Storage events via Pub/Sub and calls Vision API |
Unlike Compute Engine autoscaling that requires fine-tuned rules, serverless autoscaling on both platforms is instant during failover or event bursts. Choose Cloud Functions when the remediation is a short script tied directly to a trigger, and choose Cloud Run when the remediation needs a container image, broader language packaging, or more control over concurrency and resources.
Remediation scripts in Python and Go are imperative programs that programmatically interact with Google Cloud APIs to orchestrate provisioning and operational workflows. A script receives the event payload, extracts the method name, resource name, and initiator email from the audit log entry, compares the actual configuration to policy, and then executes the fix through an API call.
Remediation handlers typically combine several focused actions:
The script depends on the connector and API it calls, and the workflow depends on safe retry behavior to avoid loops. Retry logic tells the function whether to retry after an error, while infinite-retry protection discards events older than 10 seconds so a poison message cannot retrigger remediation forever.
Exam tip: Discard events older than 10 seconds to stop infinite retries; use explicit retry-on-error only when the remediation API call is safe to repeat.
How infrastructure as code (IaC) and policy as code keep remediation safe is by giving event-driven scripts a declared baseline to restore and guardrails to obey. Declarative IaC with tools such as Terraform, Config Controller, and Cloud Build creates repeatable infrastructure and a known good state, which improves rollback, audit, and troubleshooting compared to imperative scripting alone. The event-driven script therefore restores drift toward the IaC-defined state rather than inventing a new configuration.
When IaC is combined with continuous integration and continuous delivery (CI/CD) pipelines, teams can adopt policy as code with Open Policy Agent (OPA) to run automatic checks on infrastructure code before changes deploy and to audit changes over time. At runtime, Eventarc or Workflows automatically trigger remedial actions in response to issues observed through audit logs, autoscaling adds capacity during failover, and organization controls such as Organization Policy Service, Identity and Access Management (IAM), and Virtual Private Cloud (VPC) Service Controls limit what both humans and remediation identities can change.
Cloud Build executes versioned build steps that turn source from Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket into built artifacts such as Docker containers or Java archives. Artifact Registry stores those built packages and container images as versioned, scannable artifacts consumed by runtimes. Cloud Deploy then promotes those versioned artifacts through target environments in a defined promotion sequence.
Secret Manager creates and manages operational credentials as centrally controlled secrets with access granted through Identity and Access Management (IAM) rather than values checked into repositories. During a pipeline run, the Cloud Build service account retrieves the secret value at execution time and passes it only to the step that needs it, which keeps database passwords, API keys, and deployment tokens out of source code, build logs, and container layers. A build step fails closed when its service account lacks permission, so missing access appears as a pipeline error.
Use Cloud Functions for lightweight single-purpose handlers that parse one log or message and call one or two APIs, such as processing audit logs or publishing to Pub/Sub. Use Cloud Run when the remediation needs a container image from Artifact Registry, custom dependencies, longer processing, or an HTTP push handler, such as an image-processing controller that receives Cloud Storage events via Pub/Sub. Both scale instantly with event volume and can scale down to zero resources when idle.
On a local workstation, client libraries discover Application Default Credentials (ADC) created on the workstation, including impersonated service account credentials when testing permissions. On Google Cloud, they use the credentials of the service account attached to the hosting compute resource, which is the preferred method because no local keys are stored or distributed. In both cases traffic flows from the script to the client library to the Google Cloud API, but local access depends on a workstation file while cloud access follows the hosting resource and its Identity and Access Management (IAM) bindings.
Your organization requires real-time automated remediation of infrastructure configuration drift across its Google Cloud projects. Specifically, whenever a storage bucket or resource IAM policy is modified to grant public access, an automated Python remediation script must execute immediately to evaluate the change and revert non-compliant IAM bindings.
Which architecture should you implement to deploy and trigger this event-driven remediation script with minimal operational overhead and latency?
Last updated