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
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 (