terraform-infrastructure-as-code
Comprehensive Terraform Infrastructure as Code skill covering resources, modules, state management, workspaces, providers, and advanced patterns for cloud-agnostic infrastructure deployment
By manutej · 405 installs
npx skills add manutej/luxor-claude-marketplace --skill terraform-infrastructure-as-code
Source repository · Upstream listing
Terraform Infrastructure as Code Comprehensive Guide
Table of Contents
1. [Introduction to Terraform]( introduction to terraform)
2. [Core Concepts]( core concepts)
3. [Resources]( resources)
4. [Data Sources]( data sources)
5. [Variables and Outputs]( variables and outputs)
6. [Modules]( modules)
7. [State Management]( state management)
8. [Workspaces]( workspaces)
9. [Provider Configuration]( provider configuration)
10. [Advanced Features]( advanced features)
11. [Dependencies]( dependencies)
12. [Provisioners]( provisioners)
13. [Best Practices]( best practices)
14. [CI/CD Integration]( cicd integration)
Introduction to Terraform
Terraform is an open source Infrastructure as Code (IaC) tool created by HashiCorp that enables you to define and provision infrastructure using a declarative configuration language called HashiCorp Configuration Language (HCL). Terraform manages external resources such as public cloud infrastructure, private cloud infrastructure, network appliances, and software as a service.
Key Benefits
Declarative Configuration : Define what your infrastructure should look like, not how to create it
Cloud Agnostic : Works with multiple cloud providers and services
Version Control : Infrastructure code can be versioned and reviewed
Plan Before Apply : Preview changes before applying them
Resource Graph : Automatically manages dependencies between resources
State Management : Tracks the current state of your infrastructure
Terraform Workflow
Core Concepts
1. Resources
Resources are the most fundamental elements in Terraform. They represent infrastructure objects like virtual machines, networks, databases, or DNS records.
2. Providers
Providers are plugins that allow Terraform to interact with cloud platforms, SaaS providers, and other APIs.
3. State
Terraform stores information about your infrastructure in a state file. This state is used to map real world resources to your configuration and track metadata.
4. Configuration Language (HCL)
HCL is designed to be both human readable and machine friendly, making it ideal for infrastructure configuration.
Resources
Resources are the building blocks of Terraform configurations. Each resource block describes one or more infrastructure objects.
Basic Resource Syntax
Resource Examples
AWS EC2 Instance
AWS VPC
AWS S3 Bucket
AWS Security Group
Resource Lifecycle
Lifecycle options:
create before destroy : Create new resource before destroying the old one
prevent destroy : Prevent accidental destruction of resources
ignore changes : Ignore changes to specified attributes
replace triggered by : Force replacement when specific resources change
Data Sources
Data sources allow Terraform to use information defined outside of Terraform, or defined by another separate Terraform configuration.
Data Source Syntax
Data Source Examples
Common Data Sources
Variables and Outputs
Input Variables
Variables allow you to parameterize your configurations for reusability.
Variable Types
Setting Variables
terraform.tfvars:
Output Values
Outputs make information about your infrastructure available for other configurations or display to users.
Using Outputs
Local Values
Local values assign a name to an expression for reuse within a module.
Modules
Modules are containers for multiple resources that are used together. They enable you to create reusable components.
Module Structure
Creating a Module
modules/vpc/main.tf:
modules/vpc/variables.tf:
modules/vpc/outputs.tf:
Using Modules
Module Sources
Module Versioning
State Management
Terraform state is a critical component that maps your configuration to real world resources.
Local State
By default, Terraform stores state locally in a file named terraform.tfstate .
Remote State
Remote state enables team collaboration and provides better security and reliability.
S3 Backend
Setup DynamoDB for state locking:
Azure Backend
Terraform Cloud Backend
Consul Backend
State Commands
State Locking
State locking prevents concurrent operations that could corrupt your state.
Importing Existing Resources
Configuration needed before import:
State Migration
Workspaces
Workspaces allow you to manage multiple instances of a single configuration.
Workspace Commands
Workspace Workflow
Using Workspace in Configuration
Workspace State Isolation
Each workspace has its own state file:
terraform.tfstate.d/development/terraform.tfstate
terraform.tfstate.d/staging/terraform.tfstate
terraform.tfstate.d/production/terraform.tfstate
Provider Configuration
Providers are plugins that enable Terraform to interact with cloud platforms and other services.
Single Provider
Multiple Provider Instances (Aliases)
Multi Cloud Setup
Provider Configuration in Modules
Module configuration:
Advanced Features
Dynamic Blocks
Dynamic blocks allow you to dynamically construct repeatable nested blocks.
For Each Meta Argument
Count Meta Argument
Conditional Expressions
Functions
Terraform Functions in Practice
Dependencies
Implicit Dependencies
Terraform automatically infers dependencies from resource references.
Explicit Dependencies
Use depends on when dependencies cannot be inferred automatically.
Module Dependencies
Provisioners
Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction.
Local Exec Provisioner
Remote Exec Provisioner
File Provisioner
Provisioner Failure Behavior
Best Practices
1. State Management
Always use remote state for team collaboration
Enable state locking to prevent concurrent modifications
Enable encryption for sensitive data
Regular state backups for disaster recovery
2. Code Organization
3. Variable Management
4. Naming Conventions
5. DRY Principle
6. Security Best Practices
7. Testing
8. Version Control
.gitignore:
9. Module Best Practices
10. Resource Tagging
CI/CD Integration
GitHub Actions
.github/workflows/terraform.yml:
GitLab CI
.gitlab ci.yml:
Jenkins Pipeline
Jenkinsfile:
Azure DevOps
azure pipelines.yml:
Pre commit Hooks
.pre commit config.yaml:
Automated Testing
test/terraform test.go:
Additional Commands and Tools
Terraform Console
Terraform Graph
Terraform Taint
Terraform Providers
Conclusion
This comprehensive guide covers the essential aspects of Terraform Infrastructure as Code, from basic concepts to advanced patterns and CI/CD integration. By following these practices and patterns, you can build maintainable, scalable, and secure infrastructure deployments across any cloud provider.
Key takeaways:
Use modules for reusable components
Implement remote state with locking for team collaboration
Leverage workspaces for environment isolation
Follow security best practices and never hardcode credentials
Integrate Terraform into CI/CD pipelines for automated deployments
Use validation, formatting, and testing tools
Maintain clear documentation and consistent naming conventions
Implement proper tagging strategies for resource management
Terraform enables infrastructure teams to adopt DevOps practices, improve collaboration, reduce manual errors, and accelerate deployment cycles while maintaining full control and visibility over infrastructure changes.