auth.tf 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. data "aws_eks_cluster_auth" "this" {
  2. name = module.eks.cluster_id
  3. }
  4. data "aws_caller_identity" "current" {}
  5. locals {
  6. kubeconfig = yamlencode({
  7. apiVersion = "v1"
  8. kind = "Config"
  9. current-context = "terraform"
  10. clusters = [{
  11. name = module.eks.cluster_id
  12. cluster = {
  13. certificate-authority-data = module.eks.cluster_certificate_authority_data
  14. server = module.eks.cluster_endpoint
  15. }
  16. }]
  17. contexts = [{
  18. name = "terraform"
  19. context = {
  20. cluster = module.eks.cluster_id
  21. user = "terraform"
  22. }
  23. }]
  24. users = [{
  25. name = "terraform"
  26. user = {
  27. token = data.aws_eks_cluster_auth.this.token
  28. }
  29. }]
  30. })
  31. # we have to allow the root account to access the api
  32. aws_auth_configmap_yaml = <<-EOT
  33. ${chomp(module.eks.aws_auth_configmap_yaml)}
  34. - rolearn: arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/admin
  35. username: system:aws:root
  36. groups:
  37. - system:masters
  38. EOT
  39. }
  40. resource "null_resource" "patch_cm" {
  41. triggers = {
  42. kubeconfig = base64encode(local.kubeconfig)
  43. cmd_patch = <<-EOT
  44. kubectl patch configmap/aws-auth --patch "${local.aws_auth_configmap_yaml}" -n kube-system --kubeconfig <(echo $KUBECONFIG | base64 --decode)
  45. EOT
  46. }
  47. provisioner "local-exec" {
  48. interpreter = ["/bin/bash", "-c"]
  49. environment = {
  50. KUBECONFIG = self.triggers.kubeconfig
  51. }
  52. command = self.triggers.cmd_patch
  53. }
  54. }