Answers: <p>Without seeing the contents of your two modules I'm guessing a bit, but it looks like you have an AWS VPC declared in your <code>vpc</code> module and some subnets declared in your <code>prod_subnets</code> module and you are asking how the configuration of the subnets can get access to the VPC ID.</p>
  
  <p>If so, the answer is that the <code>vpc</code> module must export the VPC ID as an <a href="https://www.terraform.io/docs/configuration/outputs.html" rel="nofollow noreferrer">output value</a> and then the <code>prod_subnets</code> module must accept the VPC ID as an <a href="https://www.terraform.io/docs/configuration/variables.html" rel="nofollow noreferrer">input variable</a>.</p>
  
  <p>In your <code>vpc</code> module, you can declare a <code>vpc_id</code> output value like this, for example in a file <code>modules/vpc/outputs.tf</code>:</p>
  
  <pre><code>output "vpc_id" {
    value = aws_vpc.production_vpc
  }
  </code></pre>
  
  <p>In your <code>prod_subnets</code> module you can declare a <code>vpc_id</code> input variable, for example in a file <code>modules/vpc/modules/subnets/production/outputs.tf</code>:</p>
  
  <pre><code>variable "vpc_id" {
    type = string
  }
  </code></pre>
  
  <p>Then in your existing <code>modules/vpc/modules/subnets/production/production.tf</code> file, on line 162, change the <code>vpc_id</code> argument for the subnet to refer to that variable:</p>
  
  <pre><code>  vpc_id = var.vpc_id
  </code></pre>
  
  <p>Finally, you must then edit the top-level file whose source code you shared in your question to pass the value between the two modules, like this:</p>
  
  <pre><code>module "vpc" {
    source = "./modules/vpc"
  }
  
  module "prod_subnets" {
    source = "./modules/vpc/modules/subnets/production"
  
    vpc_id = module.vpc.vpc_id
  }
  </code></pre>
  
  <p><code>module.vpc.vpc_id</code> means to take the value of the <code>vpc_id</code> output value from the <code>vpc</code> module.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-03T23:03:37.293Z
Answers: 2020-04-03T23:03:37.293Z
Answers: Martin Atkins
Answers: <p>It should be written as:</p>
  <pre><code>output &quot;vpc_id&quot; {
    value = aws_vpc.production_vpc.id
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-25T06:46:33.997Z
Answers: 2021-03-25T08:07:08.083Z
Answers: 2021-03-25T08:07:08.083Z
Answers: Srikanth
Body: <p>I have a module structure like</p>
  
  <pre><code>module "vpc" {
  source = "./modules/vpc"
  }
  
  module "prod_subnets" {
  source = "./modules/vpc/modules/subnets/production"
  }
  </code></pre>
  
  <p>when running am getting an error like</p>
  
  <pre><code>[0m on modules/vpc/modules/subnets/production/production.tf line 
  
      162, in resource "aws_subnet" "prod-pub-1b":
      14:22:55 162: vpc_id = [4maws_vpc.production_vpc[0m.id
      14:22:55 [0m
      14:22:55 A managed resource "aws_vpc" "production_vpc" has not been declared in
      14:22:55 prod_subnets.
  </code></pre>
  
  <p>can anyone please help ?</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-04-03T12:26:16.487Z
Comments: This error also coming while giving inside module 
  17:54:27  + terraform plan -out=tfplan -input=false
  17:54:28  [31m
  17:54:28  [1m[31mError: [0m[0m[1mUnsupported argument[0m
  17:54:28  
  17:54:28  [0m  on main.tf line 14, in module "ec2":
  17:54:28    14:   [4mvpc_id[0m = "${module.vpc.production_vpc}"
  17:54:28  [0m
  17:54:28  An argument named "vpc_id" is not expected here.
Comments: PRASANTHMV
Tags: <terraform><aws-vpc>
Title: How to get VPC id in Terraform module?
UserDisplayName: PRASANTHMV
ThreadText: # How to get VPC id in Terraform module?
  
  (Asked by: username_0 on 2020-04-03)
  
  I have a module structure like
  
  ```
  module "vpc" {
  source = "./modules/vpc"
  }
  
  module "prod_subnets" {
  source = "./modules/vpc/modules/subnets/production"
  }
  
  ```
  
  when running am getting an error like
  
  ```
  [0m on modules/vpc/modules/subnets/production/production.tf line 
  
      162, in resource "aws_subnet" "prod-pub-1b":
      14:22:55 162: vpc_id = [4maws_vpc.production_vpc[0m.id
      14:22:55 [0m
      14:22:55 A managed resource "aws_vpc" "production_vpc" has not been declared in
      14:22:55 prod_subnets.
  
  ```
  
  can anyone please help ?
  
  **Tags:** terraform, aws-vpc
  
  > **Comments:**
  > **username_0**: This error also coming while giving inside module  17:54:27  + terraform plan -out=tfplan -input=false 17:54:28  [31m 17:54:28  [1m[31mError: [0m[0m[1mUnsupported argument[0m 17:54:28   17:54:28  [0m  on main.tf line 14, in module "ec2": 17:54:28    14:   [4mvpc_id[0m = "${module.vpc.production_vpc}" 17:54:28  [0m 17:54:28  An argument named "vpc_id" is not expected here.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-04-03. Score: 3
  
  Without seeing the contents of your two modules I'm guessing a bit, but it looks like you have an AWS VPC declared in your `vpc` module and some subnets declared in your `prod_subnets` module and you are asking how the configuration of the subnets can get access to the VPC ID.
  
  If so, the answer is that the `vpc` module must export the VPC ID as an [output value](https://www.terraform.io/docs/configuration/outputs.html) and then the `prod_subnets` module must accept the VPC ID as an [input variable](https://www.terraform.io/docs/configuration/variables.html).
  
  In your `vpc` module, you can declare a `vpc_id` output value like this, for example in a file `modules/vpc/outputs.tf`:
  
  ```
  output "vpc_id" {
    value = aws_vpc.production_vpc
  }
  
  ```
  
  In your `prod_subnets` module you can declare a `vpc_id` input variable, for example in a file `modules/vpc/modules/subnets/production/outputs.tf`:
  
  ```
  variable "vpc_id" {
    type = string
  }
  
  ```
  
  Then in your existing `modules/vpc/modules/subnets/production/production.tf` file, on line 162, change the `vpc_id` argument for the subnet to refer to that variable:
  
  ```
    vpc_id = var.vpc_id
  
  ```
  
  Finally, you must then edit the top-level file whose source code you shared in your question to pass the value between the two modules, like this:
  
  ```
  module "vpc" {
    source = "./modules/vpc"
  }
  
  module "prod_subnets" {
    source = "./modules/vpc/modules/subnets/production"
  
    vpc_id = module.vpc.vpc_id
  }
  
  ```
  
  `module.vpc.vpc_id` means to take the value of the `vpc_id` output value from the `vpc` module.
  
  
  ---
  
  ### Answer by username_2 on 2021-03-25. Score: 1
  
  It should be written as:
  
  ```
  output "vpc_id" {
    value = aws_vpc.production_vpc.id
  }
  
  ```
  
  
  ---
  
  