Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use.
outputs.tf
output "instance_ip_addr" {
value = aws_instance.server.private_ip
description = "The private IP address of the main server instance."
}
terraform output # Command is used to extract the value of an output variable from the state file.
Variables let you customize aspects of Terraform modules without altering the module's own source code.
variables.tf - file for variables.
default - A default value which then makes the variable optional.
type - This argument specifies what value types are accepted for the variable.
description - This specifies the input variable's documentation.
validation - A block to define validation rules, usually in addition to type constraints.
sensitive - Limits Terraform UI output when the variable is used in configuration.
Sample1
variable "project_name" {
default = "Tesla"
}
Sample2
variable "region" {
description = "Please Enter using region"
default = "eu-central-1"
}
provider "aws" {
region = var.region
}
To specify individual variables on the command line, use the -var option when running the terraform plan and terraform apply commands:
terraform apply -var="image_id=ami-abc123" -var="instance_type=t2.micro"
Terraform also automatically loads of variable definitions file if it is present terraform.tfvars
To set lots of variables, it is more convenient to specify their values in a variable definitions file .tfvars
terraform apply -var-file="testing.tfvars"
Link: https://www.terraform.io/docs/language/values/variables.html
Local values assign a name to an expression, that can then be used multiple times within a module.
main.tf
...
locals {
full_project_name = "${var.environment}-${var.project_name}"
}
locals {
contributor = "Billy Bob"
}
resource "aws_eip" "my_static_ip" {
tags = {
Project = local.full_project_name
participant = local.contributor
}
}