🍏 iOS / Mac OSApple M6-Chip: erste Benchmark-tests sind da(15.09.2026 um 22:27 Uhr)
🕵️ SicherheitslückenCVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition(15.09.2026 um 21:40 Uhr)
🍏 iOS / Mac OSApple M6-Chip: erste Benchmark-tests sind da(15.09.2026 um 22:27 Uhr)
🕵️ SicherheitslückenCVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition(15.09.2026 um 21:40 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 3 Min Lesezeit
0

Variables and Outputs in Terraform Day 3

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Terraform, an Infrastructure as Code (IaC) tool, allows you to manage and provision infrastructure resources using declarative configuration files. Two critical concepts in Terraform—variables and outputs—help in parameterizing configurations and extracting useful information about the managed infrastructure. This article covers variable types, variable declaration and definition, output values, local values, and variable validation in Terraform, supplemented by examples and hands-on exercises.






Variable Types



Terraform variables enable dynamic configuration by parameterizing values. They support the following types:





  1. String: Represents plain text.




CODE
   variable "region" {
type = string
default = "us-east-1"
}








  1. Number: Represents numeric values, including integers and floats.




CODE
   variable "instance_count" {
type = number
default = 2
}








  1. Bool: Represents a boolean value (true or false).




CODE
   variable "enable_feature" {
type = bool
default = true
}








  1. List: Represents a collection of values of the same type.




CODE
   variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}








  1. Map: Represents a collection of key-value pairs.




CODE
   variable "tags" {
type = map(string)
default = {
environment = "production"
team = "devops"
}
}








  1. Object: Represents structured data with named attributes.




CODE
   variable "instance_config" {
type = object({
instance_type = string
ami_id = string
key_name = string
})
default = {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
key_name = "default"
}
}








  1. Tuple: Represents a collection of values of different types.




CODE
   variable "example_tuple" {
type = tuple([string, number, bool])
default = ["example", 123, true]
}









Variable Declaration and Definition






Declaration



Variables are declared in a Terraform configuration file, typically variables.tf, using the variable block:




CODE
variable "example_variable" {
type = string
default = "default_value"
}









Definition



The values for variables can be defined in multiple ways:





  1. Inline Definition: Using -var flag during terraform apply:




CODE
   terraform apply -var "region=us-west-2"








  1. Variable Definition Files: Using .tfvars files:


    • Create a file named terraform.tfvars or example.tfvars.






CODE
   region = "us-west-2"







  • Apply the configuration:




CODE
   terraform apply -var-file="example.tfvars"








  1. Environment Variables: Prefix variable names with TF_VAR_:




CODE
   export TF_VAR_region=us-west-2








  1. Interactive Input: Terraform prompts for values if no default is provided.






Example: Dynamic AWS Instance Configuration



variables.tf:




CODE
variable "instance_type" {
type = string
default = "t2.micro"
}

variable "region" {
type = string
}






main.tf:




CODE
provider "aws" {
region = var.region
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}






Command:




CODE
terraform apply -var "region=us-east-1"









Output Values



Output values allow you to extract information about your resources after deployment. These can be displayed in the terminal, used by other configurations, or exported for automation.






Syntax






CODE
output "output_name" {
value = resource.attribute
}









Example: Displaying AWS Instance Public IP



outputs.tf:




CODE
output "instance_ip" {
value = aws_instance.example.public_ip
}









Command to View Outputs






CODE
terraform output









Local Values



Local values help define intermediate values that simplify configurations. They are defined using the locals block.






Example: Defining Derived Values



main.tf:




CODE
locals {
environment = "production"
instance_tags = {
Name = "example-instance"
Environment = local.environment
}
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = local.instance_tags
}









Variable Validation



Variable validation ensures that inputs meet specific criteria. It is defined using the validation block inside a variable block.






Example: Validating Instance Count



variables.tf:




CODE
variable "instance_count" {
type = number

validation {
condition = var.instance_count > 0 && var.instance_count <= 10
error_message = "The instance count must be between 1 and 10."
}
}









Hands-On Validation






CODE
terraform apply -var "instance_count=15"






Output:




CODE
Error: The instance count must be between 1 and 10.









Conclusion



Variables and outputs in Terraform provide powerful mechanisms for creating modular, reusable, and parameterized infrastructure configurations. By using variable types, declaration methods, output values, local values, and variable validation, you can create robust Terraform scripts tailored to your needs. Start with small examples and gradually incorporate advanced features into your workflows.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
CVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition
1 Quelle
DFN-CERT-2026-4853 Xcode: Eine Schwachstelle ermöglicht das Ausspähen von Informationen
1 Quelle
Enforce GitHub Advanced Security configurations
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Variables and Outputs in Terraform Day 3

Thematisch verwandte Begriffe: Variables, Outputs, Terraform · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...