Terraform is an IAC (Infrastructure as code) tool that helps to automate provisioning, configuring and managing the application infrastructure, platform and services.
- It resembles ansible in a major way but ansible is more likely a configuration tool on an existing infrastructure
- We can easily make any changes to existing infrastructure using Terraform.
- We can easily replicate an existing infrastructure using Terraform.
Terraform has two components
- Terraform Core
- Terraform Input
- Terraform State
- Terraform Providers
- IAAS (Cloud) Providers (AWS)
- PaaS Providers (Kubenetes)
- Service Providers (Fastly)
Terraform core components is used to create plan while the provider components is used to execute that plan.
Terraform code is written in a language called HCL i.e. Hashicorp Configuration Language. The code is saved in a file with extension .tf. It can create infrastructure across variety of providers like AWS, GCP, Azure, Digital Ocean etc.
Terraform Commands
- Refresh:
- Gets the current state using the provider component
- Plan:
- Creates an execution plan using the core component
- Apply
- Executes the plan
- Destroy
- Removes the infrastructure
Pre-requisites
- AWS CLI
- Terraform
- AWS CLI configured for AWS account to be used. See
Install terraform
- choco install terraform (via Windows Powershell)
- brew install terraform (via Mac terminal)
- Run below command to verify installation
- terraform --version
Terraform plugins
- These are executable binaries written in Go language that communicate with Terraform Core over an RPC interface. e.g. aws provider is a plugin
Terraform modules
- A module is a container for multiple resources that are used together.
- A terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.
Terraform providers
- A provider adds a set of resource types and or data sources that Terraform can manage.
- They are available in terraform registry at url https://registry.terraform.io/browse/providers?product_intent=terraform
- They are constrained in configuration called provider requirements in production environments
# Provider requirements are defined in this block
terraform {
# Declare the required version using Version Constraint Syntax
required_version = ">= 1.0"
# Declare the required providers needed by the module
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.50.0, < 5.0.0"
}
}
}
Terraform Variables
- Input
- Input variables let you customise aspects of Terraform modules without altering the module's own source code.
- To declare variables in the root module of the configuration, we can set their values using CLI options and environment variables.
- To declare variables in child modules, the calling module should pass values in the module block.
- An input variable in terraform can be defined as
variable "variable_name"{
default = "value",description="Stores the value for variable_name",type="string/number/bool/list",validation{condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."}
}
Sample Terraform code
- To define the provider and the region to be used for provisioning infrastructure, you can create a file with name main.tf and add below content
- To create a resource such as instance, database, load balancer etc, you can add content in below syntax
e.g.
resource "aws_instance" "testing"{
instance_type="t2.micro"
- To execute terraform code
- Go to the directory, where the main.tf is saved, via terminal
- Run command
- terraform init
- The above command will initialize backend and the requested provider plugins inside a folder called .terraform
- Run command
- terraform plan -out "myplan.txt"
- The above command will show what terraform will actually do. It is a kind of sanity testing. The plan will be saved to file myplan.txt
- Run command
- terraform apply "myplan.txt"
- The above command will create the resource
- Run command
- terraform destroy
- The above command will delete all the resources
No comments:
Post a Comment