iam module creates AWS IAM roles that can be assumed by Kubernetes service accounts through OIDC (OpenID Connect) federation. This enables Kubernetes pods to securely access AWS resources without managing long-lived credentials.
Overview
This module:- Creates an IAM role that trusts your Kubernetes cluster’s OIDC provider
- Configures the trust policy to allow specific Kubernetes service accounts to assume the role
- Supports multiple namespaces for the same service account name
- Uses OIDC web identity federation for secure, short-lived credentials
Input Variables
Name of the Kubernetes service account and the generated IAM role. The same name will be used for both the Kubernetes service account and the AWS IAM role.
Set of Kubernetes namespace(s) where the service account exists. The IAM role will trust service accounts with the specified name in all listed namespaces.
URL of the Kubernetes OIDC issuer. This is typically the cluster’s OIDC provider URL (e.g.,
https://oidc.eks.us-east-1.amazonaws.com/id/EXAMPLE).ARN of the Kubernetes OIDC provider in AWS IAM. This is the federated identity provider that enables the trust relationship.
Outputs
The ID of the created IAM role.
The ARN of the created IAM role. Use this ARN to annotate Kubernetes service accounts and attach IAM policies.
Resources Provisioned
IAM Role
Trust Policy Document
- Allows the action
sts:AssumeRoleWithWebIdentity - Uses a
StringEqualscondition to verify the service account identity - Constructs the service account subject format:
system:serviceaccount:<namespace>:<service-account-name> - Supports multiple namespaces by creating a condition for each
Usage Example
Basic Usage
Kubernetes Service Account Annotation
After creating the IAM role, annotate your Kubernetes service account:Complete Example with Policy
How It Works
- OIDC Federation: The Kubernetes cluster has an OIDC provider registered in AWS IAM
- Service Account Token: When a pod uses a service account, Kubernetes issues a signed OIDC token
- AssumeRoleWithWebIdentity: The pod exchanges this token for AWS credentials by calling
sts:AssumeRoleWithWebIdentity - Trust Policy Validation: AWS validates the token signature and checks the trust policy conditions
- Temporary Credentials: If valid, AWS issues temporary credentials for the pod to use
Dependencies
This module requires:- AWS provider configured
- EKS cluster with OIDC provider enabled
- OIDC provider registered in AWS IAM
Notes
- The role name must match the Kubernetes service account name for the IRSA (IAM Roles for Service Accounts) pattern to work correctly
- Service accounts in any of the specified namespaces can assume this role
- The trust policy uses
StringEqualsfor exact matching of the service account identity - All resources are tagged with
created-by = "terraform"for tracking