Terraform Basics
Disadvantages of Data Center Deployment
- Time to set up
- Many manual steps involved
- Under utilization of resources e.g. unused web sites during night time
- Costly
IaC (Infrastructure as Code)
Hence IaC comes into picture which is API based and provisions resources in cloud environment.
Basically it can configures the entire provisioning environment.
Types of infrastructure components are web servers, databases, networks
3 types of Iac
1. Configuration Management
- Ansible
- Puppet
- SaltStack
2. Server Templating
- Docker
- Packer
- Vagrant
3. Provisioning Tools
- Terraform
- Cloud Formation
Terraform - Infrastructure Provisioning tool for various cloud platforms.
Providers
Providers enable Terraform to provision various infrastructure components.
- Infrastructure Components on cloud platforms
- Network Components - BigIp, Infoblox
- Data Diagnostics and Monitoring tools - DataBag, Grafana
- Databases - MySql, MongoDB
- VCS (Version Control Systems) - Github, Bitbucket
HCL (HashiCorp Configuration Language)
File extension - .tf
Terraform Stages
- INIT - Initializes project directory and identifies set of providers. Downloads and installs plugins for providers.
- PLAN - Create plan to achieve target state on target environment.
- APPLY - Makes changes to achieve the desired state on target environment.
Terraform scripts are used to Provision, Configure and Decommission Resources.
Terraform manages states of resources & remembers them as they are in existence. Imports state of other resources and manages them as well.
How to install Terraform
Terraform binary or zip can be downloaded from www.terrafor.io and installed on the deployment environment.
Structure of Terraform Files
main.tf
<block> <parameters>{
key1 = value1
key2 = value2
}
e.g. resource (block name) local_file (provider_resouceType) resource_name{
Arguments
}
Arguments are key value pairs which depend upon the resource type.
Providers
Available in registry.terraform.io maintained by Hashicorp
Official Providers - aws, azure, local
Verified Providers - Partner providers (3rd party) e.g. Heroku
Complementary Providers - provided by individual contributors of hashicorp community.
Providers are installed in .terraform/plugins
Location of official providers - registry.terraform.io/hashicorp/local (hostname/organizational namespace/provider)
Location of veirified providers registry.terraform.io/<org namespace>/heroku
Sets cannot have duplicate values, lists can.
Automatically loaded varibles - terraform.tfvars or terraform.tfvars.json, **.auto.tfvars or **.auto.tfvars.json
All other .tfvars files must be specified as command line argument in tarraform apply -var-file command.
Variable Precedence in Terraform
=> command line with -var or -var-file
=> ***.auto.tfvars or ***.auto.tfvars.json
=> terraform.tfvars or terraform.tfvars.json
=> environment variables - export TF_VARS_variableName = "value"
Explicit Dependency
Each resource created by Terraform will have a unique id which terraform will store in the resource's state file.
Terraform state will provide the following benefits:
* keeps a cache of the resource's atttributes and will identify which resources to change correctly based in dependencies
* terraform plan -refresh=false will only rely on local state copy and doesn't refresh state from actual resources. Performant!
Terraform state is the single point of truth of the state of the real world resources.
Terraform state files contain sensitive information and should be stored in remote secured storage such as AWS, Azure, Terraform Cloud.
Terraform refresh will refresh state file from real world resources if any changes. It'll not change configuration files.

Comments
Post a Comment