Skip to content

omattsson/simple-tf-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Terraform Module

🤖 AI Generated: This entire Terraform module (code, documentation, examples, and tests) was generated by GitHub Copilot AI to demonstrate best practices.

A simple, well-structured Terraform module demonstrating best practices for module development. This module showcases input variable handling, data processing, local values, conditional resources, and comprehensive outputs.

Features

  • 📝 Input Validation: Variables with validation rules and descriptive error messages
  • 🔄 Data Processing: Combines and transforms input variables using local values
  • 🏷️ Tag Management: Merges user-provided tags with automatically generated ones
  • 🎯 Conditional Resources: Demonstrates conditional resource creation
  • 📤 Comprehensive Outputs: Exposes processed data and metadata
  • 🔍 Example Usage: Includes multiple usage examples

Usage

Basic Example

module "simple_example" {
  source = "path/to/simple-tf-module"
  project_name = "my-project"
  environment  = "dev"
}

Advanced Example

module "advanced_example" {
  source = "path/to/simple-tf-module"
  project_name = "advanced-project"
  environment  = "production"
  string_list = ["database", "cache", "queue", "storage"]
  default_tags = {
    Owner       = "devops-team"
    CostCenter  = "engineering"
    Compliance  = "required"
  }
  enable_random_pet = true
  pet_length        = 3
  pet_separator     = "_"
}

Requirements

Name Version
terraform >= 1.0
random ~> 3.1

Providers

Name Version
random ~> 3.1

Inputs

Name Description Type Default Required
project_name Name of the project. Used for resource naming and tagging. string "CASE NIGHTMARE GREEN" no
environment Environment name (e.g., dev, staging, prod). Used for resource naming and tagging. string n/a yes
string_list List of strings to be processed and combined in outputs. list(string) ["item1", "item2", "item3"] no
default_tags Default tags to apply to all resources. Will be merged with automatically generated tags. map(string) { "Terraform": "true", "Module": "simple-tf-module" } no
enable_random_pet Whether to create a random pet resource. Demonstrates conditional resource creation. bool true no
pet_length Number of words in the random pet name when enabled. number 2 no
pet_separator Separator to use between words in the random pet name. string "-" no
prefix Prefix for resource naming. Should be lowercase and hyphenated. string n/a yes
resource_group_name The name of the resource group where resources will be created. string "default-resource-group" no

Outputs

Name Description
full_name Combined project name and environment
resource_prefix Standardized prefix for resource naming (lowercase, hyphenated)
common_tags Merged tags combining user inputs with automatically generated tags
processed_items String list processed and combined into a comma-separated uppercase string
random_id Generated random ID (hex format)
random_pet Generated random pet name (only available when enabled)
pet_based_resource_name Example resource name combining project prefix with random pet
pet_with_timestamp Random pet name with timestamp for unique identification
pet_uppercase Random pet name converted to uppercase for naming conventions
pet_metadata Detailed information about the random pet resource
module_info Information about this module instance
prefix The prefix used for resource naming.
resource_group_name The name of the resource group where resources will be created.

Resources

Name Type
random_id.example resource
random_pet.conditional resource

Examples

See the examples/ directory for comprehensive usage examples including:

  • Basic usage with minimal configuration
  • Advanced usage with custom tags and settings
  • Minimal configuration with disabled features

Best Practices Demonstrated

  1. Variable Validation: All input variables include validation rules
  2. Descriptive Documentation: Comprehensive descriptions for all components
  3. Consistent Naming: Standardized resource naming conventions
  4. Tag Management: Automatic tag generation and merging
  5. Conditional Logic: Conditional resource creation based on input
  6. Local Values: Data processing and transformation using locals
  7. Version Constraints: Explicit Terraform and provider version requirements
  8. Output Organization: Well-structured outputs with clear descriptions

Development

To test this module locally:

  1. Clone the repository
  2. Navigate to the examples directory: cd examples
  3. Run terraform init to initialize the module
  4. Run terraform plan to see the planned changes
  5. Run terraform apply to create the resources

Alternatively, you can reference the module from your own Terraform configuration by pointing to the root directory.

Running Tests

This module includes comprehensive test suites using Terraform's native testing framework:

# Run all tests
terraform test

# Run specific test file
terraform test -filter=tests/basic_module_test.tftest.hcl

# Run with verbose output
terraform test -verbose

See the tests/ directory for detailed test documentation and test cases covering input validation, output verification, and various configuration scenarios.

Contributing

  1. Follow the existing code structure and naming conventions
  2. Add validation rules for new variables
  3. Update documentation for any changes
  4. Include examples for new features

License

This project is licensed under the terms specified in the LICENSE file.

About

A simple Terraform module to use when testing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors