This repository contains a collection of Terraform modules and testing logic that demonstrate how to provision infrastructure, configure resources using user-input YAML files, and test Terraform's functionality in different scenarios. The modules are designed to cover various use cases, including Kubernetes deployments, cloud resources provisioning, and dynamic configuration using the for_each
loop with YAML inputs.
- Introduction
- Prerequisites
- Directory Structure
- Scenarios Overview
- Testing Modules and Logic
- Running the Tests
- Cleaning Up Resources
- Conclusion
This lab is designed to help you practice and test different aspects of Terraform configuration using real-world scenarios. The focus is on using Terraform to manage infrastructure and configure resources based on user-input YAML files, leveraging the for_each
loop, and automating resource creation dynamically. The tests cover common scenarios encountered in infrastructure management.
Ensure you have the following installed on your machine:
- Terraform
- Kubernetes
- Minikube (if using local Kubernetes)
- Docker (optional for running containers)
terraform-lab/
├── modules/
│ └── kubernetes-pod/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── test/
│ ├── test_scenario_1.md
│ ├── test_scenario_2.md
│ ├── test_scenario_3.md
│ ├── test_scenario_4.md
│ └── test_scenario_5.md
├── terraform.tfvars
├── README.md
└── pods_config.yaml
modules/
: Contains reusable Terraform modules. Thekubernetes-pod
module provisions Kubernetes pods based on user input.test/
: Contains markdown files that describe how to test each scenario, with detailed instructions and expected outcomes.terraform.tfvars
: Variables file to manage input variables for the Terraform configuration.pods_config.yaml
: The YAML file used as input for Kubernetes pod creation.
Each scenario demonstrates a specific use case of the Terraform module. These scenarios use different configurations, testing various parts of the module to ensure that the logic behaves correctly with different inputs.
This scenario tests whether the Terraform module can correctly parse and validate the user-input YAML file.
- Objective: Ensure that valid YAML files are parsed correctly, and invalid files are rejected.
- Key Files:
pods_config.yaml
main.tf
- Expected Outcome: Terraform should validate the input and reject any malformed YAML file with an appropriate error message.
- Instructions: Scenario 1 Detailed Steps
In this scenario, Terraform reads the YAML input file to create Kubernetes pods dynamically.
- Objective: Verify that the module creates resources as described in the YAML file.
- Key Files:
main.tf
pods_config.yaml
- Expected Outcome: Terraform should create Kubernetes pods with the specifications from the YAML file.
- Instructions: Scenario 2 Detailed Steps
This test focuses on ensuring that Terraform is idempotent when updating resources based on changes in the YAML file.
- Objective: Ensure that only changes in the YAML file are applied, without recreating the resources unnecessarily.
- Key Files:
pods_config.yaml
main.tf
- Expected Outcome: Terraform applies only changes, keeping the rest of the resource configuration intact.
- Instructions: Scenario 3 Detailed Steps
This scenario tests how the module handles invalid YAML files, such as those with missing parameters or syntax errors.
- Objective: Ensure Terraform gracefully handles YAML syntax errors and missing required fields.
- Key Files:
invalid_pods_config.yaml
main.tf
- Expected Outcome: Terraform should return an error indicating the issues in the YAML file.
- Instructions: Scenario 4 Detailed Steps
In this scenario, the YAML file is stored in a remote location, such as an S3 bucket or a public URL, and Terraform fetches it to create resources.
- Objective: Validate that the module can read and use remote YAML files to create resources.
- Key Files:
remote_config.yaml
main.tf
- Expected Outcome: Terraform should successfully fetch the YAML file and create the necessary resources.
- Instructions: Scenario 5 Detailed Steps
-
Initialize Terraform:
terraform init
-
Validate the Configuration:
terraform validate
-
Apply the Terraform Plan:
terraform apply
-
Check the Results:
- Use
kubectl
to verify the created pods.
kubectl get pods
- Use
- For more automated testing, you can use Terratest to create Go-based tests for the Terraform modules. Instructions for setting this up are provided in each test scenario.
To clean up all the resources created by the tests, run:
terraform destroy
This will ensure that all Kubernetes pods and any other resources provisioned by the module are removed.
This Terraform lab provides a comprehensive guide to testing modules that read YAML files and dynamically create resources. It allows you to practice real-world scenarios, ensuring that your Terraform logic works as expected in different cases.
Create a .gitignore
file that ignores all .terraform
directories or files across all directories and subdirectories.
# Ignore all .terraform directories and any file or directory that starts with .terraform
**/.terraform*
**/.terraform*
: This will match any.terraform
directory or file that starts with.terraform
(e.g.,.terraform.lock.hcl
) in all directories and their subdirectories.
You can place this .gitignore
file in the root of your repository to ensure Terraform-related local state, provider plugins, and lock files do not get committed to version control.
You might also want to ignore other files like Terraform plan files (*.tfplan
) or auto-generated state files (*.tfstate
) that you don’t want to track:
# Ignore Terraform state files
*.terraform
*.tfstate
*.tfstate.backup
# Ignore Terraform plan files
*.tfplan
# Ignore the .terraform directory and files
**/.terraform*
This ensures you keep your repository clean from temporary or generated Terraform files while still committing essential configuration files (*.tf
, *.tfvars
files).