Instance details not showing vpc in terraform

Below terraform code showing most of the details of instances in a region. Even subnet id showing but not vpc id.

provider “aws” {
region = “us-west-1”
}

data “aws_instances” “instances” {
instance_state_names = [“running”, “stopped”,“pending”, “shutting-down”, “stopping”] #, “terminated”]
}

data “aws_instance” “this” {
for_each = toset(data.aws_instances.instances.ids)
instance_id = each.key
}
output “instance_arns” {
value = data.aws_instance.this[*]
}

How to retrieve vpc id also?

Hi @uday-globuslive,

In the EC2 API there is no direct relationship between EC2 instance and VPC: an EC2 instance’s network interface belongs to one subnet and then each subnet belongs to one VPC.

Once you have a subnet ID, you can find the VPC it belongs to using the aws_subnet data source and then accessing the vpc_id attribute of the resulting object.

Could you please help how to implement that in above code.