📖 Scenario: You are building a simple cloud application that needs to keep track of user session states. To do this, you will use Google Cloud Storage to save and retrieve session data as JSON files.
🎯 Goal: Create a Google Cloud Storage bucket and write Python code to save a session state dictionary as a JSON file, then read it back to verify the state is managed correctly.
📋 What You'll Learn
Create a dictionary called session_state with exact keys and values
Create a variable called bucket_name with the exact bucket name string
Write code to upload session_state as JSON to the bucket using blob.upload_from_string()
Write code to download the JSON file from the bucket and load it back into a dictionary called loaded_state
💡 Why This Matters
🌍 Real World
Many cloud applications need to save user session data or application state between runs. Using cloud storage to save JSON files is a simple way to manage state without a database.
💼 Career
Cloud engineers and developers often manage state in distributed systems. Knowing how to store and retrieve state data in cloud storage is a foundational skill for building scalable cloud applications.
Progress0 / 4 steps
1
Create the session state dictionary
Create a dictionary called session_state with these exact key-value pairs: 'user_id': 'abc123', 'is_logged_in': true, 'last_page': 'home'.
GCP
Hint
Use curly braces to create a dictionary and include the exact keys and values.
2
Set the Google Cloud Storage bucket name
Create a variable called bucket_name and set it to the string 'my-session-bucket'.
GCP
Hint
Assign the exact string to the variable bucket_name.
3
Upload the session state as JSON to the bucket
Import storage from google.cloud. Create a storage.Client() instance called client. Get the bucket using client.get_bucket(bucket_name) and assign it to bucket. Create a blob named 'session.json' from bucket.blob(). Upload the JSON string of session_state using blob.upload_from_string() with content_type='application/json'. Use json.dumps() to convert the dictionary to a JSON string.
GCP
Hint
Use the Google Cloud Storage client library to upload the JSON string to the bucket.
4
Download and load the session state JSON
Create a new blob object from bucket.blob('session.json'). Download the JSON string using blob.download_as_text() and assign it to json_data. Use json.loads(json_data) to convert it back to a dictionary and assign it to loaded_state.
GCP
Hint
Download the JSON text from the blob and convert it back to a dictionary.
Practice
(1/5)
1. What is the main purpose of state management in cloud infrastructure?
easy
A. To reduce the cost of cloud services
B. To increase the speed of cloud resource creation
C. To automatically fix errors in cloud resources
D. To keep track of the current setup of cloud resources
Solution
Step 1: Understand what state management means
State management records the current status and configuration of cloud resources.
Step 2: Identify the main goal of state management
Its main goal is to keep track of resources so changes can be managed safely and consistently.
Final Answer:
To keep track of the current setup of cloud resources -> Option D
Quick Check:
State management = tracking resource setup [OK]
Hint: State management means tracking resource setup [OK]
Common Mistakes:
Thinking it speeds up resource creation
Believing it fixes errors automatically
Assuming it reduces cloud costs directly
2. Which of the following is the correct way to configure a remote backend in Terraform using Google Cloud Storage?
What happens when two team members run terraform apply at the same time?
medium
A. Terraform will lock the state file to prevent simultaneous changes
B. Terraform will merge both changes automatically without conflicts
C. Terraform will overwrite the state file causing loss of data
D. Terraform will create duplicate resources in Google Cloud Storage
Solution
Step 1: Understand remote backend behavior with GCS
Terraform uses state locking with remote backends like GCS to prevent concurrent changes.
Step 2: Identify what happens on simultaneous apply
When two users run apply simultaneously, Terraform locks the state file so only one can proceed at a time.
Final Answer:
Terraform will lock the state file to prevent simultaneous changes -> Option A
Quick Check:
Remote backend locks state to avoid conflicts [OK]
Hint: Remote backends lock state to avoid conflicts [OK]
Common Mistakes:
Thinking Terraform merges changes automatically
Assuming state file is overwritten without warning
Believing duplicate resources are created automatically
4. You configured Terraform with a remote backend on GCS, but when running terraform apply, you get an error saying the state file is locked indefinitely. What is the best way to fix this?
medium
A. Delete the state file from the GCS bucket manually
B. Run terraform force-unlock with the lock ID
C. Change the backend configuration to local state
D. Restart your computer and try again
Solution
Step 1: Understand the cause of indefinite lock
State file locks can remain if a previous operation was interrupted or crashed.
Step 2: Use Terraform's force-unlock command
The correct fix is to run terraform force-unlock with the lock ID to safely remove the lock.
Final Answer:
Run terraform force-unlock with the lock ID -> Option B
Quick Check:
Use force-unlock to clear stuck state locks [OK]
Hint: Use terraform force-unlock to clear stuck locks [OK]
Common Mistakes:
Deleting state file manually causing data loss
Switching to local state without fixing lock
Restarting computer does not clear remote locks
5. Your team wants to manage multiple environments (dev, staging, prod) using Terraform with GCS backend. Which approach best ensures isolated state files and safe collaboration?
hard
A. Use one GCS bucket with different prefixes for each environment
B. Use separate GCS buckets for each environment with the same prefix
C. Use local state files stored on each developer's machine
D. Use one GCS bucket and store all environments in the same prefix
Solution
Step 1: Understand environment isolation needs
Each environment should have its own state to avoid conflicts and accidental changes.
Step 2: Choose backend configuration for isolation and collaboration
Using one bucket with different prefixes (folders) isolates states while keeping management simple and collaborative.
Final Answer:
Use one GCS bucket with different prefixes for each environment -> Option A
Quick Check:
Different prefixes isolate environments in one bucket [OK]
Hint: Use prefixes in one bucket to separate environments [OK]
Common Mistakes:
Using same prefix causes state conflicts
Using local state prevents team collaboration
Using multiple buckets complicates management unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.