IRSA: accessing the AWS API from an EKS pod
Suppose an application running in an EKS pod needs access to a private S3 bucket in the same AWS account.
Here are some ways to do it.
IAM user
This option lets you debug the application locally.
The application calls the AWS API through an SDK. Pass the client the aws_access_key_id and aws_secret_access_key for an IAM user with access to the resource.
Configure the AWS IAM resources as follows:
AWS configuration
- aws_iam_policy
- aws_iam_user
- aws_iam_user_policy_attachment
resource "aws_iam_policy" "this" {
name = "s3-app"
policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"",
"Effect":"Allow",
"Action": "s3:*",
"Resource":[
"arn:aws:s3:::${aws_s3_bucket.this.bucket}",
"arn:aws:s3:::${aws_s3_bucket.this.bucket}/*"
]
}
]
}
EOF
}
resource "aws_iam_user" "this" {
name = "s3-manager"
}
resource "aws_iam_user_policy_attachment" "this" {
user = aws_iam_user.this.name
policy_arn = aws_iam_policy.this.arn
}
k8s configuration
---
apiVersion: apps/v1
kind: Deployment
...
spec:
...
template:
...
spec:
containers:
- name: s3-app
...
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-app
key: aws_access_key_id
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-app
key: aws_secret_access_key
IRSA
IRSA stands for IAM Roles for Service Accounts. It establishes trust between an AWS IAM role and a Kubernetes ServiceAccount through the EKS cluster's OIDC provider.
Attach the same IAM policy to the role that you attached to the user in the previous example. Containers in a pod using that ServiceAccount can then obtain the credentials they need.
AWS configuration
Configure IAM as follows:
- aws_iam_policy
- module iam-assumable-role-with-oidc
resource "aws_iam_policy" "this" {
name = "s3-app"
policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"",
"Effect":"Allow",
"Action": "s3:*",
"Resource":[
"arn:aws:s3:::${aws_s3_bucket.this.bucket}",
"arn:aws:s3:::${aws_s3_bucket.this.bucket}/*"
]
}
]
}
EOF
}
module "iam_assumable_role_s3_app" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "4.2.0"
create_role = true
role_name = "s3-app"
provider_url = replace(module.eks_cluster.cluster_oidc_issuer_url, "https://", "")
role_policy_arns = [
aws_iam_policy.this.arn
]
// NOTE: Trust entry format for the ServiceAccount
// system:serviceaccount:<ServiceAccount.metadata.namespace>:<ServiceAccount.metadata.name>
oidc_fully_qualified_subjects = [
"system:serviceaccount:dev:s3-app",
]
}
Use this if the ServiceAccount name is dynamic.
oidc_subjects_with_wildcards = [
"system:serviceaccount:crossplane-system:aws-provider-*"
]
⚠️ Use either oidc_fully_qualified_subjects or oidc_subjects_with_wildcards.
k8s configuration
- ServiceAccount
- Deployment
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: s3-app
namespace: dev
annotations:
eks.amazonaws.com/role-arn: 'arn:aws:iam::<aws_account_id>:role/s3-app' # (1)
- ARN of the IAM role created by the
iam-assumable-role-with-oidcmodule
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: dev
...
...
spec:
...
template:
...
spec:
serviceAccountName: s3-app
containers:
- name: s3-app
...
Verification
Open a shell in the pod and look for these environment variables:
env | grep AWS
AWS_DEFAULT_REGION=<aws_region>
AWS_REGION=<aws_region>
AWS_ROLE_ARN=arn:aws:iam::<aws_account_id>:role/s3-app
AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
Decode the token at /var/run/secrets/eks.amazonaws.com/serviceaccount/token on https://jwt.io/ to see these claims:
{
"aud": [
"sts.amazonaws.com"
],
"exp": 1673637993,
"iat": 1673551593,
"iss": "https://oidc.eks.<aws_region>.amazonaws.com/id/<HEX_HASH>",
"kubernetes.io": {
"namespace": "test",
"pod": {
"name": "s3-app-<replicaSet>-<random-hash>",
"uid": "47a2e254-d0d3-44bc-acdc-ef8f07a9f63e"
},
"serviceaccount": {
"name": "s3-app",
"uid": "91bb19bc-dab8-4c72-9718-eac7adbc4b59"
}
},
"nbf": 1673551593,
"sub": "system:serviceaccount:dev:s3-app"
}
Check access to the bucket:
aws s3 ls --recursive s3://bucket-s3-app
2021-12-14 15:07:52 47134393 app/my-cool.stuff
...
The Amazon EKS pod identity webhook sets this up.
Cross account irsa
Now suppose the application runs in an EKS pod in AWS account A and needs access to a private S3 bucket in AWS account B.
The diagram shows the CI account as A and the target account as B:

In most cases, I'd try to avoid designing a system where a pod in account A has to call the AWS API for a resource in account B.
AWS account A
AWS configuration
- aws_iam_policy
- aws_iam_role
- aws_iam_role_policy_attachment
resource "aws_iam_policy" "this" {
name = "s3-app"
policy = <<EOF
// This policy allows assuming the role in account B
{
"Version":"2012-10-17",
"Statement":[
{
"Action":"sts:AssumeRole",
"Resource":"arn:aws:iam::<aws_account_id_B>:role/s3-app-B",
"Effect":"Allow",
"Sid":""
}
]
}
EOF
}
locals {
oidc = replace(module.eks_cluster.cluster_oidc_issuer_url, "https://", "")
}
resource "aws_iam_role" "this" {
name = "s3-app-A"
assume_role_policy = <<EOF
// Establish trust between the role in account A and the EKS ServiceAccount
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":{
"Federated":"arn:aws:iam::<aws_account_id_A>:oidc-provider/${local.oidc}"
},
"Action":"sts:AssumeRoleWithWebIdentity",
"Condition":{
"StringEquals":{
"${local.oidc}:sub":"system:serviceaccount:dev:s3-app"
}
}
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "this" {
role = aws_iam_role.this.name # (2)
policy_arn = aws_iam_policy.this.arn # (1)
}
k8s configuration
- ServiceAccount
- Deployment
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: s3-app
namespace: dev
annotations:
eks.amazonaws.com/role-arn: 'arn:aws:iam::<aws_account_id_A>:role/s3-app-A' # (1)
- ARN of the IAM role that trusts this ServiceAccount and can assume the role in account B
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: dev
...
...
spec:
...
template:
...
spec:
serviceAccountName: s3-app
containers:
- name: s3-app
...
AWS account B
- aws_iam_policy
- aws_iam_role
resource "aws_iam_policy" "this" {
name = "s3-app"
policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"",
"Effect":"Allow",
"Action": "s3:*",
"Resource":[
"arn:aws:s3:::${aws_s3_bucket.this.bucket}",
"arn:aws:s3:::${aws_s3_bucket.this.bucket}/*"
]
}
]
}
EOF
}
resource "aws_iam_role" "this" {
name = "s3-app-B"
assume_role_policy = <<EOF
// Gotcha: The role in account B must trust the role in account A
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":{
"AWS":"arn:aws:iam::<aws_account_id_A>:role/s3-app-A"
},
"Action":"sts:AssumeRole"
}
]
}
EOF
}
- aws_iam_role_policy_attachment
resource "aws_iam_role_policy_attachment" "this" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.this.arn
}
Verification
Open a shell in the pod and look for the same environment variables:
# env
env | grep AWS
AWS_DEFAULT_REGION=<aws_region>
AWS_REGION=<aws_region>
AWS_ROLE_ARN=arn:aws:iam::<aws_account_id_A>:role/s3-app-A
AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
The JWT has the same claims as before.
Trying to access the S3 bucket returns an error:
aws s3 ls --recursive s3://bucket-s3-app
An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
Add this configuration:
cat <<EOF >>~/.aws/config
[profile A]
role_arn = arn:aws:iam::<aws_account_id_A>:role/s3-app-A
web_identity_token_file = /var/run/secrets/eks.amazonaws.com/serviceaccount/token
[profile B]
role_arn = arn:aws:iam::<aws_account_id_B>:role/s3-app-B
source_profile = A
role_session_name = xactarget
EOF
Try again with the named profile:
aws s3 ls --recursive s3://bucket-s3-app --profile B
2021-12-14 15:07:52 47134393 app/my-cool.stuff
...
Done 🔥