Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinukadev/7f61f0b24194df5e6c5ad249a48917d7 to your computer and use it in GitHub Desktop.
Save dinukadev/7f61f0b24194df5e6c5ad249a48917d7 to your computer and use it in GitHub Desktop.
A script to create an IAM role with full S3 access linked to a service account in Kubernetes
#!/bin/bash
CLUSTER_NAME=`eksctl get cluster --region ap-southeast-2 -o json | jq -r '.[0].name'`
ISSUER_URL=$(aws eks describe-cluster \
--name $CLUSTER_NAME \
--query cluster.identity.oidc.issuer \
--output text \
--region ap-southeast-2)
ISSUER_HOSTPATH=$(echo $ISSUER_URL | cut -f 3- -d'/')
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
PROVIDER_ARN="arn:aws:iam::$ACCOUNT_ID:oidc-provider/$ISSUER_HOSTPATH"
cat > irp-trust-policy.json << EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "$PROVIDER_ARN"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"${ISSUER_HOSTPATH}:sub": "system:serviceaccount:ns-utils:s3fullaccess"
}
}
}
]
}
EOF
ROLE_NAME=s3-fullaccess-latest
aws iam create-role \
--role-name $ROLE_NAME \
--assume-role-policy-document file://irp-trust-policy.json
aws iam update-assume-role-policy \
--role-name $ROLE_NAME \
--policy-document file://irp-trust-policy.json
aws iam attach-role-policy \
--role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
S3_ROLE_ARN=$(aws iam get-role \
--role-name $ROLE_NAME \
--query Role.Arn --output text)
kubectl create sa s3fullaccess -n ns-utils
kubectl annotate sa s3fullaccess -n ns-utils eks.amazonaws.com/role-arn=$S3_ROLE_ARN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment