📖 Scenario: You work as a cloud engineer for a small company. Your team wants to manage cloud resources in a way that is easy, fast, and reliable. You will learn how to use Infrastructure as Code (IaC) to create and manage cloud resources automatically.
🎯 Goal: Build a simple Google Cloud Platform (GCP) configuration using IaC to create a virtual machine instance. This will show why IaC matters by making resource setup repeatable and consistent.
📋 What You'll Learn
Create a dictionary called vm_config with keys name, machine_type, and zone with exact values.
Add a variable called project_id with the exact string value.
Write a function called create_instance_config that returns a dictionary combining project_id and vm_config.
Add a final dictionary called deployment_config that includes the instance configuration under the key resources.
💡 Why This Matters
🌍 Real World
IaC is used by cloud engineers to automate and manage cloud resources reliably without manual clicks.
💼 Career
Understanding IaC is essential for cloud jobs to improve speed, reduce errors, and enable collaboration.
Progress0 / 4 steps
1
Create the VM configuration dictionary
Create a dictionary called vm_config with these exact entries: 'name': 'test-vm', 'machine_type': 'e2-medium', and 'zone': 'us-central1-a'.
GCP
Hint
Use curly braces to create a dictionary with the exact keys and values.
2
Add the project ID variable
Add a variable called project_id and set it to the string 'my-gcp-project'.
GCP
Hint
Assign the exact string to the variable project_id.
3
Write a function to create instance config
Write a function called create_instance_config that returns a dictionary with keys project set to project_id and instance set to vm_config.
GCP
Hint
Define a function that returns a dictionary combining the project ID and VM config.
4
Create the final deployment configuration
Create a dictionary called deployment_config with a key resources set to a list containing the result of calling create_instance_config().
GCP
Hint
Create a dictionary with a list of resources using the function result.
Practice
(1/5)
1. Why is Infrastructure as Code (IaC) important when working with cloud environments like GCP?
easy
A. It requires manual configuration every time.
B. It makes cloud setup faster and repeatable.
C. It only works for small projects.
D. It replaces the need for cloud security.
Solution
Step 1: Understand IaC benefits
IaC automates cloud setup, making it faster and consistent each time.
Step 2: Compare options
Manual setup is slow and error-prone; IaC is designed for repeatability and speed.
Final Answer:
It makes cloud setup faster and repeatable. -> Option B
Quick Check:
IaC speeds up setup = A [OK]
Hint: IaC automates setup for speed and consistency [OK]
Common Mistakes:
Thinking IaC requires manual setup each time
Believing IaC only suits small projects
Confusing IaC with security tools
2. Which of the following is the correct way to define a simple Google Cloud Storage bucket using Terraform (an IaC tool)?
easy
A. resource "google_storage_bucket" "my_bucket" { name = "my-bucket" location = "US" }
B. resource google_storage_bucket my_bucket { name: "my-bucket" location: "US" }
C. resource "google_storage_bucket" my_bucket { name = "my-bucket" location = "US" }
D. resource "google_storage_bucket" "my_bucket" ( name = "my-bucket" location = "US" )
Solution
Step 1: Check Terraform resource syntax
Terraform uses resource "type" "name" { ... } with key = value pairs inside braces.
Step 2: Identify correct syntax
resource "google_storage_bucket" "my_bucket" { name = "my-bucket" location = "US" } matches this syntax exactly; others have wrong punctuation or missing quotes.
Final Answer:
resource "google_storage_bucket" "my_bucket" { name = "my-bucket" location = "US" } -> Option A
Quick Check:
Terraform resource syntax uses quotes and braces = B [OK]
Hint: Terraform resource blocks need quotes and braces [OK]
Common Mistakes:
Omitting quotes around resource type or name
Using colons instead of equals
Using parentheses instead of braces
3. Given this Terraform snippet for a Google Compute Engine instance: