1
votes

I am trying to pass a variable from the root module to a child module with the following syntax and i'm unable to do that:

└── Terraform
            ├── main.tf
            ├── variable.tf
    └── module
                ├──main.tf
                ├── variable.tf

Terraform Version:

Terraform v0.11.11
+ provider.openstack v1.15.0

Terraform Configuration Files /Terraform/main.tf:

provider "openstack" {
openstack_user_name         = "${var.openstack_user_name}"
openstack_tenant_name   = "${var.openstack_tenant_name}"
openstack_password      = "${var.openstack_password}"
openstack_auth_url          = "${var.openstack_auth_url}"
domain_name             = "${var.domain_name}"
}
module "testMod" {
name                    = "${var.name}"
imageId                     = "${var.imageId}"
flavor_name                 = "${var.flavor_name}"
openstack_keypair   = "${var.openstack_keypair}"
tenant_network_id           = "${var.tenant_network_id}"
tenant_network      = "${var.tenant_network}"
source = "./modules"
}

/Terraform/variable.tf:

variable "name" {default = "XXX"}
variable "imageId" {default = "11-22-33"}
variable "flavor_name"{default = "flavor"}
...

/Terraform/modules/main.tf:

resource "openstack_compute_instance_v2" "test" {
 name               = "${var.name}"
imageId                 = "${var.imageId}"
flavor_name             = "${var.flavor_name}"
openstack_keypair       = "${var.openstack_keypair}"
security_groups     =   ["default"]

network {
tenant_network_id       = "${var.tenant_network_id}"
tenant_network      = "${var.tenant_network}"
    }
}

/Terraform/modules/variable.tf:

variable "name" {}
variable "imageId" {}
variable "flavor_name" {}
variable "openstack_keypair" {}
variable "tenant_network_id"{}
variable "tenant_network" {}

Actual Behavior

Error: module.testMod.openstack_compute_instance_v2.test: : invalid or unknown key: imageId

Steps to Reproduce

  1. terraform init
  2. terraform apply

Unsure what is going wrong here

1

1 Answers

2
votes

The error is alerting you to the unknown keyimageId. This message is accurate as, in fact, the key should be image_id. You can check the Terraform openstack_compute_instance_v2 resource documentation and note the presence of the image_id argument.

Your code would then look like:

resource "openstack_compute_instance_v2" "test" {
  name              = "${var.name}"
  image_id          = "${var.imageId}"
  flavor_name       = "${var.flavor_name}"
  openstack_keypair = "${var.openstack_keypair}"
  security_groups   = ["default"]
}