Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an instance id / arn of an existing ec2 instance via terraform?

I am using Terraform for some infrastructure deployment:

data "aws_instance" "ec2_my_admin_instance" {
  filter {
    name = "tag:TfServerType"
    values = ["myAdmin"]
  }
}


output "theId" {
  value = "${data.aws_instance.ec2_my_admin_instance.instance_id}"
}
output "type" {
  value = "${data.aws_instance.ec2_my_admin_instance.instance_type}"
}
output "placement_group" {
  value = "${data.aws_instance.ec2_my_admin_instance.placement_group}"
}
output "availability_zone" {
  value = "${data.aws_instance.ec2_my_admin_instance.availability_zone}"
}
output "arn" {
  value = "${data.aws_instance.ec2_my_admin_instance.arn}"
}

However, the most important values (id, arn) are not shown:

Outputs:

availability_zone = ap-southeast-2b
type = m4.large

Did I miss anything?

like image 206
hey Avatar asked Mar 07 '23 22:03

hey


1 Answers

@jdurkin answered the question but didn't tell you how to get it, which is you are asking for.

terraform documents are not always updated with all attributes (I call them available output variables). In your case, you can't find the right attribute to be used from the online document. Guess is possible as jdurkin's answer, but it is not always correct.

So you need to find them out by yourself and find the right one.

( you can change the region with your favorite region)

provider "aws" {
  region = "ap-southeast-2"
}

data "aws_instance" "ec2_my_admin_instance" {
  filter {
    name = "tag:TfServerType"
    values = ["myAdmin"]
  }
}

terraform apply the change (it is data source, will not create any resources, so you are safe to run)

terraform init
terraform plan
terraform apply

After you successfully run above commands, you should get a local file terraform.tfstate

Cat the file, in part of attributes, you should get all attributes with attribute name and value, id is in the attribute list, but there is no attribute arn which you can confirm now. You should be confident to choose the right one and put in your output codes.

"modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "data.aws_instance.ec2_my_admin_instance": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-xxxx",
                        "attributes": {
                            "ami": "ami-xxxx",
                            ....
                            "id": "i-xxxx",
                            "instance_state": "running",
                            ....
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": ""
                }
            },
            "depends_on": []
        }
    ]
like image 157
BMW Avatar answered Apr 28 '23 16:04

BMW