Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconeks-alb-ingress-controller-iam-policy
file-type-iconeks-alb-ingress-controller
file-type-iconeks-aws-auth-merger
file-type-iconeks-cloudwatch-container-logs
file-type-iconeks-cluster-control-plane
file-type-iconeks-cluster-managed-workers
file-type-iconeks-cluster-workers-cross-access
file-type-iconeks-cluster-workers
file-type-iconeks-container-logs
file-type-iconeks-iam-role-assume-role-policy-for-servic...
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconeks-k8s-cluster-autoscaler-iam-policy
file-type-iconeks-k8s-cluster-autoscaler
file-type-iconeks-k8s-external-dns-iam-policy
file-type-iconeks-k8s-external-dns
file-type-iconeks-k8s-role-mapping
file-type-iconeks-scripts
file-type-iconeks-vpc-tags
file-type-iconrfc
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconCONTRIBUTING.md
file-type-iconGRUNTWORK_PHILOSOPHY.md
file-type-iconLICENSE.md
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconsetup.cfg

Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconeks-alb-ingress-controller-iam-policy
file-type-iconeks-alb-ingress-controller
file-type-iconeks-aws-auth-merger
file-type-iconeks-cloudwatch-container-logs
file-type-iconeks-cluster-control-plane
file-type-iconeks-cluster-managed-workers
file-type-iconeks-cluster-workers-cross-access
file-type-iconeks-cluster-workers
file-type-iconeks-container-logs
file-type-iconeks-iam-role-assume-role-policy-for-servic...
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconeks-k8s-cluster-autoscaler-iam-policy
file-type-iconeks-k8s-cluster-autoscaler
file-type-iconeks-k8s-external-dns-iam-policy
file-type-iconeks-k8s-external-dns
file-type-iconeks-k8s-role-mapping
file-type-iconeks-scripts
file-type-iconeks-vpc-tags
file-type-iconrfc
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconCONTRIBUTING.md
file-type-iconGRUNTWORK_PHILOSOPHY.md
file-type-iconLICENSE.md
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconsetup.cfg
EC2 Kubernetes Service (EKS) Cluster

EC2 Kubernetes Service (EKS) Cluster

Deploy a Kubernetes cluster on top of Amazon EC2 Kubernetes Service (EKS).

Code Preview

Preview the Code

mobile file icon

README.md

down

EKS IAM Role Assume Role Policy for Kubernetes Service Accounts

This Terraform module can be used to create Assume Role policies for IAM Roles such that they can be used with Kubernetes Service Accounts. This requires a compatible EKS cluster that supports the IAM Roles for Service Accounts feature.

See the corresponding section of the eks-cluster-control-plane module README for information on how to set up IRSA and how it works.

How do you use this module?

  • See the root README for instructions on using Terraform modules.
  • See variables.tf for all the variables you can set on this module.
  • See outputs.tf for all the variables that are outputed by this module.

This module is intended to be passed to the assume_role_policy input for an IAM role. For example:

module "assume_role_policy" {
  source = "git::git@github.com:gruntwork-io/terraform-aws-eks.git//modules/eks-iam-role-assume-role-policy-for-service-account?ref=v0.7.0"

  eks_openid_connect_provider_arn = module.eks_cluster.eks_iam_openid_connect_provider_arn
  eks_openid_connect_provider_url = module.eks_cluster.eks_iam_openid_connect_provider_url
  namespaces                      = ["default"]
  service_accounts                = []
}

resource "aws_iam_role" "example" {
  name               = "example-iam-role"
  assume_role_policy = module.assume_role_policy.assume_role_policy_json
}

The above example will configure the IAM role example-iam-role such that it is availble to be assumed by the EKS cluster provisioned in the eks_cluster module block (not shown). Note that we restrict it so that it can only be assumed by Service Accounts in the Namespace default. You can restrict it further to specific Service Accounts if you specify the service_accounts input variable.

If you want to allow additional Namespaces, append them to the namespaces input. For example, if you want to allow Service Accounts in either the default Namespace or kube-system Namespace:

module "assume_role_policy" {
  # Other parameters omitted for brevity

  namespaces = ["default", "kube-system"]
}

You can also restrict to specific Service Accounts. For example, to only allow the list-eks-clusters-sa Service Account in the default Namespace to assume the role:

module "assume_role_policy" {
  # Other parameters omitted for brevity

  namespaces       = []
  service_accounts = [{
    namespace = "default"
    name      = "list-eks-clusters-sa"
  }]
}

You can allow other Service Accounts as well by expanding the list.

If you wish to allow any Service Account in your cluster to assume the role, you can set both namespaces and service_accounts to an empty list.

Note that this module does not support specifying both namespaces and service_accounts at the same time. You must use one or the other.

Refer to the corresponding section of the eks-cluster-control-plane module README for information on how to use the IAM role in your Pods.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?