Terraform Basics
2 min readJul 31, 2022
Terraform workflow
These are the major commands that are used to initialise create, update or delete Infrastructure resources.
- Init
- For initialising the working directory where containing the config files
- It download Providers
- Initialising the backend - Validate
- Validates the terraform config. Files in that respective directory to make sure it is syntactically and internally consistent. - Plan
- Create an execution plan with command line - Apply
- It will apply on the cloud provider - Destroy
- It will destroy it will all and also single commands
Terraform configuration language syntax
Types of Blocks
- Top level blocks
- Example:- Resource and Providers - Block inside blocks
- Example Provisioners and resource specific blocks like tags - “Block Type” “Block label<defined by cloud provider>” “Block label<For local reference>”
- Example:- Resource “aws_instance” “my_ec2” - Resource contains 2 Block labels and Variables blocks contains 1 label
- Comments:- To comments we can use :- #, // and /* */
Understand about Arguments, Attributes and Meta-Arguments
- Arguments
- It configures a particular resource example “ami” = “ami-2213412421”
- It can be required and optional - Attribute reference
- Values exposed by particular resource
- It can be referenced
- It is assigned by underlying cloud provider
- This can be exported as output values - Meta-arguments
- Terraform specific arguments
- Examples depends_on, count, for_each, provider and lifecycle
Understanding about Terraform Blocks
- Block that can appear outside of any other block in a TF config. file
- Most of TF feature are implemented as Top level blocks
- 3 Major blocks
- Fundamental blocks
- Terraform Block:- Here is the terraform settings
- Providers Block:- Aws or Azure providers
- Resource Block:- What kind of resource you are provisioning - Variables blocks:- This tells how to parametrise the whole thing we created
- Input Variables
- Output Values Block
- Local Values Block - Calling or referencing blocks:-
- Data Source Block:- To get some information about cloud providers
- Modules Block.- To provide some information to that block