Use Case: Setting Up IRSA (IAM Roles for Service Accounts) in EKS Cluster Using Terraform
Ramandeep Chandna
AWS Community Builder AI Engineering | System Engineering Manager AWS | 7xAWS | CKA | CKAD | 2xCloudBees
Let’s walk through a use case in the STAR (Situation, Task, Action, Result) method for setting up IAM Roles for Service Accounts (IRSA) in an AWS EKS cluster using Terraform. We will also explore best practices and benefits for this approach.
Use Case in STAR Method:
Situation:
You have an EKS cluster running in AWS, and you want to grant specific AWS permissions to workloads running inside the cluster (such as an application in a pod) securely. These workloads need to access AWS resources like S3 buckets, DynamoDB tables, or other AWS services. Instead of granting these permissions to EC2 instances or Kubernetes nodes, you need a more granular, pod-level permission model.
You decide to implement IAM Roles for Service Accounts (IRSA) in your EKS cluster to enable pods to assume IAM roles securely, following AWS best practices.
Task:
The goal is to:
Action:
To implement this, you will:
Here’s the Terraform code to set up IRSA in an EKS cluster:
Sample Terraform Code for IRSA Setup:
provider "aws" {
region = "us-west-2"
}
# EKS cluster setup
module "eks" {
source = "terraform-aws-modules/eks/aws"
cluster_name = "example-eks-cluster"
cluster_version = "1.26"
vpc_id = "vpc-abcde123"
subnets = ["subnet-12345", "subnet-67890"]
}
# Create OIDC identity provider for the EKS cluster
resource "aws_iam_openid_connect_provider" "eks_oidc" {
url = "https://oidc.eks.${module.eks.cluster_endpoint}:/idp/eks/${module.eks.cluster_name}"
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = [
"9a5d5a3beeb57414988dadd9b49d9b86949a9a9f" # This is an example thumbprint; it should be updated accordingly
]
}
# Create IAM policy for the service account
resource "aws_iam_policy" "eks_s3_access_policy" {
name = "EKS-S3-Access-Policy"
description = "Policy to allow access to S3 buckets"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "s3:ListBucket"
Effect = "Allow"
Resource = "arn:aws:s3:::your-bucket-name"
},
{
Action = "s3:GetObject"
Effect = "Allow"
Resource = "arn:aws:s3:::your-bucket-name/*"
}
]
})
}
# Create IAM role for the service account
resource "aws_iam_role" "eks_s3_access_role" {
name = "EKS-S3-Access-Role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Federated = aws_iam_openid_connect_provider.eks_oidc.arn
}
Action = "sts:AssumeRoleWithWebIdentity"
Condition = {
StringEquals = {
"oidc.eks.${module.eks.cluster_endpoint}:sub" = "system:serviceaccount:default:s3-access-sa"
}
}
}
]
})
}
# Attach the policy to the IAM role
resource "aws_iam_role_policy_attachment" "eks_s3_access_attachment" {
role = aws_iam_role.eks_s3_access_role.name
policy_arn = aws_iam_policy.eks_s3_access_policy.arn
}
# Create the Kubernetes service account that will assume the IAM role
resource "kubernetes_service_account" "s3_access_sa" {
metadata = {
name = "s3-access-sa"
namespace = "default"
}
automount_service_account_token = true
}
# Annotate the service account with the IAM role ARN
resource "kubernetes_service_account_iam_role_binding" "s3_access_binding" {
metadata {
name = "s3-access-binding"
namespace = kubernetes_service_account.s3_access_sa.metadata[0].namespace
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "Role"
name = aws_iam_role.eks_s3_access_role.name
}
subjects {
kind = "ServiceAccount"
name = kubernetes_service_account.s3_access_sa.metadata[0].name
namespace = kubernetes_service_account.s3_access_sa.metadata[0].namespace
}
}
Explanation of the Terraform Code:
领英推荐
Result:
Best Practices for IRSA Setup:
Benefits of Using IRSA with EKS and Terraform:
By following best practices and implementing IRSA using Terraform, you can secure your AWS EKS workloads and enable them to interact with AWS resources in a way that is both scalable and manageable. This approach reduces the security risks associated with managing AWS credentials and access for Kubernetes workloads.
Join me in exploring the latest in AWS DevSecOps! Subscribe to the newsletter and follow for more insights. ????
?? Book 1:1 Connect: https://lnkd.in/dCgmt2hw
?? Join the AWS DevSecOps Community: https://lnkd.in/dDsf4rCv
?? Follow me on LinkedIn for more insights: https://lnkd.in/gy8xy2Gb
?? Subscribe to my YouTube channel for comprehensive content: https://lnkd.in/deiCTySg
Reference Link: https://platformwale.blog/2023/08/02/iam-roles-for-service-accounts-irsa-in-aws-eks-within-and-cross-aws-accounts/
Bonus Video for AWS/DevOps my AMA session : https://www.youtube.com/watch?v=ijAgPQoHMrI&t=2388s
Remember, like, share, and comment to help spread valuable knowledge further. Let's keep learning and growing together. ???? hashtag#AWS hashtag#DevSecOps hashtag#AI hashtag#ML hashtag#Data
Systems Architect at EPAM
4 个月Nice content, concisely explained.
?? Multi-Cloud & DevOps Strategist | OCI DevOps Engineer | GenAI Enthusiast | 2x OCI | 2x AWS | 1x GCP | 1x Azure| Kubernetes ?? | Terraform ??? | Helm ?? | Docker ?? | Prisma Cloud ?? | DevSecOps ? | Ex-TCS
4 个月Insightful!
DevOps Engineer | ?? AWS Cloud | ?? Git | ?? Jenkins CI/CD | ?? Kubernetes | ?? Docker | ?? Ansible | ??? Terraform (Certified) | ?? Linux | ?? Grafana | ?? Python | ?? Jira | Banking ?? | Payments ?? | Healthcare ??
4 个月Very informative,and useful tips.