Skip to main content

Files & Components

Terraform does not have a specified directory structure as found in Chef or Ansible. But terraform follows different file extentions for the need.
Terraform reads all .tf files and apply them.

Filesโ€‹

.tfโ€‹

  • All terraform configurations are writen in .tf files.
  • We can write one file with all required configurations or we can split the same configuration into multiple *.tf files for the ease of use.

.tfvarsโ€‹

  • Declaring terraform variables with default values should be handled in .tf files
  • However multiple .tfvars files can be maintained for different environments or application.
  • These .tfvars file can be passed to plan & apply commands to overwrite the default values.
terraform plan --var-file ./dev.tfvars
terraform apply --var-file ./dev.tfvars

.tfstateโ€‹

  • It stores the result of apply or current state from the terraform configuration
  • It follows json syntax
  • We can use backend configuration and specify the location to store
  • If backend is not specified terraform.tfstate file will be generated in the current directory

Componentsโ€‹

Resourcesโ€‹

  • resource creates the resource with the confuguration mentioned against it's attributes
resource "aws_instance" "web" {
ami = "ami-408c7f28"
instance_type = "t1.micro"
}
  • To get the complete list of attribute for each resource, please visit terraform official document.

Data:โ€‹

  • data source allows a Terraform configuration to build on information defined outside of Terraform.
data "aws_ami" "web" {
filter {
name = "state"
values = ["available"]
}

filter {
name = "tag:Component"
values = ["web"]
}

most_recent = true
}

resource "aws_instance" "web" {
ami = "${data.aws_ami.web.id}"
instance_type = "t1.micro"
}
  • To get the complete list of attributes for each data source, please visit terraform official document.

Providersโ€‹

  • Providers are responsible in Terraform for managing the lifecycle of a resource: create, read, update, delete.
  • Most providers require some sort of configuration to provide authentication information, endpoint URLs, etc.
provider "aws" {
access_key = "foo"
secret_key = "bar"
region = "us-east-1"
}

Variablesโ€‹

  • Input variables are parameters for the terraform configuration
  • variable type can be string, list, map
  • If type is not specified, type will be inferred from default
    tip

    If the default value is not specified and terraform plan/apply ran without --var-file flag, terraform will prompts for the user to key in the value. This practise may lead to errors during CI/CD. It is good practise to specify the default value.

  • We can use TFVAR as a pre-fix for the variables and use environment variable.
export  TF_VAR_instance_type=t2.micro
variable "instance_type" {
type = "string"
}
  • .tfvars files store values of input variables
instance_type=t2.micro
foo=bar

Outputโ€‹

  • Output values are like return or print statements.
  • While using output in child modules, it works like return so that these output values can be used else where in the main module/terraform configuration.
output "instance_id" {
value = aws_instance.web.id
}

Modulesโ€‹

  • Similar to Classes in other programming language, modules help in offloading standard configuration.
  • In Terraform, every directory which consists of .tf files is a module.
  • Modules can be import from local filesystem or from a public or private registry.
  • source atrribute consist of paths, addresses, and URIs which represent the local/public/private module.
module "ec2-instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "2.17.0"
name = "my-cluster"
instance_count = 5
}

Functionsโ€‹

output "combinied" {
value = zipmap(aws_iam_user.lb[*].name, aws_iam_user.lb[*].arn)
}