amazon-web-servicesterraform

Terraform retrieve existing aws_vpc object by id


I have a VPC id vpc-xyz1234 and I need to pass the matching VPC object in terraform to one of our modules. However, I am struggling with how I can retrieve the actual VPC "object" based on the vpc id. I thought about using import since I can pass the id there, but I don't want to keep the VPC in my state, it is managed by someone else's terraform script.

I know I can use "data" to reference objects without having them in my state, but I don't know how to pass my vpc id to the "data" block.

import {
  to = aws_vpc.my_vpc
  id = "vpc-xyz1234"
}
data "aws_vpc" "my_vpc" {
}

module "my_module" {
   vpc = data.aws_vpc.my_vpc
}

Solution

  • Apparently I suffered from temporary blindness. The documentation lists the id right at the top.

    data "aws_vpc" "test_vpc" {
      id = "vpc-xyz1234"
    }