Terraform Syntax
Terraform employs its dedicated domain-specific language known as HashiCorp Configuration Language (HCL) to establish infrastructure resources and configurations. HCL is crafted to find an equilibrium between human-friendly readability and machine-friendly interpretability. Provided below is a synopsis of the syntax utilized within Terraform. To know more about terraform blocks refer to Different Types of Blocks in Terraform.
Resource Block
Resource blocks delineate singular infrastructure resources. For the AWS EC2 instance.
resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = “t2.micro”
}
A block has a type (resource in this example). Each block type defines how many labels must follow the type keyword. The resource block type expects two labels, which are aws_instance and example in the example above. A particular block type may have any number of required labels, or it may require none as with the nested network_interface block type. You can apply the above code to make the provision by using the terraform apply command.
Variables Block
Variables grant the ability for parameterization and re-usability in your setups. They can be outlined as input variables or output values.
variable “variable_name” {
description = “Description of the variable”
default = “default_value”
}
Data Blocks
Data blocks retrieve information from external origins or existing resources for utilization in your configurations.
data “aws_ami” “example” {
most_recent = true
owners = [“self”]
}
Incorporating these constructs, Terraform embraces HCL as a tool for crafting both human-readable and machine-processable infrastructure code.From above code we are going to create Amazon machine image (AMI).
Terraform Resources
Terraform, created by HashiCorp, is an open-source tool for infrastructure as code (IaC). Through code, it empowers you to declaratively define, oversee, and provision infrastructure resources. This uniform workflow allows you to efficiently generate, modify, and remove infrastructure elements, whether on diverse cloud platforms or within on-premises setups. It facilitates the definition, management, and provisioning of infrastructure resources using code in a declarative manner. To know how to set up Terraform On Linux and Windows Machine.