Help improve this page
To contribute to this user guide, choose the Edit this page on GitHub link that is located in the right pane of every page.
Assign IAM roles to Kubernetes service accounts
This topic covers how to configure a Kubernetes service account to assume an AWS Identity and Access Management (IAM) role. Any Pods that are configured to use the service account can then access any AWS service that the role has permissions to access.
Prerequisites
-
An existing cluster. If you don’t have one, you can create one by following one of the guides in Get started with Amazon EKS.
-
An existing IAM OpenID Connect (OIDC) provider for your cluster. To learn if you already have one or how to create one, see Create an IAM OIDC provider for your cluster.
-
Version
2.12.3or later or version1.27.160or later of the AWS Command Line Interface (AWS CLI) installed and configured on your device or AWS CloudShell. To check your current version, useaws --version | cut -d / -f2 | cut -d ' ' -f1. Package managers suchyum,apt-get, or Homebrew for macOS are often several versions behind the latest version of the AWS CLI. To install the latest version, see Installing and Quick configuration with aws configure in the AWS Command Line Interface User Guide. The AWS CLI version that is installed in AWS CloudShell might also be several versions behind the latest version. To update it, see Installing AWS CLI to your home directory in the AWS CloudShell User Guide. -
The
kubectlcommand line tool is installed on your device or AWS CloudShell. The version can be the same as or up to one minor version earlier or later than the Kubernetes version of your cluster. For example, if your cluster version is1.29, you can usekubectlversion1.28,1.29, or1.30with it. To install or upgradekubectl, see Set up kubectl and eksctl. -
An existing
kubectlconfigfile that contains your cluster configuration. To create akubectlconfigfile, see Connect kubectl to an EKS cluster by creating a kubeconfig file.
Step 1: Create IAM Policy
If you want to associate an existing IAM policy to your IAM role, skip to the next step.
-
Create an IAM policy. You can create your own policy, or copy an AWS managed policy that already grants some of the permissions that you need and customize it to your specific requirements. For more information, see Creating IAM policies in the IAM User Guide.
-
Create a file that includes the permissions for the AWS services that you want your Pods to access. For a list of all actions for all AWS services, see the Service Authorization Reference.
You can run the following command to create an example policy file that allows read-only access to an Amazon S3 bucket. You can optionally store configuration information or a bootstrap script in this bucket, and the containers in your Pod can read the file from the bucket and load it into your application. If you want to create this example policy, copy the following contents to your device. Replace
my-pod-secrets-bucketwith your bucket name and run the command.{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::my-pod-secrets-bucket" } ] } -
Create the IAM policy.
aws iam create-policy --policy-name my-policy --policy-document file://my-policy.json
Step 2: Create and associate IAM Role
Create an IAM role and associate it with a Kubernetes service account. You can use either eksctl or the AWS CLI.
Create and associate role (eksctl)
This eksctl command creates a Kubernetes service account in the specified namespace, creates an IAM role (if it doesn’t exist) with the specified name, attaches an existing IAM policy ARN to the role, and annotates the service account with the IAM role ARN. Be sure to replace the sample placeholder values in this command with your specific values. To install or update eksctl, see Installationeksctl documentation.
eksctl create iamserviceaccount --name my-service-account --namespace default --cluster my-cluster --role-name my-role \ --attach-policy-arn arn:aws:iam::111122223333:policy/my-policy --approve
Important
If the role or service account already exist, the previous command might fail. eksctl has different options that you can provide in those situations. For more information run eksctl create iamserviceaccount --help.
Create and associate role (AWS CLI)
If you have an existing Kubernetes service account that you want to assume an IAM role, then you can skip this step.
-
Create a Kubernetes service account. Copy the following contents to your device. Replace
my-service-accountwith your desired name anddefaultwith a different namespace, if necessary. If you changedefault, the namespace must already exist.cat >my-service-account.yaml <<EOF apiVersion: v1 kind: ServiceAccount metadata: name: my-service-account namespace: default EOF kubectl apply -f my-service-account.yaml -
Set your AWS account ID to an environment variable with the following command.
account_id=$(aws sts get-caller-identity --query "Account" --output text) -
Set your cluster’s OIDC identity provider to an environment variable with the following command. Replace
my-clusterwith the name of your cluster.oidc_provider=$(aws eks describe-cluster --name my-cluster --region $AWS_REGION --query "cluster.identity.oidc.issuer" --output text | sed -e "s/^https:\/\///") -
Set variables for the namespace and name of the service account. Replace
my-service-accountwith the Kubernetes service account that you want to assume the role. Replacedefaultwith the namespace of the service account.export namespace=default export service_account=my-service-account -
Run the following command to create a trust policy file for the IAM role. If you want to allow all service accounts within a namespace to use the role, then copy the following contents to your device. Replace
StringEqualswithStringLikeand replace$service_accountwith*. You can add multiple entries in theStringEqualsorStringLikeconditions to allow multiple service accounts or namespaces to assume the role. To allow roles from a different AWS account than the account that your cluster is in to assume the role, see Authenticate to another account with IRSA for more information.{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::123456789012:oidc-provider/$oidc_provider" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "$oidc_provider:aud": "sts.amazonaws.com", "$oidc_provider:sub": "system:serviceaccount:$namespace:$service_account" } } } ] } -
Create the role. Replace
my-rolewith a name for your IAM role, andmy-role-descriptionwith a description for your role.aws iam create-role --role-name my-role --assume-role-policy-document file://trust-relationship.json --description "my-role-description" -
Attach an IAM policy to your role. Replace
my-rolewith the name of your IAM role andmy-policywith the name of an existing policy that you created.aws iam attach-role-policy --role-name my-role --policy-arn=arn:aws:iam::$account_id:policy/my-policy -
Annotate your service account with the Amazon Resource Name (ARN) of the IAM role that you want the service account to assume. Replace
my-rolewith the name of your existing IAM role. Suppose that you allowed a role from a different AWS account than the account that your cluster is in to assume the role in a previous step. Then, make sure to specify the AWS account and role from the other account. For more information, see Authenticate to another account with IRSA.kubectl annotate serviceaccount -n $namespace $service_account eks.amazonaws.com/role-arn=arn:aws:iam::$account_id:role/my-role -
(Optional) Configure the AWS Security Token Service endpoint for a service account. AWS recommends using a regional AWS STS endpoint instead of the global endpoint. This reduces latency, provides built-in redundancy, and increases session token validity.
Step 3: Confirm configuration
-
Confirm that the IAM role’s trust policy is configured correctly.
aws iam get-role --role-name my-role --query Role.AssumeRolePolicyDocumentAn example output is as follows.
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::111122223333:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:sub": "system:serviceaccount:default:my-service-account", "oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud": "sts.amazonaws.com" } } } ] } -
Confirm that the policy that you attached to your role in a previous step is attached to the role.
aws iam list-attached-role-policies --role-name my-role --query "AttachedPolicies[].PolicyArn" --output textAn example output is as follows.
arn:aws:iam::111122223333:policy/my-policy -
Set a variable to store the Amazon Resource Name (ARN) of the policy that you want to use. Replace
my-policywith the name of the policy that you want to confirm permissions for.export policy_arn=arn:aws:iam::111122223333:policy/my-policy -
View the default version of the policy.
aws iam get-policy --policy-arn $policy_arnAn example output is as follows.
{ "Policy": { "PolicyName": "my-policy", "PolicyId": "EXAMPLEBIOWGLDEXAMPLE", "Arn": "arn:aws:iam::111122223333:policy/my-policy", "Path": "/", "DefaultVersionId": "v1", [...] } } -
View the policy contents to make sure that the policy includes all the permissions that your Pod needs. If necessary, replace
1in the following command with the version that’s returned in the previous output.aws iam get-policy-version --policy-arn $policy_arn --version-id v1An example output is as follows.
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::my-pod-secrets-bucket" } ] }If you created the example policy in a previous step, then your output is the same. If you created a different policy, then the
examplecontent is different. -
Confirm that the Kubernetes service account is annotated with the role.
kubectl describe serviceaccount my-service-account -n defaultAn example output is as follows.
Name: my-service-account Namespace: default Annotations: eks.amazonaws.com/role-arn: arn:aws:iam::111122223333:role/my-role Image pull secrets: <none> Mountable secrets: my-service-account-token-qqjfl Tokens: my-service-account-token-qqjfl [...]