ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
IAM User က access key (long-term credential) နဲ့ authenticate လုပ်ပါတယ် — code ထဲ hardcode ရင် leak risk ရှိပါတယ်။ IAM Role ကတော့ AWS resource (EC2 instance, Lambda function) ကို 'assume' လုပ်ခွင့်ပေးထားပြီး, resource က temporary credential (auto-rotate ဖြစ်နေတဲ့) ကို automatic ရယူပါတယ် — access key ကို code ထဲ တစ်ခါမှ ရေးစရာ မလိုတော့ပါ။ EC2 instance ကို IAM Role (ဥပမာ - S3ReadOnlyRole) attach ထားရင်, instance ထဲက application က S3 ကို access key မလိုဘဲ ချိတ်ဆက်နိုင်ပါတယ် — AWS SDK က instance metadata ကနေ temporary credential ကို auto-fetch ပေးပါတယ်.
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
EC2 instance ပေါ်က application က S3 bucket ကို ဖတ်ချင်ရင် — access key ကို config file ထဲ ရေးမယ့်အစား, instance ကို 'S3ReadOnlyRole' IAM Role attach လိုက်ရင်, application code ထဲက AWS SDK က automatic ဖြင့် temporary credential ရယူပြီး S3 ကို ချိတ်ဆက်ပေးပါလိမ့်မယ် — code ထဲ credential တစ်လုံးမှ မရေးထားလို့ leak risk လုံးဝ မရှိပါ.
အတူတူ ကြည့်မယ်
# Create a role EC2 can assume, attach S3 read-only policy
aws iam create-role \
--role-name S3ReadOnlyRole \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws iam attach-role-policy \
--role-name S3ReadOnlyRole \
--policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
# Attach the role to a running EC2 instance
aws ec2 associate-iam-instance-profile \
--instance-id i-0123456789abcdef0 \
--iam-instance-profile Name=S3ReadOnlyRole$ (from inside the EC2 instance) aws s3 ls
# Works with zero configured credentials — the role provides them၅ မိနစ် စမ်းကြည့်
IAM Role တစ်ခု (S3 read-only) create လုပ်ပြီး EC2 instance ကို attach ကြည့်ပါ — instance ထဲက `aws s3 ls` run ကြည့်ပြီး access key config မလိုဘဲ အလုပ်လုပ်ကြောင်း confirm လုပ်ကြည့်ပါ။
သတိလေးတစ်ချက်
IAM Role ကို EC2 instance မှာ attach ပြီးရင်, instance ထဲမှာ run နေတဲ့ application/user အားလုံးက ဒီ Role ရဲ့ permission ကို share သုံးနိုင်ပါတယ် — multi-tenant instance မှာ Role permission ကို သတိထားပါ။