GitLab CI中AWS/EKS Terraform配置出现不支持参数错误求助
问题
按照GitLab教程使用Terraform及示例项目在EKS中部署CI Runner,克隆仓库后执行以下命令:
terraform fmt terraform init -upgrade
同时修改backend.tf,将后端配置改为local:
terraform { backend "local" { } }
执行gitlab-terraform validate时触发错误,提示eks模块中的变量不符合预期。
当前eks.tf配置如下:
// eks.tf module "eks" { source = "terraform-aws-modules/eks/aws" version = "18.1.0" cluster_name = var.cluster_name cluster_version = var.cluster_version vpc_id = module.vpc.vpc_id subnet_ids = module.vpc.private_subnets eks_managed_node_groups = { default = { min_size = 1 max_size = var.instance_count desired_size = var.instance_count instance_types = [var.instance_type] } } }
错误输出:
$ gitlab-terraform validate Initializing modules... Initializing provider plugins... - Reusing previous version of hashicorp/helm from the dependency lock file - Reusing previous version of hashicorp/aws from the dependency lock file - Reusing previous version of hashicorp/kubernetes from the dependency lock file - Installing hashicorp/kubernetes v2.22.0... - Installed hashicorp/kubernetes v2.22.0 (signed by HashiCorp) - Installing hashicorp/helm v2.10.1... - Installed hashicorp/helm v2.10.1 (signed by HashiCorp) - Installing hashicorp/aws v5.8.0... - Installed hashicorp/aws v5.8.0 (signed by HashiCorp) Terraform has made some changes to the provider dependency selections recorded in the .terraform.lock.hcl file. Review those changes and commit them to your version control system if they represent changes you intended to make. Terraform has been successfully initialized! ╷ │ Error: Unsupported argument │ │ on eks.tf line 5, in module "eks": │ 5: cluster_name = var.cluster_name │ │ An argument named "cluster_name" is not expected here. ╵ ╷ │ Error: Unsupported argument │ │ on eks.tf line 6, in module "eks": │ 6: cluster_version = var.cluster_version │ │ An argument named "cluster_version" is not expected here. ╵ ╷ │ Error: Unsupported argument │ │ on eks.tf line 8, in module "eks": │ 8: vpc_id = module.vpc.vpc_id │ │ An argument named "vpc_id" is not expected here. ╵ ╷ │ Error: Unsupported argument │ │ on eks.tf line 9, in module "eks": │ 9: subnet_ids = module.vpc.private_subnets │ │ An argument named "subnet_ids" is not expected here. ╵ ╷ │ Error: Unsupported argument │ │ on eks.tf line 11, in module "eks": │ 11: eks_managed_node_groups = { │ │ An argument named "eks_managed_node_groups" is not expected here. ╵ Cleaning up project directory and file based variables 00:00 ERROR: Job failed: exit code 1
项目目录结构:
. ├── README.md ├── agent.tf ├── backend.tf ├── data.tf ├── eks.tf ├── outputs.tf ├── providers.tf ├── variables.tf ├── versions.tf └── vpc.tf
解决方法
错误根源是使用的terraform-aws-modules/eks/aws模块版本为18.1.0,该版本的参数结构与旧版本差异显著,当前配置沿用了旧版写法,导致参数不被识别。需按照v18.1.0的模块规范修改eks.tf:
修改后的配置如下:
// eks.tf module "eks" { source = "terraform-aws-modules/eks/aws" version = "18.1.0" cluster = { name = var.cluster_name version = var.cluster_version } vpc_config = { vpc_id = module.vpc.vpc_id subnet_ids = module.vpc.private_subnets } managed_node_groups = { default = { min_size = 1 max_size = var.instance_count desired_size = var.instance_count instance_types = [var.instance_type] } } }
核心修改点:
- 原顶层参数
cluster_name、cluster_version需嵌套在cluster块内 - 原顶层参数
vpc_id、subnet_ids需嵌套在vpc_config块内 - 原参数
eks_managed_node_groups更名为managed_node_groups,作为顶层参数使用
修改完成后重新执行gitlab-terraform validate即可通过校验。
内容的提问来源于stack exchange,提问作者3therk1ll
相关产品推荐
相关产品推荐

