Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the Instance ID of the EMR master instance in Terraform?

Tags:

terraform

The following code gives me a list of all EC2 instances that are part of my cluster:

data "aws_instances" "emrMaster" {
  instance_tags {
    Name = "emr-cluster-name"
  }
}

But when I try to narrow the list using the AWS generated tag for the master instance, I get the same list.

data "aws_instances" "emrMaster" {
  instance_tags {
    Name = "emr-cluster-name"
    "aws:elasticmapreduce:instance-group-role" = "MASTER"
  }
}

If I remove the quotes on the key name, I get a illegal character error due to the colons in the key name.

data "aws_instances" "emrMaster" {
  instance_tags {
    Name = "emr-cluster-name"
    aws:elasticmapreduce:instance-group-role = "MASTER"
  }
}

Is there a better way to do this, am I doing something wrong, or have I uncovered a bug in Terraform?

I am using Terraform v0.11.7

I am looking to capture this value so I can build specific cloudwatch alerts for the master instance that are different from the Core instances.

like image 964
Francis Nickels Avatar asked Oct 20 '25 09:10

Francis Nickels


1 Answers

For this purpose better use aws_instance (not aws_instances)

resource "aws_emr_cluster" "emr-cluster" {
....
}

data "aws_instance" "master" {

  filter {
    name = "tag:Name"
    values = ["${aws_emr_cluster.emr-cluster.name}"]
  }

  filter {
    name = "tag:aws:elasticmapreduce:instance-group-role"
    values = ["MASTER"]
  }
}

And then just use output:

output "master_id" {
  value = "${data.aws_instance.master.id}"
}
like image 156
VitaliyD Avatar answered Oct 22 '25 05:10

VitaliyD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!