如何通过 terraform 编辑 aws-auth configmap?

发布于 2025-01-10 15:11:13 字数 171 浏览 0 评论 0原文

我正在使用 Kuberbetes 提供程序和 Terraform EKS 模块创建 eks 集群。问题是我使用 Terraform Enterprise 工作区来创建它,因此我无法从 IAM 角色编辑 aws configmap。如何通过 terraform 编辑配置映射,以便将所需的角色和用户添加到生成的身份验证配置映射中?

I'm creating an eks cluster using the Kuberbetes provider and the Terraform EKS module. The problem is that I am using a Terraform Enterprise workspace to create it, so I can't edit the aws configmap from my IAM role. How do I edit the configmap through terraform so that it adds the desired roles and users to the generated was auth configmap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2025-01-17 15:11:13

创建 eks 集群后应用身份验证。您可以按照以下步骤操作。
创建一个模板文件,其内容类似于变量文件中的值并传递值。 (我提供了一个非常高级别的示例,在下面声明资源/数据地形时,此处未分配一些值)

apiVersion: v1
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system
data:
  mapRoles: |
    ${indent(4, worker_roles_yaml)}
%{if iam_roles_yaml != "[]" }
    ${indent(4, iam_roles_yaml)}
%{ endif }
%{if iam_users_yaml != "[]" }
  mapUsers: |
    ${indent(4, iam_users_yaml)}
%{ endif }
%{if aws_accounts_yaml != "[]" }
  mapAccounts: |
    ${indent(4, aws_accounts_yaml)}
%{ endif }

使用上面的模板文件创建数据

data "template_file" "configmap_auth" {
 
  template = file(local.configmap_auth_template_file)
##pass all variables, i am giving samples only.
  vars = {
    iam_users_yaml=var.iam_users_yaml
    iam_users_yaml=var.iam_users_yaml
  }
}

,然后渲染文件

resource "local_file" "configmap_auth" {
  content  = join("", data.template_file.configmap_auth.*.rendered)
  filename = var.configmap_auth_file
}

创建具有触发器的 null_resource 并使用本地 exec 配置程序来应用身份验证文件

resource "null_resource" "apply_configmap_auth" {
  ##i am using cluster health/status and file content changes

  triggers = {
    cluster_updated                     = join("", aws_eks_cluster.default.*.id)
    worker_roles_updated                = var.worker_roles_yaml
    additional_roles_updated            = var.iam_roles_yaml
    additional_users_updated            = var.iam_users_yaml
    additional_aws_accounts_updated     = var.aws_accounts_yaml
    configmap_auth_file_content_changed = join("", var.configmap_auth.*.content)
    configmap_auth_file_id_changed      = join("", var.configmap_auth.*.id)
  }

depends_on = [aws_eks_cluster.default, local_file.configmap_auth]
     provisioner "local-exec" {
     command = <<EOT
      sleep 240
      set -e
aws eks update-kubeconfig --name= --region= --kubeconfig=abc
kubectl apply -f ${var.configmap_auth_file} --kubeconfig abc

}

}

Apply auth after eks cluster creation. you can follow below steps.
Create a template file having content like and pass value in your variable file. (I am providing a just sample at very high level, some values are not assigned here while declaring resources/data terraform below )

apiVersion: v1
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system
data:
  mapRoles: |
    ${indent(4, worker_roles_yaml)}
%{if iam_roles_yaml != "[]" }
    ${indent(4, iam_roles_yaml)}
%{ endif }
%{if iam_users_yaml != "[]" }
  mapUsers: |
    ${indent(4, iam_users_yaml)}
%{ endif }
%{if aws_accounts_yaml != "[]" }
  mapAccounts: |
    ${indent(4, aws_accounts_yaml)}
%{ endif }

Create data using above template file

data "template_file" "configmap_auth" {
 
  template = file(local.configmap_auth_template_file)
##pass all variables, i am giving samples only.
  vars = {
    iam_users_yaml=var.iam_users_yaml
    iam_users_yaml=var.iam_users_yaml
  }
}

then render file

resource "local_file" "configmap_auth" {
  content  = join("", data.template_file.configmap_auth.*.rendered)
  filename = var.configmap_auth_file
}

create null_resource having trigger and use local exec provisioner to apply auth file

resource "null_resource" "apply_configmap_auth" {
  ##i am using cluster health/status and file content changes

  triggers = {
    cluster_updated                     = join("", aws_eks_cluster.default.*.id)
    worker_roles_updated                = var.worker_roles_yaml
    additional_roles_updated            = var.iam_roles_yaml
    additional_users_updated            = var.iam_users_yaml
    additional_aws_accounts_updated     = var.aws_accounts_yaml
    configmap_auth_file_content_changed = join("", var.configmap_auth.*.content)
    configmap_auth_file_id_changed      = join("", var.configmap_auth.*.id)
  }

depends_on = [aws_eks_cluster.default, local_file.configmap_auth]
     provisioner "local-exec" {
     command = <<EOT
      sleep 240
      set -e
aws eks update-kubeconfig --name= --region= --kubeconfig=abc
kubectl apply -f ${var.configmap_auth_file} --kubeconfig abc

}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文