2

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 ?

1
  • 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.
    – PRASANTHMV
    Apr 3, 2020 at 12:26

2 Answers 2

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 and then the prod_subnets module must accept the VPC ID as an input variable.

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.

1

It should be written as:

output "vpc_id" {
  value = aws_vpc.production_vpc.id
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.