How to match data source and acquire the other info

Hello!

I’m trying to capture the AWS instance ID, using the following code. My intent is to match on the tag name of ES1_DB, and then the data source populates the instance _id. Is this going to work? How else to match on the tag name and then capture the instance_id? Trying to refer to it in building a cloudwatch dashboard.

#Get instance ID for DB server

data “aws_instance” “ES1_DB” {

instance_id = [db-i-instanceid] # this is a var, to store the acquired instance ID.

filter {

name   = "tag:Name"

values = ["ES1_DB"]   #  That's a literal, match the correct EC2 instance on that tag value

}

}
and here’s my two errors:

Error: Invalid reference

on asg.tf line 24, in data “aws_instance” “ES1_DB”:
24: instance_id = [db-i-instanceid]

A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.

Error: “dashboard_body” contains an invalid JSON: invalid character ‘d’ looking for beginning of value

on cloudwatch.tf line 7, in resource “aws_cloudwatch_dashboard” “main”:
7: resource “aws_cloudwatch_dashboard” “main” {

This is not how Terraform works, this is a descriptive language where you work with definitions and attributes.

Looking at https://www.terraform.io/docs/providers/aws/d/instance.html we can see that the data source has an optional parameter instance_id as one way of indicating which instance we wish to retrieve data about. So that’s an input, and we can’t use that one as we don’t have the instance id. (I guess right now you’re thinking “why bother looking it up if we already know it?” Thing is that the data source gives us a lot more information about the instance, not just the id.)

In your case you want to search for an instance using the Name tag as a filter.

This would simply be

data aws_instance ES1_DB {
  filter {
    name   = "tag:Name"
    values = ["ES1_DB"]   #  That's a literal, match the correct EC2 instance on that tag value
  }
}

And then Terraform will go and retrieve information about that instance and make it available as attributes, one of them being the instance id which you can refer to in other places of the configuration like this: data.ES1_DB.id

Thanks much Bent!

Ok I tried that (I think).

Here I grab the info:
#Data sources
#Go get the db instance info
data aws_instance ES1_DB {
filter {
name = “tag:Name”
values = [“ES1_DB”]
}
}

Then as you suggest, I used this in the cloudwatch metric code:
[
“AWS/EC2”,
“CPUUtilization”,
“InstanceId”,
[“data.ES1_DB.id”]
[
and the error is still
Error: “dashboard_body” contains an invalid JSON: invalid character ‘d’ looking for beginning of value

on cloudwatch.tf line 7, in resource “aws_cloudwatch_dashboard” “main”:

  • 7: resource “aws_cloudwatch_dashboard” “main” {*

PS - I did validate that it is this metric that is causing the fail, because when I remove it, the other two work fine - they are based on the “AutoScalingGroupName”.

You get this error because all instance IDs start with “i-” and you are using the string “data…” which starts with a “d” :wink:

Try removing the quotes so it becomes a variable reference instead of a literal string.