Search code examples
terraformterraform-provider-aws

Error: element types must all match for conversion to list terraform


I have a terraform module with below variable.tf. I am creating a module for launch template which will have options for instance type and instance attributes.

variables.tf

variable "ec2_launch_template" {
  type = list(object({
    name                     = string
    image_id                 = string
    instance_type            = optional(string)
    key_name                 = string
    lt_security_group_names = list(string)
    user_data = string
    default_version = number
    iam_instance_profile_arn = string
    core_count = optional(number)
    threads_per_core = optional(number)
    instance_requirements = optional(list(object({
            cpu_max = optional(number)
            cpu_min = optional(number)
            memory_max = optional(number)
            memory_min = optional(number)
    })))
    ebs_volume = optional(list(object({
      device_name = optional(string)

            volume_type = optional(string)
            volume_size = optional(number)
            snapshot_id  = optional(string)
            kms_key_arn = optional(string)
            encrypted = optional(bool)
            ebs_block_delete_on_termination = optional(bool)
            iops     = optional(number)
            throughput  = optional(number)
    })))

    tags                     = map(string)
  }))
  description = "EC2 launch template to create, with their properties"
  default     = []
}

The .tfvars I am using is below

.tfvars

ec2_launch_template = [
  {
    "core_count": "3",
    "default_version": "1",
    "ebs_volume": [],
    "iam_instance_profile_arn": "arn:aws:iam::114833826347:instance-profile/testing-ec2-role-manoj",
    "image_id": "ami-05637405f4b0773e4",
    "instance_requirements": [],
    "instance_type": "t2.medium",
    "key_name": "RDSTEST",
    "lt_security_group_names": [
      "RDP"
    ],
    "name": "testtemplate6",
    "tags": {
      "Name": "testtemplate6"
    },
    "threads_per_core": "2",
    "user_data": "testdata.sh"
  },
  {
    "core_count": "4",
    "default_version": "1",
    "ebs_volume": [
      {
        "device_name": "/dev/sdb",
        "ebs_block_delete_on_termination": false,
        "encrypted": true,
        "iops": "10000",
        "kms_key_arn": "arn:aws:kms:us-east-1:114833826347:key/7ed65523-f8ec-40f1-889e-ee65f7e58172",
        "snasphot_id": "snap-00a70b80c54bb6af5",
        "throughput": "125",
        "volume_size": "50",
        "volume_type": "gp3"
      }
    ],
    "iam_instance_profile_arn": "arn:aws:iam::114833826347:instance-profile/testing-ec2-role-manoj",
    "image_id": "ami-05637405f4b0773e4",
    "instance_requirements": [
      {
        "cpu_max": "3",
        "cpu_min": "2",
        "memory_max": "80",
        "memory_min": "20"
      }
    ],
    "instance_type": null,
    "key_name": "RDSTEST",
    "lt_security_group_names": [
      "RDP"
    ],
    "name": "testtemplate5",
    "tags": {
      "Name": "testtemplate5"
    },
    "threads_per_core": "2",
    "user_data": "testdata.sh"
  }
]

We are getting below error:

element types must all match for conversion to list.

In order to debug, we tried with variable type as any. It's still giving the same issue. Any help is much appreciated.


Solution

  • We were defining variables which were not needed. Once we commented those it worked.