Skip to content

icon picker
Actionable

| |


Exercise 1: Getting Started with Terraform Fundamentals

Objectives:
Install Terraform and set up your working directory.
Write a basic HCL configuration.
Use the Terraform CLI to initialize, plan, apply, and destroy your configuration.
Steps:
Set Up a Directory: Create a directory named terraform-exercise-1.
Create a Basic Configuration: In that directory, create a file named main.tf with the following content:
Run Terraform Commands:
Initialize the project: terraform init
Create an execution plan: terraform plan
Apply the changes: terraform apply
Once finished, clean up: terraform destroy
Key Concepts Covered:
Infrastructure as Code (IaC)
Terraform CLI
HCL (HashiCorp Configuration Language)
Resource
Plan, Apply, and State File (implicitly via Terraform’s management of state)
Dependency Graph (you can view it with terraform graph)

Exercise 2: Introducing Variables, Outputs, and Interpolation

Objectives:
Learn how to parameterize configurations using Variables.
Define Outputs to display important values.
Use Interpolation to dynamically reference values in your configuration.
Steps:
Create Variables File: Create a file named variables.tf:
Modify Main Configuration: Update your main.tf to use the variable:
Define an Output: Create an outputs.tf file:
Run Terraform: Reinitialize (if needed), then run terraform plan and terraform apply to see the variable in action and view the output.
Key Concepts Covered:
Variable
Output
Interpolation

Exercise 3: Configuring Providers and Using Data Sources

Objectives:
Set up provider configurations.
Use two different providers: one for generating randomness and one for fetching external data.
Learn how to declare and utilize Data Sources.
Steps:
Add Provider Blocks: Update (or create) your main.tf with provider configurations. For example, use the and
providers:
Create a Random Resource: Add a resource to generate a unique pet name:
Fetch External Data: Use a data source to fetch content from a public URL (for example, ):
Output the Results: Add outputs to display the generated pet name and a snippet of the fetched content:
Test It Out: Run terraform init, then terraform apply and review the outputs.
Key Concepts Covered:
Provider
Provider Configuration
Data Sources
Resource

Exercise 4: Exploring Execution Workflow and Dependency Management

Objectives:
Understand how Terraform manages execution flow.
Use Interpolation to connect outputs between resources.
Examine the Dependency Graph and state management.
Steps:
Create Interdependent Resources: Update your configuration to add a second resource that depends on the pet name generated:
Visualize the Dependency Graph: Run terraform graph and (if you have Graphviz installed) pipe the output to generate a visual diagram:
Open the generated graph.png to see how Terraform links your resources.
Observe the State File: Notice how Terraform maintains a state file that records current resource states. You can review this file (terraform.tfstate) to see the recorded infrastructure details.
Key Concepts Covered:
Interpolation
Dependency Graph
Plan, Apply, and State File

Exercise 5: Building Modular Configurations with Provisioners

Objectives:
Create a reusable Module.
Use Provisioners within the module for post-creation actions.
Understand how modules encapsulate resources, variables, and outputs.
Steps:
Create a Module Directory: Inside your project, create a new folder modules/simple_module.
Module Files: In modules/simple_module, create a main.tf:
Optionally, add a variables.tf and outputs.tf if you want to parameterize the module and expose values.
Call the Module: In your root configuration (main.tf), add:
Apply and Verify: Run terraform init (which now detects the module), then terraform apply to execute the module. You should see the output from the provisioner in your terminal.
Key Concepts Covered:
Module
Provisioners
Variable & Output (if you expand the module)

Exercise 6: Advanced State Management, Workspaces, and Versioning

Objectives:
Configure a Backend for storing Terraform state remotely (or simulate it locally).
Learn how to manage multiple Workspaces to handle different environments.
Apply Versioning to your providers and modules for stability and consistency.
Steps:
Set Up a Backend: In your main.tf, add (for example, using the local backend to simulate remote state):
If you have access to an S3 bucket or another remote backend, follow to configure it.
Specify Version Constraints: In your terraform block, you might have already specified provider versions. Double-check and adjust as needed:
Workspaces for Environment Isolation: Create a new workspace (e.g., for staging):
Switch between workspaces:
Experiment by applying different variable values or configurations in each workspace to simulate separate environments.
Test and Observe: Run terraform apply in each workspace. Notice that Terraform maintains separate state files (or separate state sections) per workspace.
Key Concepts Covered:
Backend
Remote State
Workspace
Versioning
Provider Configuration


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.