Catalog / Terraform Cheatsheet
Terraform Cheatsheet
A quick reference guide for Terraform, covering basic commands, resource definitions, modules, and best practices for infrastructure as code.
Terraform Basics
Core Concepts
Infrastructure as Code (IaC): Managing and provisioning infrastructure through code rather than manual processes. |
Declarative Configuration: Defining the desired state of the infrastructure, and Terraform figures out how to achieve it. |
State Management: Terraform tracks the state of your infrastructure to understand what resources it manages and how they relate to each other. |
Providers: Plugins that allow Terraform to interact with various cloud providers (AWS, Azure, GCP) and other services. |
Essential Commands
|
Initializes a Terraform working directory. Downloads providers and modules. |
|
Creates an execution plan, showing the changes Terraform will make. |
|
Applies the changes required to reach the desired state of the configuration. |
|
Destroys all resources managed by the Terraform configuration. |
|
Inspect the current state. |
|
Show output values from the state. |
Configuration Files
Terraform configuration files are written in HashiCorp Configuration Language (HCL) or JSON. |
Files typically have a |
A basic configuration includes |
Resources and Providers
Resource Definition
A |
|
Attributes within the resource block configure the resource (e.g., |
Provider Configuration
The |
|
Credentials for the provider can be configured through environment variables, or through the |
Data Sources
Data sources allow Terraform to fetch information about existing resources. |
|
Use data sources to dynamically retrieve values needed for resource configuration. |
Modules and Variables
Module Definition
Modules are reusable Terraform configurations that encapsulate a set of resources. |
Modules improve code organization and reusability. |
|
Input Variables
Variables allow you to parameterize your Terraform configurations. |
|
Variables can be defined in |
Output Values
Outputs expose values from your Terraform configuration, making them accessible to other configurations or users. |
|
Outputs are displayed after a successful |
State Management and Best Practices
State Storage
Terraform state should be stored remotely for collaboration and consistency. |
Common remote state backends include AWS S3, Azure Storage Account, and HashiCorp Terraform Cloud. |
|
Terraform Cloud
HashiCorp Terraform Cloud provides collaboration, version control, and remote state management. |
It allows teams to manage infrastructure changes in a controlled and auditable manner. |
Consider using Terraform Cloud for team-based infrastructure management. |
Best Practices
Version Control: Store your Terraform configurations in a version control system like Git. |
Code Reviews: Use code reviews to ensure the quality and correctness of your Terraform configurations. |
Testing: Implement automated testing to validate your infrastructure changes. |
Idempotency: Ensure that running |
Locking: Remote state backends support locking, which prevents concurrent modifications to the state file. |