2
votes

I'm trying to use terraform to create some aws resource. Here is my issue:

I'm creating some variables, so I can access them from the resources. Here is the variables.tf file content:

variables.tf

variable "zones" {
  type = "list"
  default = ["us-east-1a", "us-east-1b"]
}

variable "init" {
    type = object({
                    vpc-id=list(string),
                    public-subnet=string, 
                    aws_region=string, 
                    ami=string,
                    vpc-sec-group= list(string)
            })

    param = {
        vpc-id = ["vpc-1111111"]
        public-subnet = "subnet-98e4567"
        aws_region = "${element(var.zones,0)}"
        ami = "ami-09d95fab7fff3776c",
        vpc-sec-group = ["sg-d60bf3f5"]
    }
}


variable "instances" {
    type = list(object({ type=string, count=string, tags=map(string) }))
    t2-micro ={
        type = "t2.micro"
        count = 4
        tags = { Name = "Test T2"}
    }
    m4-large ={
        type = "m4-large"
        count = 2
        tags = { Name = "Test M4"}
    }
}

My plan is to use these variable to create some ec2 instances as so Here is ec2.tf

ec2.tf

resource "aws_instance" "Test-T2" {
    type   = lookup(var.insts["t2-micro"],"type")
    ami = lookup(var.init.ami["ami"],var.init.aws_region["aws_region"] )
    count  = lookup(var.insts["t2-micro"],"count")
    tags = lookup(var.insts["t2-micro"],"tags")
    key_name = aws_key_pair.terraform-demo.key_name
    vpc_security_group_ids = toset(lookup(var.init, "vpc-sec-group"))
    subnet_id = lookup(var.init.params["public-subnet"])
}

ISSUE

When I execute

terraform init

I get the following error:

Error: Unsupported argument

  on variables.tf line 26, in variable "instances":
  26:     t2-micro ={

An argument named "t2-micro" is not expected here.


Error: Unsupported argument

  on variables.tf line 32, in variable "instances":
  32:     m4-large ={

An argument named "m4-large" is not expected here.


Terraform has initialized, but configuration upgrades may be needed.

Terraform found syntax errors in the configuration that prevented full
initialization. If you've recently upgraded to Terraform v0.12, this may be
because your configuration uses syntax constructs that are no longer valid,
and so must be updated before full initialization is possible.

Could someone please help me to correct those errors?

More details and some actions I have taken

I have tried different ways of creating the variables to the best of my knowledge and following the Terraform documentation to no avail.

I'm just emulating what would be a Python:

  • list of objects( list of dictionaries) and
  • an object

Here is my version of terraform

terraform  -v                                                        
Terraform v0.12.26
+ provider.aws v2.65.0

some more details

I'm using the latest version of Visual Studio Code 1.45.1 with the Terraform module HashiCop 1.4.0 for "Syntax highlighting, linting, formatting, and validation for Hashicorp's Terraform"

1

1 Answers

2
votes

You need to separate your variable declarations and assignments. Those cannot co-exist within the same block.

Your declaration within the variables.tf would look like (with some fixes and cleanup):

variable "instances" {
  type = list(object({
    type  = string # removed commas since you specified object type
    count = number # fixed from string type
    tags  = map(string)
  }))
}

Your variable assignment should be moved to a .tfvars file. Customarily this file would be named terraform.tfvars:

instances = [ # you specified a list so we add the proper syntax here
  { # you specified an object, so we remove the keys and retain the values
    type  = "t2.micro"
    count = 4
    tags  = { "Name" = "Test T2"} # you specified map(string) so Name becomes string
  },
  {
    type  = "m4-large"
    count = 2
    tags  = { "Name" = "Test M4"}
  }
}]

and that will be a valid input variable with proper assignment given your declaration. That will fix your error.