我有一个带有以下variable.tf的terraform模块。我正在为启动模板创建一个模块,其中包含实例类型和实例属性的选项。
变量.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 = []
}
我使用的.tfvars如下
.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"
}
]
我们收到以下错误:
元素类型必须全部匹配才能转换为列表。
为了调试,我们尝试将变量类型设置为any。它仍然给出同样的问题。非常感谢任何帮助。
在您的定义中,所有已声明为
optional(number)
的变量,它们的值都作为字符串传递,而不应该这样定义:
"core_count": 4,
对于类型为 number 的所有其他变量来说,这应该是相同的。
在您的 tfvars 文件中,snasphot_id 中有一个拼写错误,需要更改为 snapshot_id。
由于您没有提供发生错误的确切行,我认为这些是您的 tfvar 遇到的主要问题,并且您的输入应该看起来更像这样:
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",
"snapshot_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"
}
]