declaración:
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(string)
, list(list)
, list(map)
, …list("a", "b", "c") # <=0.11
["a", "b", "c"] # >=0.12
variable "public_subnet_cidr_blocks" { description = "Available cidr blocks for public subnets." type = list(string) default = [ "10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24", "10.0.8.0/24", ] }
map("a", "b", "c", "d") { "a" = "b" "c" = "d" }
{"a" = "b", "c" = "d"} { "a" = "b" "c" = "d" }
{ name = "John" age = 52 }
variable "wks_rg_name" { type = object({ pre = string, int = string, default = string }) } wks_rg_name = { pre = "prepre", int = "intint", default = "" } resource "azurerm_resource_group" "rg" { name = "rg1-rg-${var.wks_rg_name[terraform.workspace]}" location = "${var.location}" }
["a", 15, true]
/via: https://www.terraform.io/docs/configuration/types.html
variable "image_id" { type = string } variable "availability_zone_names" { type = list(string) default = ["us-west-1a"] } variable "docker_ports" { type = list(object({ internal = number external = number protocol = string })) default = [ { internal = 8300 external = 8300 protocol = "tcp" } ] }
var.<NOMBRE_DECLARATIVO>
name = "web-sg-${var.resource_tags["project"]}-${var.resource_tags["environment"]}"
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." } }
-var='<KEY>=«<VALUE>»'
image_id = "ami-abc123" availability_zone_names = [ "us-east-1a", "us-west-1c", ]
-var-file=«<FILE.TFVARS>»
-var «server=web»
{ "image_id": "ami-abc123", "availability_zone_names": ["us-west-1a", "us-west-1c"] }
-var
o -var-file
como valores de retorno de un módulo o recurso
terraform apply
data "terraform_remote_state" "vpc" { backend = "remote" config = { organization = "hashicorp" workspaces = { name = "vpc-prod" } } } # Terraform >= 0.12 resource "aws_instance" "foo" { # ... subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id }
output "instance_ip_addr" { value = aws_instance.server.private_ip }
terraform output [ [-j-son] [-no-color] [-state=<path_to_state_file>] ] <NOMBRE> *
-state se ignora si se trabaja con remote state
*
terraform output password password = <sensitive>'' ¿?locals { service_name = "forum" owner = "Community Team" }
locals { # Ids for multiple sets of EC2 instances, merged together instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id) } locals { # Common tags to be assigned to all resources common_tags = { Service = local.service_name Owner = local.owner } }
resource "aws_instance" "example" { # ... tags = local.common_tags }