Taula de continguts

terraform variables

declaración

declaración:

variables.tf
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}
 
variable "instance_count" {
  description = "Number of instances to provision."
  type        = number
  default     = 2
}
 
variable "enable_vpn_gateway" {
  description = "Enable a VPN gateway in your VPC."
  type        = bool
  default     = false
}
 
variable "resource_tags" {
  description = "Tags to set for all resources"
  type        = map(string)
  default     = {
    project     = "project-alpha",
    environment = "dev"
  }
}

uso:

 provider "aws" {
  region  = var.aws_region
 }
 
 module "ec2_instances" {
   source = "./modules/aws-instance"
 
   depends_on = [module.vpc]
 
   instance_count = var.instance_count
 
   #tags = {
   #  project     = "project-alpha",
   #  environment = "dev"
   #}
   tags = var.resource_tags
 }

list, map, set

funciones

más tipos

/via: https://www.terraform.io/docs/configuration/types.html

input variables

interpolación

name        = "web-sg-${var.resource_tags["project"]}-${var.resource_tags["environment"]}"

validación

variable "resource_tags" {
  description = "Tags to set for all resources"
  type        = map(string)
  default     = {
    project     = "my-project",
    environment = "dev"
  }
 
  validation {
    condition     = length(var.resource_tags["project"]) <= 16 && length(regexall("[^a-zA-Z0-9-]", var.resource_tags["project"])) == 0
    error_message = "The project tag must be no more than 16 characters, and only contain letters, numbers, and hyphens."
  }
 
  validation {
    condition     = length(var.resource_tags["environment"]) <= 8 && length(regexall("[^a-zA-Z0-9-]", var.resource_tags["environment"])) == 0
    error_message = "The environment tag must be no more than 8 characters, and only contain letters, numbers, and hyphens."
  }
}

asignación de valores en variables del módulo raiz (Root Module)

output

como valores de retorno de un módulo o recurso

usos

declaración

output "instance_ip_addr" {
  value = aws_instance.server.private_ip
}

acceso

Local values