# 从失败到成功：一段精结的 EKS Access Entry API 调试历程

> 作者：zning（https://ning.ae） · 发布：2025/04/27 23:48 下午 (UTC+8) · 原文：https://zning.me/17457689291001.html

这是一段运营员们看起来很简单，实际运维中却漫长而重处失败和重来的调试历程。

最初，我需要将使用 Terragrunt 托管的 AWS EKS 集群，将访问权限管理模式从传统的 `aws-auth` ConfigMap 迁移到 Access Entry API。听起来不过是修改一行 Terraform 配置：将 `authentication_mode` 设为 `API_AND_CONFIG_MAP`。

真实情况说：我一开始就失败了。

## 首先是 Provider 版本问题

第一次 apply 的时候，直接报错：

```
Error: Unsupported argument
on main.tf line 9, in resource "aws_eks_cluster" "this":
9: authentication_mode = var.authentication_mode
An argument named "authentication_mode" is not expected here.
```

原因：没有把 `authentication_mode` 放到 `access_config` 块内！

> 教\u8训：单纯升级 Provider 版本是不够的，你的模块代码也要调整。

## 接着是 variables 未声明问题

调整了代码，拉起了 Terragrunt apply，结果继续报错：

```
Error: Reference to undeclared input variable
on main.tf line 192
for_each = { for entry in var.access_entries : entry.principal_arn => entry }
```

正确做法：在 variables.tf 中声明：

```hcl
variable "access_entries" {
  description = "List of IAM principal entries to add into EKS access management"
  type = list(object({
    principal_arn = string
    policy_arn    = string
    scope_type    = string
  }))
  default = []
}
```

> 教\u8训：每个 Terragrunt inputs 传输的参数，在 Terraform 模块里必须有声明。

## 然后是 for_each 对象错误

再 apply，又报：

```
Error: Unsupported attribute
on main.tf line 204
policy_arn = each.value.policy_arn
```

原因：将 for_each 写成了 aws_eks_access_entry.this，而 each.value 对象本身没有 policy_arn。

修正：

```hcl
resource "aws_eks_access_policy_association" "this" {
  for_each = { for entry in var.access_entries : entry.principal_arn => entry }
  ...
}
```

> 教\u8训：确保 for_each 的数据源正确，才能正确获取属性。

## 最后成功完成 Apply

最终，我看到了精致的 Plan：

- + create aws_eks_access_entry
- + create aws_eks_access_policy_association

并且用 AWS CLI 验证：

```bash
aws eks list-access-entries --cluster-name <cluster-name>
aws eks list-access-policies --cluster-name <cluster-name>
```

全部展示正常！

> 结论：这是一段从错误里练成巧的过程，也是对 EKS 访问权限管理理解进步深入的一次实战。

---

大家如果要从 aws-auth 迁移到 Access Entry，必须调整 Terraform 模块，谨慎检查，避免同样的坑！

下次，要起绘全部行为流程，而不是简单看文档！

你，学会了吗？🌟
