Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 45 ยท Practice and Review

Practice Exercises with Solutions

Test yourself with Google Cloud scenario exercises on IAM, cost, networking, storage lifecycle and event design, with runnable checkers and solutions.

Reading about Google Cloud is not the same as deciding what to do when a real request lands on your desk. This lesson gives you five short scenarios, one each for IAM, cost, networking, storage and event design. Each one has a question, a hint, a runnable checker and a written solution.

The real console, gcloud and billing need an account, so the commands in the solutions are read-only. The checkers are stand-ins: small JavaScript functions that encode the rule you are practicing so you get instant feedback. They do not call Google, and the prices in them are invented for teaching. Try each exercise first, then open the solution.

Five scenarios

IAM Give the smallest role that works.
Cost Work out what really bills.
Networking Predict which firewall rule wins.
Storage Move and delete by age.

Edit the answer in each editor and run it.

1 / 2

Exercise 1: a safe role for an analyst

Scenario. An analyst must run queries on the dataset sales in the project shop-prod. She must be able to read tables in that dataset only. She must not edit tables, and she must not see other datasets.

Hint. Running a query is a job, and a job belongs to a project. Reading data is a permission on the dataset. Two bindings on two scopes are better than one big role. Revisit IAM and least privilege if needed.

The checker below treats a binding as a role on a scope. Change answer and see whether it passes.

JS Checker: IAM bindings for the analyst

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution. Grant roles/bigquery.jobUser on the project (so she can start queries) and roles/bigquery.dataViewer on the dataset sales only. Granting a basic role like Editor works but lets her change almost everything.

Terminal
gcloud projects add-iam-policy-binding shop-prod \
  --member="user:[email protected]" \
  --role="roles/bigquery.jobUser"

bq add-iam-policy-binding \
  --member="user:[email protected]" \
  --role="roles/bigquery.dataViewer" \
  shop-prod:sales

Exercise 2: what does the dev VM really cost?

Scenario. A developer's VM runs only during work: 10 hours a day, 22 days a month. A teammate says that "stopping it when unused saves everything". The VM has a 100 GB disk. Using the invented rates below, compute the monthly cost when the VM is stopped after work, and when it is left on all month (730 hours).

Invented rates: VM $0.04 per running hour, disk $0.04 per GB-month. Remember what Compute Engine bills while a VM is stopped.

JS Checker: monthly cost of a stopped VM

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution. Running 220 hours costs $8.80 of compute, and the disk adds $4.00 in both cases, so stopping saves compute only: the saving is $20.40, not "everything". To cut the disk cost you must delete the disk or snapshot it first. See cost management for committed use discounts, Spot VMs and right-sizing.

Exercise 3: which firewall rule wins?

Scenario. A VM tagged web receives SSH attempts. Two ingress rules match: an allow rule for tcp:22 from anywhere at priority 1000, and a deny rule for tcp:22 from anywhere at priority 900. Is the connection allowed? Also, how many addresses can you use in a subnet 10.0.1.0/24?

Hint. In VPC firewalls a lower number means higher priority. Google reserves four addresses in each subnet's primary range. Details are in VPC networks.

JS Checker: firewall priority and subnet size

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution. Priority 900 beats 1000, so SSH is denied. HTTPS is allowed, and anything with no matching rule is denied by the implied ingress rule. A /24 has 256 addresses and 252 are usable. If two matching rules share a priority, deny wins.

Exercise 4: lifecycle rules for logs

Scenario. A bucket holds log files. Requirement: move objects to the Nearline storage class after 30 days and delete them after 365 days. Predict what happens to objects aged 10, 45 and 400 days, then check with the engine. See Cloud Storage.

JS Checker: a tiny lifecycle engine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution. A 10-day-old object stays in Standard, a 45-day-old object is in Nearline, and a 400-day-old object is gone. In the real service the rules are a JSON document applied with gcloud storage buckets update gs://my-logs --lifecycle-file=lifecycle.json, and Google evaluates them asynchronously, so changes are not instant to the minute.

Exercise 5: handle duplicate events

Scenario. Uploads publish messages to a Pub/Sub topic and a function processes each one. Pub/Sub guarantees at-least-once delivery, so the same message can arrive twice. Your function charges a customer for each message. How do you avoid charging twice?

Hint. Make the handler idempotent: key the work on something stable, such as the message's business ID, and ignore repeats.

JS Checker: idempotent message handling

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution. The naive handler charges 120; the idempotent one charges 65. In production the "seen" set must live in a shared store (for example a Firestore document or a database row with a unique key), because function instances come and go. Pub/Sub's own deduplication does not replace this.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pair gives an analyst query access to one BigQuery dataset only?

    Jobs run at project level and data access is granted at dataset level, which keeps the grant as small as possible.
  2. 2. A VM is stopped. Which cost remains?

    Stopping ends compute billing, but attached storage keeps billing.
  3. 3. Two ingress firewall rules match: allow at priority 1000, deny at priority 900. What happens?

    Lower numbers are evaluated first, so the deny rule at 900 decides.
  4. 4. Why must an event handler be idempotent?

    At-least-once delivery means duplicates are possible, so repeats must not repeat side effects.

Summary

  • Combine a job-level role with a resource-level data role instead of reaching for a basic role.
  • Stopped VMs still pay for disks; compute the full bill, not just the CPU part.
  • Lower firewall priority numbers win, and a /24 subnet has 252 usable addresses.
  • Lifecycle rules act on object age: change the class first, delete later.
  • Treat every event as possibly duplicated and key your work on a stable ID.

Next: Google Cloud Interview Questions