Wednesday, October 5, 2022

[SOLVED] Terraform - passing vars between different resources

Issue

I have one resource to create ec2 instance, and another one for creating ebs (with attach inside).

In the ebs I need to give it instance_id = aws_instance.ec2.id which is created in the ec2 resource. How can I pass the var value from one resource to the other one?

I'm using modules with tfvars file to consume bout resources to create ec2 instance with external disk.

ec2 main file:

# NIC
resource "aws_network_interface" "nic" {
  subnet_id   = "${var.subnet_id}"
  private_ips = "${var.ip_address}"
  tags       = { Name = var.tags }
}


# EC2 Instance
resource "aws_instance" "ec2" {
  ami                     = "${var.ami}"
  instance_type           = "${var.instance_type}"
  iam_instance_profile    = "${var.iam_instance_profile}"
  tags                    = { Name = var.tags }
  key_name                = "${var.key_name}"
  network_interface {
      network_interface_id = "${aws_network_interface.nic.id}"
      device_index = 0
    }
}

External disk main file:

resource "aws_ebs_volume" "external_disk" {
    availability_zone = "${var.availability_zone}"
    size              = "${var.disk_size}"
    type              = "${var.disk_type}"
    tags              = { Name = var.tags }
}


resource "aws_volume_attachment" "disk_attach" {
    device_name = "${var.device_name}"
    volume_id   = aws_ebs_volume.external_disk.id
    instance_id = aws_instance.ec2.id
}

Main env module:

module "external_disk_red" {
  source                  = "../source-modules/external-disk"
  availability_zone       = "${var.availability_zone}"
  size                    = "${var.disk_size_red}"
  type                    = "${var.disk_type_red}"
}


module "red" {
  source                  = "../source-modules/ec2"
  region                  = "${var.region}"
  access_key              = "${var.access_key}"
  secret_key              = "${var.secret_key}"
  ami                     = "${var.ami}"
  instance_type           = "${var.instance_type}"
  iam_instance_profile    = "${var.iam_instance_profile}"
  tags                    = "${var.tags_red}"
  key_name                = "${var.key_name}"
  ip_address              = "${var.ip_address_red}"
  subnet_id               = "${var.subnet_id}"
  device_name             = "${var.volume_path_red}"
}

Solution

What you would have to do in this situation is add an output to your ec2 module and use it as an input (variable) in your external disk module.

Also, I don't know what version of Terraform you are using, but using double quotes to refer to a variable is considered legacy (unless you want to do interpolation).

So...

source-modules/ec2/output.tf

output "instance_id" {
  value       = aws_instance.ec2.id
  description = "ID of the EC2 instance"
}

source-modules/external-disk/variables.tf

variable "instance_id" {
  type        = string
  description = "ID of the EC2 instance"
}

source-modules/external-disk/main.tf

resource "aws_ebs_volume" "external_disk" {
  availability_zone = var.availability_zone
  size              = var.disk_size
  type              = var.disk_type
  tags              = { Name = var.tags }
}

resource "aws_volume_attachment" "disk_attach" {
  device_name = var.device_name
  volume_id   = aws_ebs_volume.external_disk.id
  instance_id = var.instance_id
}

Main env module

module "external_disk_red" {
  source                  = "../source-modules/external-disk"
  availability_zone       = var.availability_zone
  size                    = var.disk_size_red
  type                    = var.disk_type_red
  instance_id             = module.red.instance_id
}


module "red" {
  source                  = "../source-modules/ec2"
  region                  = var.region
  access_key              = var.access_key
  secret_key              = var.secret_key
  ami                     = var.ami
  instance_type           = var.instance_type
  iam_instance_profile    = var.iam_instance_profile
  tags                    = var.tags_red
  key_name                = var.key_name
  ip_address              = var.ip_address_red
  subnet_id               = var.subnet_id
  device_name             = var.volume_path_red
}


Answered By - Julien B.
Answer Checked By - Katrina (WPSolving Volunteer)