简体   繁体   English

您如何正确设置 Terraform 以使用跨账户 IAM 承担角色身份验证来管理 AWS EKS 集群?

[英]How do you properly setup Terraform to manage an AWS EKS cluster using cross-account IAM assume role authentication?

We're in the process of setting up some green field AWS infrastructure.我们正在建立一些新的 AWS 基础设施。

We have at the organisation account level an IAM user that Terraform authenticates as using access keys.我们在组织帐户级别有一个 IAM 用户,Terraform 使用访问密钥进行身份验证。 We've then setup our Terraform code to "assume role" into our sub accounts within their respective git repos (we have 1 git repo per account).然后,我们将 Terraform 代码设置为“承担角色”到我们各自 git 存储库中的子帐户(每个帐户有 1 个 git 存储库)。 Something like,就像是,

provider "aws" {
  assume_role {
    role_arn = "arn:aws:iam::XXXXXXXXXX:role/TerraformCloudRole"
  }
}

We're running into issues setting up an EKS cluster using the terraform-aws-modules/eks/aws module.我们在使用terraform-aws-modules/eks/aws模块设置 EKS 集群时遇到问题。 The cluster creates fine, but we've set manage_aws_auth_configmap = true so we can provide IAM roles/users and manage what they can authenticate against.集群创建良好,但我们设置了manage_aws_auth_configmap = true ,因此我们可以提供 IAM 角色/用户并管理他们可以进行身份验证的内容。 We're actually seeing multiple errors depending on where we do creates or updates, and some subtle changes to the code.我们实际上看到了多个错误,具体取决于我们创建或更新的位置,以及对代码的一些细微更改。 Essentially they are,本质上,它们是,

Error: The configmap "aws-auth" does not exist
with module.eks_main.module.eks.kubernetes_config_map_v1_data.aws_auth[0]
on .terraform/modules/eks_main.eks/main.tf line 470, in resource "kubernetes_config_map_v1_data" "aws_auth":

Or或者

Error: Get "http://localhost/api/v1/namespaces/kube-system/configmaps/aws-auth": dial tcp [::1]:80: connect: connection refused
with module.eks_main.module.eks.kubernetes_config_map_v1_data.aws_auth[0]
on .terraform/modules/eks_main.eks/main.tf line 470, in resource "kubernetes_config_map_v1_data" "aws_auth":

We did some Googling and we found this issue .我们做了一些谷歌搜索,我们发现了这个问题 We added a provider and this seemed to solve some issues, specifically using this approach .我们添加了一个提供者,这似乎解决了一些问题,特别是使用这种方法 The reason was because the exec route didn't work for us.原因是exec路线对我们不起作用。 It appeared to be trying to execute the AWS CLI command using the base access keys and not the assumed role.它似乎正在尝试使用基本访问密钥而不是假定角色来执行 AWS CLI 命令。 But the errors are back when we're making updates to the cluster or trying to run a destroy.但是当我们对集群进行更新或尝试运行破坏时,错误又回来了。 It doesn't appear to be picking up the provider for some reason, The latter error above is during the plan phase.由于某种原因,它似乎没有选择提供者,上面的后一个错误是在计划阶段。 not apply.不适用。

So to my question .所以我的问题 How do we setup Terraform to connect to/manage an EKS cluster properly when AWS assume-role/cross-account is involed?当涉及 AWS 承担角色/跨账户时,我们如何设置 Terraform 以正确连接/管理 EKS 集群?

maybe help:也许有帮助:

provider "aws" {
  alias  = "main"
  region = var.aws_region
}

data "aws_eks_cluster" "selected" {
  provider = aws.main
  name     = local.eks_cluster_name
}

data "aws_eks_cluster_auth" "selected" {
  provider = aws.main
  name     = local.eks_cluster_name
}

provider "kubernetes" {
  host                   = element(concat(data.aws_eks_cluster.selected[*].endpoint, tolist([""])), 0)
  cluster_ca_certificate = base64decode(element(concat(data.aws_eks_cluster.selected[*].certificate_authority.0.data, tolist([""])), 0))
  exec {
    api_version = "client.authentication.k8s.io/v1alpha1"
    args        = ["token", "-i", element(concat(data.aws_eks_cluster.selected[*].id, tolist([""])), 0)]
    command     = "aws-iam-authenticator"
  }
}

provider "helm" {
  kubernetes {
    host                   = element(concat(data.aws_eks_cluster.selected[*].endpoint, tolist([""])), 0)
    cluster_ca_certificate = base64decode(element(concat(data.aws_eks_cluster.selected[*].certificate_authority.0.data, tolist([""])), 0))
    exec {
      api_version = "client.authentication.k8s.io/v1alpha1"
      args        = ["token", "-i", element(concat(data.aws_eks_cluster.selected[*].id, tolist([""])), 0)]
      command     = "aws-iam-authenticator"
    }
  }
}

I'm not sure this will up to date, but I believe this should help you a lot, you can change the command to tho, as it use aws-iam-authenticator binary我不确定这是否是最新的,但我相信这对您有很大帮助,您可以将命令更改为 tho,因为它使用aws-iam-authenticator二进制文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM