Getting the vpc_id in an aws_instance module


I’m stuck with creating an aws_instance via a module I created.
I did setup the VPC already, which also uses a module. Now I’d like to create an AWS Instance in that VPC but I can’t figure out how to get the vpc_id that is needed for creation.

Following is the hierarchy of my terraform files

|-- dev
|   `-- custXX
|       |-- services
|       `-- vpc
|-- modules
|   |-- ec2_service
|   |   |--
|   |   |--
|   |   `--
|   `-- vpc
|       |--
|       |--
|       `--

The running vpc comes from dev/custXX/vpc
the aws instance comes from dev/custXX/services/admin-host

In the terraform config for the new host I have added a data source as follows

data "terraform_remote_state" "vpc" {
  backend = "s3"
  config = {
    bucket = "tfstate-dev"
    key    = "dev/custxx/vpc/terraform.tfstate"
    region = var.region

which gets read but it doesn’t seem to have a vpc_id. Sorry, I can’t get my head around that for days.


Okay, it was my fault. I rthought that I would have to define a variable ‘vpc_id’ as well and that’s why I got errors.
To get the vpc_id one has to use

output "vpc_id" {
  value = module.vpc.vpc_id

Sorry for the noise.

Not able to edit the Titel and add [Solved]…