I'm in the process of converting a legacy aws setup into Terraform and have the following:

  - main.tf
    - main.tf

In ci I have an aws security group which I want to reference in my submodules. Currently I have:

vpc_security_group_ids      = ["${aws_security_group.vpc-idhere.id}"]

Which errors with:

A managed resource "aws_security_group" "vpc-idhere" has not been declared in ci-workers.

The security group is defined in ci/main.tf

How do I correctly reference a resource in a sub module?


When calling across module boundaries you need to use variables/outputs to pass references to resources; modules can't peek inside other modules.

So your ci-workers module will need to declare an input variable such as:

variable "vpc_security_group_ids" {
  description = "List of VPC security group IDs"
  type        = list

You then use ${var.vpc_security_group_ids} within the module. Then when you instantiate this module, you can pass in the IDs from the parent:

module "ci-workers" {
  source                 = "..."
  vpc_security_group_ids = ["${aws_security_group.vpc-idhere.id}"]

See the module documentation for more information.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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