Skip to content

Terraform

Terraform-Core-Principles

Introduction

Terraform is an open-source tool for defining, provisioning, and managing infrastructure as code. It works with various infrastructure providers, including popular cloud providers like AWS, Azure, and Google Cloud, as well as on-premises providers like VMware and OpenStack.

Terraform’s core feature is its ability to create and manage infrastructure resources predictably. By defining infrastructure as code, users can version, review, collaborate on changes, and easily track changes and roll back to a previous state.

homepage-banner

Terraform also offers advanced features like remote state management, workspaces, and modules, which organize and reuse code and manage infrastructure state.

Terraform has a large, active community that creates and shares custom modules and providers, making it easy to manage additional resources and services without writing custom code.

terraform-life-cycle

Basic Usage

tf-cheatsheet.jpg

Configure token

Initialize

terraform init

In the process of initializing the project, Terraform will parse the *.tf files in the directory and load the relevant provider plugins.

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Change log level

export TF_LOG=DEBUG

Plan

terraform plan

Apply

terraform apply
terraform state list

## passing var
terraform apply -var static_ip=false

Destroy

terraform destroy

Terraform functions

terraform console

> max(2,3)
3
> lower("HELLO WORLD")
"hello world"
> index(["apple", "orange", "banana"], "orange")
1
> formatdate("HH:mm",timestamp())
"01:32"

Terraform Validate

terraform validate
## or
terraform validate <config_files_path>

e.g.

// Standalone EC2 instance
resource "aws_instance" "my_vm" {
 ami           = var.ami //Ubuntu AMI
 instance_type = var.instance_type
 tags = {
   Name = var.name_tag,
 }
}
variable "ami" {
 type        = string
 description = "Ubuntu AMI ID in N. Virginia Region"
 default     = "ami-065deacbcaac64cf2"
}

variable "instance_type" {
 type        = string
 description = "Instance type"
 default     = "t2.micro"
}

variable "name_tag" {
 type        = string
 description = "Name of the EC2 instance"
 default     = "My EC2 Instance"
}
terraform validate
terraform validate -no-color
terraform validate -json

output

Success! The configuration is valid.

200 Terraform Interview Q&A

200 Terraform Interview Q&A

Terraform vs Ansible

Ansible Terraform
Type Configuration management tool Orchestration tool
Syntax YAML HCL
Language Procedural Declarative
Default approach Immutable infrastructure Mutable infrastructure
Cloud support All clouds All clouds
Lifecycle (state) management Does not support Depends on the lifecycle and state management
Packaging and templating Provides complete support Provides partial support
Capabilities Provisioning and configuring Provisioning and configuring
Agentless Yes Yes
Masterless Yes Yes
License Open Source Business Source License (BUSL)

Terraform with GitHub Actions

  • https://spacelift.io/blog/github-actions-terraform

How To Set Up AWS Lambda Function in Terraform

https://www.baeldung.com/ops/terraform-aws-lambda-function

Reference

  • https://www.terraform.io
  • Learning DevOps, Second Edition, A comprehensive guide to accelerating DevOps culture adoption with Terraform, Azure DevOps, Kubernetes, and Jenkins, Mikael Krief, 2022
  • https://opentofu.org/
  • https://www.fosstechnix.com/terraform-interview-questions-and-answers/
  • https://spacelift.io/blog/terraform-validate
  • https://developer.hashicorp.com/terraform/cli/commands/validate
  • https://spacelift.io/blog/ansible-vs-terraform
  • https://spacelift.io/blog/terraform-automation
  • https://spacelift.io/blog/github-actions-terraform
  • https://cheat-sheets.nth-root.nl/terraform-cheat-sheet.pdf
Feedback