3

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. Can you help ?

Thanks

EDIT: Added example of code

Hoguss
  • 35
  • 1
  • 5
  • 1
    Would you mind adding at least a part of the code? It's hard to understand this way what is it exactly that you want. – Marko E Mar 17 '22 at 16:08
  • Sure, added some example – Hoguss Mar 17 '22 at 16:17
  • That looks like it should work. What is the specific problem or error that you are encountering? – Mark B Mar 17 '22 at 16:32
  • I have an error : An argument named \"description\" is not expected here. because the module ./modules/aws_ec2 does not have any input variable named description – Hoguss Mar 17 '22 at 16:46
  • I have two questions: 1. Why does it have to be an output of the module instead of defining a variable when you need it? 2. Does description need to provide anything related to attributes the EC2 instance provides when created? You could also consider using tags. – Marko E Mar 17 '22 at 16:53
  • It has to be an output because my action "Terraform apply" is part of a workflow in ansible, and i need the output of the module to be given to other workflows that come after the vm provisioning. – Hoguss Mar 18 '22 at 13:36

1 Answers1

2

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
}
Martin Atkins
  • 62,420
  • 8
  • 120
  • 138