terraform.tfstate
) to keep track of resource statuses. This is crucial for managing and updating resources effectively.terraform init
.Example:
provider "aws" {
region = "us-east-1"
}
provider "google" {
region = "us-central1"
}
terraform init
, Terraform downloads and installs the required providers.terraform import
command brings these resources into Terraform’s management.terraform state list
and terraform state show
allow users to inspect the current state file and the resources it manages.TF_LOG
to TRACE
, DEBUG
, etc., to provide detailed execution logs. This is useful for debugging and understanding Terraform’s behavior.module "network" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0"
}
Versioning: Specifying a module version ensures compatibility and stability. Example:
module "network" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
.tf
files.terraform plan
to preview changes.terraform apply
to create/update infrastructure.terraform plan
help detect and manage changes not made through Terraform.Backend Block: Defines where the state is stored, e.g., in an S3 bucket.
backend "s3" {
bucket = "my-tf-state"
key = "path/to/my/key"
region = "us-east-1"
}
Secrets in State: Sensitive data may be stored in state files, so securing state storage is critical. Use encryption and secure storage options to protect sensitive information.
Variables: Used to parameterize Terraform configurations.
variable "my-var" {
description = "My Test Variable"
type = string
default = "Hello"
validation {
condition = length(var.my-bat) >
error_message = "String more than 4 characters"
}
}
variable "my-var" {
description = "My Test Variable"
type = string
default = "Hello"
sensitive = true
}
Outputs: Used to return values from modules or the root module.
output "instance_ip" {
description = "VM's Private IP" # Variable config arguments such as variable description and value
value = aws_instance.my-vm.private_ip
}
Types: Terraform supports collections like lists and maps, and structural types like objects and tuples, which help handle complex data structures.
variable "training" {
type = list(string)
default = ["ACG","LA"] # Two separate strings in one variable
}
variable "instructor" {
type = object ({
name = string
age = number # Primitive types several named attributes
})
}
aws_instance
).aws_ami
).Resource Addressing: Terraform allows you to reference resources using their addresses, like aws_instance.my_instance
. This is used to define relationships and dependencies between resources.
provider "aws" {
region = "us-east-1" # Configuration parameters
}
resource "aws_instance" "web" {
ami = "ami-a1bsas9as9" # Configuration parameters
instance_type = "t4g.medium"
}
data "aws_instance" "my-vm" {
instance_id = "i-1asd12321eeadfs1"
}
# Resource Address --> data.aws_instance.my-vm
join()
, split()
, and lookup()
that are used to manipulate and manage data within configuration files.