Familiarize yourself with HCL syntax used in Terraform
Learn about HCL blocks, parameters, and arguments
Hashicorp Configuration Language, This low-level syntax of the Terraform language is defined in terms of a syntax called HCL, which is also used by configuration languages in other applications, and in particular other HashiCorp products. It is not necessary to know all of the details of HCL syntax in order to use Terraform, just knowing the basics, should be enough.
The Terraform language syntax is built around two key syntax constructs: arguments and blocks.
Blocks and Arguments :-
A block is a container for other content and An argument assigns a value to a particular name:
filename = "/home/ubuntu/abc123.txt"
The identifier before the equals sign is the argument name, and the expression after the equals sign is the argument's value.
Resource block: block name used to mention the type of the block. The resource block expects two labels, which are local_file and “pet” in the example above. A particular block type may have any number of required labels, or it may require none.
Syntax:-
resource “<provider>_<resource type> “<resource name>” {
Argument1 = “”
Argument2 = ””
}
Ex:-
resource "aws_instance" "example" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = "my-ec2-instance"
}
}
Explore the different types of resources and data sources available in Terraform
Resource Blocks
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.
Resource Syntax
Resource declarations can include a number of advanced features, but only a small subset are required for initial use. More advanced syntax features, such as single resource declarations that produce multiple similar remote objects, are described later in this page.
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}
in the above example A resource
block declares a resource of a given type ("aws_instance") with a given local name ("web"). Within the block body (between {
and }
) are the configuration arguments for the resource itself. Most arguments in this section depend on the resource type, and indeed in this example both ami
and instance_type
are arguments defined
Resource Types
Each resource is associated with a single resource type, which determines the kind of infrastructure object it manages and what arguments and other attributes the resource supports.
Data Sources
Data sources allow Terraform to use the information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.
Data Source Syntax
A data source is accessed via a special kind of resource known as a data resource, declared using a data
block:
data "aws_ami" "example" {
most_recent = true
owners = ["self"]
tags = {
Name = "app-server"
Tested = "true"
}
}
A data
block requests that Terraform read from a given data source ("aws_ami") and export the result under the given local name ("example").
Within the block body (between {
and }
) are query constraints defined by the data source. Most arguments in this section depend on the data source, and indeed in this example most_recent
, owners
and tags
are all arguments defined specifically for
Understand variables, data types, and expressions in HCL
Create a variable.tf file and define a variable
variable "file_name" {
type = string
default = "myfile.txt"
}
Use the variable in a main.tf file to create a "local_file" resource
resource "local_file" "myfile" {
filename = var.file_name
content = "This is the file created for terraform."
}
We have created files variable.tf and main.tf and now we can use terraform and we need to run the below command
terraform init
terraform plan
terraform apply
Terraform will create the local_file
resource and the myfile.txt
file will be created in the same directory.
Practice writing Terraform configurations using HCL syntax
Add required_providers to your configuration, such as Docker or AWS
We need to add required_providers block now in this we need to specify all the deatils such as source of the provider, and the version of the provider
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "2.23.0"
}
}
}
provider "docker" {}
here we are using docker providers it will create and manage the containers in Docker. now we need to run the below commands.
Test your configuration using the Terraform CLI and make any necessary adjustments
Step 1:- terraform init
-This command will scan your tf files in that folder and install all the required automation things
Step 2:- terraform plan
-This command will create an execution plan for terraforming, the things that will be installed, the names, and the properties added.
Step 3:- terraform apply
-The actual execution and automation happen in this command.
Now we can see EC2 is up and running
Devops#devops,#TerraWeek Day 1
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee