Terraform Notess
Terraform Notess
Terraform Notess
name
Terraform validate
Terraform Drift
What is Terraform?
It allows you to define and provision infrastructure using a high-level configuration language
called HashiCorp Configuration Language (HCL).
Key Concepts:
Provider: A plugin that interacts with APIs of cloud platforms or services (e.g., AWS, Azure,
Google Cloud).
State: A file that keeps track of the current state of your infrastructure.
Plan: An execution plan that shows what changes Terraform will make to your infrastructure.
Basic Commands:
bash
terraform apply # Apply the changes required to reach the desired state of the configuration
Install Terraform:
Unzip the downloaded file and move the Terraform binary to a directory included in your
system's PATH.
bash
mkdir my-terraform-project
cd my-terraform-project
hcl
# main.tf
provider "aws" {
region = "us-west-2"
bash
terraform init
Providers:
Providers are responsible for managing the lifecycle of resources. They define how Terraform
interacts with cloud services.
hcl
provider "aws" {
region = "us-west-2"
Resources:
hcl
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
Page 4: Variables and Outputs
Variables:
hcl
variable "instance_type" {
type = string
default = "t2.micro"
hcl
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = {
Name = "example-instance"
Outputs:
hcl
output "instance_id" {
value = aws_instance.example.id
Page 5: Modules
Creating a Module:
bash
mkdir -p modules/my-module
hcl
# modules/my-module/main.tf
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
hcl
module "my_module" {
source = "./modules/my-module"
State Files:
Remote State:
hcl
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "path/to/my/key"
region = "us-west-2"
Page 7: Provisioners
Provisioners:
Provisioners execute scripts on a local or remote machine as part of the resource creation or
destruction.
hcl
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
provisioner "local-exec" {
Data Sources:
Data sources allow you to fetch data from external sources for use in your configuration.
hcl
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
hcl
ami = data.aws_ami.latest_amazon_linux.id
instance_type = "t2.micro"
Workspaces:
Workspaces allow you to manage multiple environments (e.g., dev, staging, production)
using the same configuration.
bash
hcl
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
Terraform Cloud:
Terraform Cloud provides collaboration, governance, and automation features for teams
using Terraform.
You can create workspaces, manage state, and apply configurations in a shared environment.
hcl
terraform {
cloud {
organization = "my-org"
workspaces {
name = "my-workspace"
Managing Secrets:
Use tools like HashiCorp Vault or environment variables to manage sensitive information like
API keys or passwords.
bash
export TF_VAR_db_password="mysecretpassword"
By following these notes and examples, you should have a comprehensive understanding of
Terraform's core features