4

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

6
  • 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. Commented Mar 17, 2022 at 16:08
  • Sure, added some example Commented Mar 17, 2022 at 16:17
  • That looks like it should work. What is the specific problem or error that you are encountering? Commented Mar 17, 2022 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 Commented Mar 17, 2022 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. Commented Mar 17, 2022 at 16:53

1 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.