Skip to content
EgyKode
04 · Infrastructure as CodeLab 24 / 59
Guided labterraform

IAM Roles, IRSA Policies & Security Groups

Give the cluster, the nodes and the build server exactly the permissions each needs and nothing more.

Time
23 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

Before you start

CostLow cost

— IAM roles, policies and security groups cost nothing. Only the resources they are attached to do.

How to clean up

The scenario#

The cluster works because the node role has AdministratorAccess. Every pod on every node inherits it, so a compromise of any container is a compromise of the whole account.

The security groups allow 0.0.0.0/0 on the database port, with a comment saying it is temporary.

Hands-on environment

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

A role is two policies

Step 1 of 3

What you are building#

Two independent systems that people conflate:

  • IAM answers what may this identity call in the AWS API?
  • Security groups answer what may reach this network interface?

An instance with no IAM permissions can still be reached on port 22. An instance with AdministratorAccess and no inbound rules cannot be reached at all but can delete your account. You need both, and neither substitutes for the other.

text
  Internet ──> sg-alb  (:443 from 0.0.0.0/0)
                  │  referenced by
                  v
              sg-nodes (:30000-32767 from sg-alb only)
                  │  referenced by
                  v
               sg-rds  (:5432 from sg-nodes only)

Security groups reference other security groups, not CIDRs. That is the single most useful thing in this lab. A rule that says "from sg-nodes" keeps working when nodes are replaced, scaled or move subnet — a rule that says "from 10.0.10.0/24" needs editing every time the network changes, and somebody will widen it instead.


Build it#

Verify it worked#

Terminal
# No role has a wildcard action
aws iam list-attached-role-policies --role-name eks-node
aws iam get-role --role-name eks-node --query 'Role.AssumeRolePolicyDocument'
 
# No security group allows the database port from the world
aws ec2 describe-security-groups \
  --filters "Name=vpc-id,Values=$(terraform output -raw vpc_id)" \
  --query 'SecurityGroups[].IpPermissions[?contains(IpRanges[].CidrIp, `0.0.0.0/0`)].[FromPort,ToPort]' \
  --output table
# only 443 should appear
 
# IRSA works, and there is no credential file anywhere
kubectl exec -n production deploy/api -- env | grep AWS_ROLE_ARN
kubectl exec -n production deploy/api -- aws sts get-caller-identity
# the ARN is the IRSA role, not the node role
 
# The negative test — from a pod WITHOUT the annotation
kubectl run probe --rm -it --image=amazon/aws-cli --restart=Never -- sts get-caller-identity
# returns the node role, and should be denied on your scoped actions

That last check is the one worth doing. Proving a permission works is easy; proving the absence of one is the actual security claim.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
terraform destroy -auto-approve
aws iam list-roles --query 'Roles[?starts_with(RoleName, `eks-`)].RoleName'

Cost of this lab: Free — IAM roles, policies and security groups cost nothing. The resources they are attached to do.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 24 of 59 on the project path

Amazon ECR Container Registry & S3 Storage BucketsStand up the registry your images live in and the buckets your application writes to, both private by default.23 minIntermediate

Previous: AWS VPC, Subnets, Gateways & Route Tables