skip to Main Content

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
}

2

Answers


  1. Chosen as BEST ANSWER

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

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

  2. I think u can do it without import. Try do this:

    data "aws_vpc" "my_vpc" {
      id = "vpc-xyz1234"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search