0
votes

I am getting below error when I am trying to do use data block for AMI's:-

Error: module.ec2-wf.var.instance_type: variable instance_type in module ec2-wf should be type string, got map Error: module.ec2-wf.var.ami: variable ami in module ec2-wf should be type string, got map make: *** [validate] Error 1

Below is my terraform structure:-

project
    modules
        app1
        app2
        app3
        common
            global-variables.tf
            main.tf
            makefile
            provider.tf
            vpc.tf
        global
            acm
            alb
            asg
            ec2
            efs
            lc
            rds
            redis
            subapp
                ec2
                    main.tf
                    makefile
                    provider.tf
                    variable.tf

project/modules/global/subapp/ec2/main.tf

module "ec2-wf" {
    source = "../../../global/ec2"

    name                    =   "${var.name}"
    db_remote_state_bucket  =   "s3-terraform-state"
    db_remote_state_key     =   "subapp/ec2/terraform.tfstate"
    key_name                =   "${lookup(var.key_name, terraform.workspace)}"
#    ami                     =   "${lookup(var.ami, terraform.workspace)}"
#    instance_type           =   "${lookup(var.instance_type, terraform.workspace)}"
    ami                     =    "${var.ami}"
    instance_type           =    "${var.instance_type}"

    tags = {
        Name        =   "${var.project}"
        Environment =   "${lookup(var.env, terraform.workspace)}"
    }
}

project/modules/global/ec2/variables.tf

variable "instance_type" {
    description = "This describes the  Map the environment whether it is dev/test/prd etc"
}

variable "ami" {
    description = "This describes the  Map of Availability Zones to deploy"
    default     =   ""
}
1
What are the values passed in for those variables? The error is inconsistent with the default value provided for one and lack of default value provided for the other.Matt Schuchard

1 Answers

0
votes

It looks fine to me. Potentially it looks like it is looking at the commented lines for some reason? As the uncommented versions look fine.

#    ami                     =   "${lookup(var.ami, terraform.workspace)}"
#    instance_type           =   "${lookup(var.instance_type, terraform.workspace)}"

To make sure, you could always specify the type (like below). If that doesn't work, delete the commented lines and see if it still happens.

variable "ami" {
  type = string
}