amazon-web-servicesvariablesterraformoutput

How to output an input variable?


I have a custom terraform module which create an AWS EC2 instance, so it's relying on the aws provider. This terraform custom module is used as a base to describes the instance i want to create, but I also need some other information that will be reused later.

For example, i want to define a description to the VM as an input variable, but i don't need to use it at all to create my vm with the aws provider.

I just want this input variable to be sent directly as an output so it can be re-used later once terraform has done its job.

ex What I have as input variable

variable "description" {
  type        = string
  description = "Description of the instance"
}

what I wanna put as output variable

output "description" {
  value      = module.ec2_instance.description
}

What my main module is doing

module "ec2_instance" {
  source            = "./modules/aws_ec2"
  ami_id            = var.ami_id

  instance_name     = var.hostname
  disk_size         = var.disk_size
  create_disk       = var.create_disk
  availability_zone = var.availability_zone
  disk_type         = var.disk_type
// I don't need the description variable for the module to work, and I don't wanna do anything with it here, i need it later as output
}

I feel stupid because i searched the web for an answer and can't find anything to do that.

EDIT: Added example of code


Solution

  • If you have an input variable declared like this:

    variable "description" {
      type = string
    }
    

    ...then you can return its value as an output value like this, in the same module where you declared it:

    output "description" {
      value = var.description
    }