Low Orbit Flux Logo 2 D

Terraform Basics - Variables and Outputs

This section is based on the configuration created in the previous section.


Define a variable in a file like this.

variable "container_name" { description = "name of the container" type = string default = "Container1" }

Update a resource to use a variable:

resource "docker_container" "nginx" { image = docker_image.nginx.image_id name = var.container_name ports { internal = 80 external = 8080 } }

Apply the updated configuration like this:

terraform apply

You can override a variable during apply like this:

terraform apply -var "container_name=container2"


You can define outputs that will be exposed when creating infrastructure.

output "container_id" { description = "ID of the Docker container" value = docker_container.nginx.id } output "image_id" { description = "ID of the Docker image" value = docker_image.nginx.id }

You can also query the outputs with the following command after infrastructure has been built.

terraform output