Terraform_Essentials_Summarized_Notes
Terraform_Essentials_Summarized_Notes
Advantages of IaC:
What is Terraform?
Terraform is an open-source IaC tool developed by HashiCorp that allows users to define and
provision infrastructure using a declarative configuration language called HashiCorp
Configuration Language (HCL). It supports a wide variety of cloud providers, including
AWS, Azure, Google Cloud, and many others.
Features of Terraform:
1. Multi-Cloud Support: Works across multiple cloud providers and on-premises data
centers.
2. State Management: Keeps track of the infrastructure state, enabling drift detection
and safe updates.
3. Resource Graph: Automatically determines the dependency graph of resources,
ensuring proper order of operations.
4. Plan and Apply: The plan feature previews changes before applying, reducing errors
and enhancing control.
5. Modules: Supports reusability of configurations through modules, promoting
organized and modular infrastructure code.
Terraform Variables:
Terraform variables allow dynamic input into configurations, making them flexible and
reusable.
Terraform Providers:
Providers are plugins that enable Terraform to interact with APIs of various cloud platforms,
SaaS providers, and on-premises infrastructure.
1. Examples:
1. AWS Provider: Manages AWS resources.
2. Azure Provider: Manages Azure resources.
3. Google Provider: Manages Google Cloud resources.
2. Functionality: Providers configure resources such as VMs, networking, storage, and
more.
Local values are temporary, named values that can help simplify Terraform configurations by
reducing repeated logic.
Terraform Functions:
1. Examples:
1. concat(): Combines multiple lists.
2. lookup(): Fetches values from maps.
3. length(): Returns the length of a list or string.
4. join(): Joins list items into a single string.
2. Usage: Functions are used within expressions in resources, variables, outputs, and
locals.
Data sources allow you to fetch or reference external data that can be used within your
Terraform configurations without creating or modifying the data.
1. Purpose: Use existing infrastructure data like cloud resources, account information,
or external services.
2. Example:
State Management
Remote Backends
14. Definition: Backends define where and how the Terraform state is stored.
15. Common Remote Backends:
1. AWS S3, Azure Blob Storage, Google Cloud Storage, HashiCorp Consul,
Terraform Cloud, etc.
16. Features:
1. State file locking, encryption, access control, and versioning.
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "path/to/my/statefile.tfstate"
region = "us-west-2"
}
4. Specify the Resource: Identify the resource type and the target address in your
Terraform configuration.
5. Import to State File: The resource is imported into the state file, not directly into the
.tf configuration files.
6. Manual Configuration: After importing, manually define the resource in your .tf
files to maintain consistency between state and configuration.
7. Current State Only: Imports the current state of resources but does not generate
configuration details (arguments and settings).
8. Manual Addition Required: Requires manual addition of the resource configuration
in .tf files post-import to maintain consistency.
9. Data Sources Not Importable: Cannot import data sources or resources that are
purely data-based.
10. Migrating Existing Resources: Bring unmanaged cloud resources under Terraform
management.
11. Consistency in State: Align unmanaged resources with Terraform’s state for
consistent IaC.
12. Rebuilding State: Rebuild Terraform state after accidental state loss or corruption.
terraform plan
Commands Explained
A Terraform module is a container for multiple resources that are used together. It is
essentially a reusable piece of Terraform code that defines a specific set of infrastructure
resources in a modular way, allowing you to package, reuse, and share infrastructure
configurations across different projects.
5. Remote State Management: Stores and locks Terraform state files remotely to
prevent conflicts during concurrent runs.
6. VCS Integration: Integrates with version control systems (e.g., GitHub, GitLab,
Bitbucket) for automated workflows triggered by code changes.
7. Collaborative Workflows: Provides tools for team collaboration, such as
workspaces, to manage environments and roles-based access controls.
8. Policy as Code: Enforces organizational policies through Sentinel, allowing
governance over Terraform operations.
9. Run Management: Automates and manages Terraform runs, including plan, apply,
and cost estimation steps.
10. Private Module Registry: Hosts and shares private modules across teams, enhancing
collaboration.
11. Cost Estimation: Provides cost estimation before applying changes to the
infrastructure, helping teams manage expenses.
12. Notifications and Monitoring: Sends alerts and notifications about the state of
Terraform runs, ensuring transparency and operational awareness.
13. Platform vs. CLI: Terraform Core is the CLI tool used for managing infrastructure as
code locally, while Terraform Cloud is a SaaS offering that centralizes and enhances
these capabilities with team collaboration, state management, and automation.
14. State Management: Terraform Cloud manages state remotely with locking and
permissions, while Terraform Core manages state files locally unless configured to
use remote backends.
15. Automation: Terraform Cloud automates runs, approvals, and integrations with other
services, whereas Terraform Core relies on manual command execution.
16. Team Collaboration: Terraform Cloud focuses on enhancing team workflows, with
role-based access and permissions, while Terraform Core is primarily a local tool with
no built-in collaboration features.
17. Policy Enforcement: Terraform Cloud includes governance tools (Sentinel), which
are not available in Terraform Core, providing additional compliance and security
controls.