- Terraform is a tool for performing system provisioning
- It uses HCL syntax (Hashicorp Configuration Language)
Terraform uses HCL for performing the provisioning type.
<block> <resource-type> <logical-name> {
arg1 = value1
arg2 = value2
}
The syntax is divided into
block: Block name defines the type of block
Resource-Type: Resource type is the predefined names for every resource. like "local_file", where
Resource-name: It is the name of resource
arguments: All the arguments
For e.g.
Creating a file in local resource
resource "local_file" "basic_file" {
filename = "/home/basic_file.txt"
content = "This is a basic file created by Terraform"
}
AWS EC2 Instance provisioning
resource "aws_instance" "backendserver"{
ami = "ami-10101010"
instance_type = "t2.micro"
}
AWS S3 Bucket provisioning
resource "aws_s3_bucket" "doc_bucket" {
bucket = "document_bucket_for_backend"
acl = "private"
}
## Start provisioning
1. Initiate the provisioning
```terraform
terraform init
- This command check the configuration files
- Initiate the working directory with name .terraform
- Understand about the provider type and install essential plugins
- Review the plan
terraform plan
This command will display the terraform action plan
- Apply the changes
terraform apply
- To show the resources
terraform show
- To Destroy the complete provisioning
terraform destroy
-
main.tf Main file contains resource configuration
-
variables.tf Contains variable declarations
-
outputs.tf Contains Outputs from Resources
-
provider.tf Contains Provider Definition
To illustrate this,
lets make use of new provider "random"
To create random details we can use random as resource type
resource "random_pet" "my-employee" {
prefix = "Mr."
seperator = "."
length = "1"
}
Variable Syntax
variable "<variable_name>" {
default = "<value>"
}