Module dependencies

I have some base modules I want to reuse for other modules:

modules/
├── aws-ec2-instance/
└── null-label/

Then I have another module called “frontend” where I’m using both the aws-ec2-instance and null-label modules

envs/_common
├── services
    └── frontend
        ├── main.tf
        └── vars.tf

Here’s the main.tf:

module "label" {
  source     = "git::ssh://git@github.com/myrepo/project.git//modules/null-label?ref=master"
  namespace  = "${var.namespace}"
  stage      = "${var.stage}"
  name       = "${var.name}"
  delimiter  = "${var.delimiter}"
  attributes = "${var.attributes}"
  tags       = "${var.tags}"
  enabled    = "${var.enabled}"
}

module "frontend" {
  source                 = "git::ssh://git@github.com/myrepo/project.git//modules/aws-ec2-instance?ref=master"
  ami                    = "${var.ami}"
  instance_type          = "${var.instance_type}"
  name                   = "${module.label.id}"
  subnet_id              = "${var.subnet_id}"
  vpc_security_group_ids = "${var.vpc_security_group_ids}"
  tags                   = "${module.label.tags}"
}

and the vars.tf:

variable "name" {
  description = "The Name of the application or solution  (e.g. `bastion` or `portal`)"
	name = "frontend"
}

variable "namespace" {
  description = "Namespace (e.g. `eg` or `cp`)"
	namespace = "company"
}

variable "stage" {
  description = "Stage (e.g. `prod`, `dev`, `staging`)"
}


variable "delimiter" {
  type        = "string"
  default     = "-"
  description = "Delimiter to be used between `name`, `namespace`, `stage` and `attributes`"
}

variable "attributes" {
  type        = "list"
  default     = []
  description = "Additional attributes (e.g. `1`)"
}

variable "tags" {
  type        = "map"
  default     = {}
  description = "Additional tags (e.g. map('BusinessUnit','XYZ')"
}

variable "enabled" {
  default     = "true"
  description = "Set to false to prevent the module from creating or accessing any resources"
}

variable "ami" {
    default = "ami-xxxxxxx"
}
variable "instance_type" {
    default = "t2.micro"
}
variable "subnet_id" {
    default = "subnet-bxxxxx"
}
variable "vpc_security_group_ids" {
  default = ["sg-dxxxxx", "sg-4xxxxx"]
}

I get the desired result when I run terraform init and terraform plan in this directory. Now, I want to use this module inside terragrunt:

terragrunt = {
    terraform = {
        source = "git::ssh://git@github.com/myrepo/project.git//envs/_common/services/frontend?ref=master"
    }
    include = {
        path = "${find_in_parent_folders()}"
    }
}
name = "frontend"
tags = "{ Something = 'Something' }"

and it spits out this error message:

[terragrunt] [/home/user/dev/project/terraform-v2/envs/dev/services/frontend] 2018/10/14 15:22:01 Running command: terraform init -backend-config=key=services/frontend/terraform.tfstate -backend-config=region=us-west-1 -backend-config=encrypt=true -backend-config=dynamodb_table=tf-lock-table -backend-config=bucket=project
Initializing modules...
- module.label
  Getting source "git::ssh://git@github.com/myrepo/project.git//modules/null-label?ref=master"
- module.frontend
  Getting source "git::ssh://git@git@github.com/myrepo/project.git//modules/aws-ec2-instance?ref=master"

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Error: module 'label': unknown variable referenced: 'delimiter'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'attributes'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'tags'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'enabled'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'namespace'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'stage'; define it with a 'variable' block

Error: module 'label': unknown variable referenced: 'name'; define it with a 'variable' block

Error: module 'frontend': unknown variable referenced: 'subnet_id'; define it with a 'variable' block

Error: module 'frontend': unknown variable referenced: 'vpc_security_group_ids'; define it with a 'variable' block

Error: module 'frontend': unknown variable referenced: 'ami'; define it with a 'variable' block

Error: module 'frontend': unknown variable referenced: 'instance_type'; define it with a 'variable' block

I’m wondering why the variables would be unknown since they’ve all been declared in the module’s vars.tf file. Would appreciate if someone point me to the right direction.

Thanks in advance!

Can you show the full terragrunt command you’re running and the full log output? It would help to see what exactly Terragrunt sees as your source URL and what it downloads. You can also use that to browse the .terragrunt-cache directory locally for debugging.

Also, do you have any other files in the folder where you are running Terragrunt (i.e., the folder where the terraform.tfvars file lives)?

1 Like

Hi @jim, figured it out a few minutes after I posted this. I was referencing the aws and backend stuff from a symlinked file in the directory where I have terraform.tfvars. Removed the symlink with just the terraform.tfvars in the folder and it worked.