IAM Policies ඇත්තටම වැඩ කරන්නේ කොහොමද? – Allow, Deny, Action, Resource සහ Principal සරලව
AWS IAM ගැන ඉගෙනගන්නකොට එක stage එකක අනිවාර්යයෙන්ම මේ වගේ දෙයක් දකින්න ලැබෙනවා.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket/*"
}
]
}
Beginner කෙනෙක්ට පළවෙනි reaction එක sometimes:
“මේ JSON එක මොකක්ද?”
Effect?
Action?
Resource?
Principal?
ARN?
Wildcards?
Suddenly IAM complicated වගේ පේන්න පටන් ගන්නවා.
හැබැයි IAM Policy එකක් කියන්නේ complicated programming language එකක් නෙවෙයි.
ඒක basically AWS එකට දෙන set of rules එකක්.
සරලවම කියනවා නම්:
“කාට, මොන action එක, මොන resource එකට, මොන conditions යටතේ කරන්න දෙන්නද?”
එච්චරයි.
මේ sentence එක තේරුම්ගත්තම IAM Policies වල ලොකු කොටසක් already clear.
මුලින්ම AWS Request එකක් imagine කරන්න
හිතන්න Nimal කියන Developer කෙනෙක් AWS Console එකෙන් S3 Bucket එකක file එකක් download කරන්න try කරනවා.
Nimal request කරනවා:
“මට මේ file එක read කරන්න ඕන.”
AWS එක immediately එක දෙන්නේ නැහැ.
AWS එක questions කිහිපයක් අහනවා.
කවුද request කරන්නේ?
මොන action එකද?
මොන resource එකටද?
Applicable policies මොනවද?
Allow තියෙනවද?
Deny තියෙනවද?
ඊට පස්සේ තමයි final decision එක එන්නේ:
Allow
හෝ
Deny
AWS request එකක් receive කළාම principal authenticate කරලා, request context එක process කරලා, applicable policies evaluate කරලා access allow හෝ deny කරනවා.
IAM Policy කියන්නේ මොකක්ද?
IAM Policy කියන්නේ permissions define කරන JSON document එකක්.
Policy එක AWS එකට කියනවා:
“මේ identity එකට මේ actions කරන්න දෙන්න.”
හෝ
“මේ action එක කිසිම විදිහකට කරන්න දෙන්න එපා.”
Policy එකේ core elements කිහිපයක්:
Effect
Action
Resource
Principal
Condition
අපි එකින් එක බලමු.
1. Effect – Allow ද? Deny ද?
Effect කියන්නේ policy statement එකේ result එක.
Valid values දෙකයි:
Allow
Deny
AWS IAM documentation අනුව Effect required element එකක්, සහ එය statement එක access allow කරනවද explicit deny කරනවද කියලා define කරනවා.
Example:
"Effect": "Allow"
Meaning:
“මේ rule එක match වුණොත් permission දෙන්න.”
තවත් example:
"Effect": "Deny"
Meaning:
“මේ rule එක match වුණොත් permission දෙන්න එපා.”
Simple.
හැබැයි මෙතන IAM වල most important rule එකක් තියෙනවා.
Explicit Deny wins.
Allow එකක් සහ Deny එකක් දෙකම තිබ්බොත්?
හිතන්න User එකකට policy එකකින්:
Allow S3 access
දීලා තියෙනවා.
හැබැයි තවත් applicable policy එකක:
Deny S3 DeleteObject
තියෙනවා.
Userට S3 access Allow වෙලා තියෙන නිසා delete කරන්න පුළුවන්ද?
නැහැ.
Applicable explicit Deny එකක් තිබ්බොත් ඒක Allow එක override කරනවා. AWS evaluation logic එකේ explicit deny එක final deny result එකක් දෙනවා.
මතක තියාගන්න:
DENY beats ALLOW.
IAM world එකේ මේ rule එක gold.
2. Action – “මොන වැඩේ කරන්නද?”
Action කියන්නේ permission apply වෙන AWS operation එක.
Example:
"Action": "s3:GetObject"
මේකෙන් කියන්නේ:
S3 object එකක් read කරන්න.
තවත් examples:
s3:PutObject
→ Object upload කරන්න.
s3:DeleteObject
→ Object delete කරන්න.
ec2:StartInstances
→ EC2 instance start කරන්න.
ec2:StopInstances
→ EC2 instance stop කරන්න.
Policy එකක් access දෙන්නේ service එකට generally නෙවෙයි.
Specific actions වලට.
මේක තමයි IAM permissions granular කරන්න පුළුවන් කරන beautiful part එක.
Action Wildcard *
Sometimes මේ වගේ policy එකක් දකින්න පුළුවන්:
"Action": "s3:*"
මේකෙන් අදහස් වෙන්නේ:
S3 service එකේ applicable actions සියල්ල.
තවත් powerful example:
"Action": "*"
මේක practically enormous permission එකක් වෙන්න පුළුවන්.
ඒ නිසා production environment එකක * use කරනකොට reason එක clearly understand කරන්න ඕන.
Need එක:
s3:GetObject
විතරක් නම්,
Action: "*" දෙන එක හොඳ security design එකක් නෙවෙයි.
මේකෙන් ආපහු එන්නේ අපි කලින් කතා කළ principle එකට:
Least Privilege.
3. Resource – “මොන resource එකටද?”
Action කියන්නේ කරන්න ඕන වැඩේ.
Resource කියන්නේ ඒ වැඩේ කරන්න පුළුවන් මොන AWS resource එකටද කියන එක.
Example:
"Resource": "arn:aws:s3:::company-images/*"
මේ policy එක company-images bucket එක ඇතුළේ objects වලට apply වෙන්න පුළුවන්.
දැන් policy එක suddenly much more specific.
Action: Read file.
Resource: company-images bucket එකේ files.
මේක තමයි security.
“Read anything anywhere.”
නෙවෙයි.
“Read only what you actually need.”
ARN කියන්නේ මොකක්ද?
Resource element එකේ මේ වගේ strange string එකක් දකින්න පුළුවන්:
arn:aws:s3:::company-images/*
මේකට කියන්නේ:
Amazon Resource Name – ARN
ARN එක AWS resource එක identify කරන unique naming format එකක්.
Simple idea එක:
AWS එකට කියනවා:
“මම කතා කරන්නේ මේ exact resource එක ගැන.”
Different AWS services වල ARN format එක වෙනස් වෙන්න පුළුවන්.
Resource * කියන්නේ?
මේකත් powerful.
"Resource": "*"
Meaning:
Applicable resources සියල්ල.
Sometimes AWS actions වල nature එක නිසා Resource: "*" අවශ්ය වෙන situations තියෙනවා.
හැබැයි blindly use කරන්න එපා.
Need එක specific EC2 instance එකක් නම්?
Possible නම් ඒ resource එක target කරන්න.
Need එක specific S3 bucket එකක් නම්?
Bucket එක target කරන්න.
Need එක entire account නෙවෙයි නම්?
Entire account open කරන්න එපා.
4. Principal – “කාටද?”
දැන් IAM Policies වල most misunderstood element එකකට එමු.
Principal
Principal කියන්නේ:
මේ policy එක apply වෙන්නේ කාටද?
Request කරන identity එක කවුද?
ඒක:
AWS Account එකක්
IAM Role එකක්
IAM User එකක්
AWS Service එකක්
Federated identity එකක්
වෙන්න පුළුවන්.
AWS request-context model එකේ Principal කියන්නේ request කරන authenticated human හෝ workload identity එක.
S3 Bucket Policy Example එකක්
හිතන්න S3 Bucket එකකට particular Role එකක් access කරන්න ඕන.
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:role/AppRole"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::company-images/*"
}
Human language එකෙන්:
“**AppRole** කියන Role එකට **company-images** bucket එකේ objects read කරන්න දෙන්න.”
මේකේ:
Principal
→ කවුද?
Action
→ මොනවද කරන්න පුළුවන්?
Resource
→ කොහේද කරන්න පුළුවන්?
Effect
→ Allow ද Deny ද?
Suddenly JSON එක story එකක් වගේ පේනවා.
හැබැයි Important: හැම Policy එකකම Principal දාන්නේ නැහැ
මේක interview වලටත් practical work වලටත් ගොඩක් වැදගත්.
Identity-Based Policy එකක් User හෝ Role එකකට directly attach කරනවා නම්, සාමාන්යයෙන් Principal element එක දාන්නේ නැහැ.
ඇයි?
Policy එක attach කරලා තියෙන identity එකම principal කියලා already known.
හැබැයි Resource-Based Policy එකක, S3 Bucket Policy වගේ එකක, Principal හරහා access දෙන identity එක specify කරනවා. AWS documentation එක මේ distinction එක explicitly දක්වනවා.
මේ line එක මතක තියාගන්න:
Identity Policy → “What can I do?”
Resource Policy → “Who can access me?”
Principal * දාන්න පුළුවන්ද?
ඔව්.
හැබැයි මේක extremely careful වෙන්න ඕන තැනක්.
"Principal": "*"
Resource-based policy එකක Effect: Allow එක්ක මේක use කළොත් public හෝ anonymous access ලබාදෙන situation එකක් ඇති වෙන්න පුළුවන්.
AWS itself unrestricted wildcard principals ගැන caution කරනවා, intentionally public access ලබාදෙන situation එකක් නොවේ නම් specific principal specify කරන්න recommend කරනවා.
Simple lesson:
**Principal: "*"** copy-paste කරන්න කලින් ඒකෙන් අදහස් වෙන්නේ මොකක්ද කියලා තේරුම්ගන්න.
5. Condition – “මොන conditions යටතේද?”
Title එකේ නොතිබුණත් IAM Policies really understand කරන්න Condition දැනගන්න ඕන.
Condition එකෙන් කියන්නේ:
Permission එක මේ additional requirement satisfy වුණොත් විතරක් apply කරන්න.
Example:
Specific IP range එකකින් request එක එනවා නම්.
MFA use කරලා තියෙනවා නම්.
Specific VPC එකකින් request කරනවා නම්.
Specific tag එකක් තියෙන resource එකක් නම්.
Certain time range එකක් ඇතුළේ නම්.
මේකෙන් IAM Policies තවත් powerful වෙනවා.
AWS PARC model එකේ request evaluation සඳහා Principal, Action, Resource සහ request context/conditions consider කරනවා.
දැන් complete Policy එකක් බලමු
හිතන්න Developer කෙනෙක්ට specific S3 Bucket එකෙන් objects read කරන්න ඕන.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::company-images/*"
}
]
}
Line-by-line:
Version
"Version": "2012-10-17"
Policy language version එක.
මේක date එකක් වගේ පේනවා, නමුත් policy language version identifier එක.
Statement
Actual permissions rules තියෙන section එක.
Effect
"Effect": "Allow"
Permission දෙන්න.
Action
"Action": "s3:GetObject"
Objects read කරන්න.
Resource
"Resource": "arn:aws:s3:::company-images/*"
company-images bucket එකේ objects වලට විතරයි.
Human language:
“මේ identity එකට company-images bucket එකේ objects read කරන්න දෙන්න.”
JSON එක suddenly scary නෑ.
Multiple Actions දෙන්න පුළුවන්ද?
ඔව්.
"Action": [
"s3:GetObject",
"s3:PutObject"
]
Meaning:
Read කරන්නත් පුළුවන්.
Upload කරන්නත් පුළුවන්.
Delete permission?
නැහැ.
s3:DeleteObject Allow කරලා නැත්නම් automatically ඒ permission ලැබෙන්නේ නැහැ.
මේක IAM වල important principle එකට connect වෙනවා.
Default Deny – IAM වල invisible wall එක
AWS IAM වල huge concept එකක්:
Default Deny.
Permission explicitly allow කරලා නැත්නම් request එක default එකෙන් denied.
AWS මේකට implicit deny කියනවා. Request එකක් allow වෙන්න applicable policy logic අනුව explicit permission අවශ්යයි.
Example:
Nimalට:
s3:GetObject
Allow.
හැබැයි Nimal:
s3:DeleteObject
try කරනවා.
Policy එකේ DeleteObject Allow නෑ.
Explicit Deny එකකුත් නෑ.
Result?
Denied.
ඒක Implicit Deny.
Implicit Deny සහ Explicit Deny අතර වෙනස
මේක හොඳට තේරුම්ගන්න.
Implicit Deny
Permission එක Allow කරලා නැහැ.
ඒ නිසා denied.
Example:
“Delete කරන්න permission දෙලා නැහැ.”
Explicit Deny
Policy එක specifically කියනවා:
"Effect": "Deny"
Example:
“Delete කරන්න absolutely එපා.”
AWS documentation අනුව applicable Allow එකක් සහ explicit Deny එකක් දෙකම තිබුණොත් Deny එක wins.
Permission Evaluation එක ලේසි formula එකකින්
Basic mental model එක:
Step 1
Default = Deny
Step 2
Applicable Allow එකක් තියෙනවද?
නැත්නම්:
Deny
Step 3
Applicable explicit Deny එකක් තියෙනවද?
තියෙනවා නම්:
DENY
Allow එක තිබුණත්.
මේක simple model එකක්.
Real AWS environments වල permissions boundaries, session policies, Organizations SCPs/RCPs සහ resource-based policies වගේ additional policy types evaluation එකට බලපාන්න පුළුවන්.
හැබැයි beginner කෙනෙක්ට first rule:
Default Deny → Look for Allow → Explicit Deny always wins.
Real Example එකක්
හිතන්න Nimal Developer කෙනෙක්.
Nimalට Group Policy එකකින්:
Allow s3:*
ලැබෙනවා.
ඒ කියන්නේ broad S3 permissions.
හැබැයි Company security policy එකක:
Deny s3:DeleteObject
applicable.
Nimal file read කරනවා.
Allow.
Nimal file upload කරනවා.
Allow.
Nimal file delete කරනවා.
Deny.
“හැබැයි මට s3:* තියෙනවනේ?”
ඔව්.
හැබැයි:
Explicit Deny wins.
Production Protection Example එකක්
Imagine කරන්න Developers ලාට development සහ production servers දෙකම visible.
Development servers start/stop කරන්න දෙන්න ඕන.
Production servers stop කරන්න දෙන්න ඕන නැහැ.
Policy design එකෙන් production resources වල dangerous actions restrict කරන්න පුළුවන්.
මේක permissions වල power එක.
Security Team එක every developer පිටිපස්සේ ඉඳගෙන:
“ඒ button එක click කරන්න එපා!”
කියන්න ඕන නැහැ.
Architecture එකෙන්ම guardrail එකක් දානවා.
Good Security depends less on memory and more on controls.
Policy එක door lock එකක් වගේ හිතන්න
Imagine කරන්න secure building එකක්.
Principal
Door එක open කරන්න try කරන කෙනා කවුද?
Action
ඔහු මොනවද කරන්න හදන්නේ?
Door එක open කරන්නද?
Server restart කරන්නද?
File read කරන්නද?
Resource
මොන room එකද?
Condition
Extra rules තියෙනවද?
Working hours ඇතුළෙද?
MFA use කළාද?
Trusted network එකෙන්ද?
Effect
Allow ද Deny ද?
IAM Policy එක suddenly abstract JSON document එකක් නෙවෙයි.
එක digital access-control system එකක්.
Policy order එක වැදගත්ද?
Beginners ලා sometimes හිතනවා:
“Allow එක JSON එකේ උඩ තිබ්බොත් ඒක first execute වෙයිද?”
නැහැ.
මේක programming if/else flow එකක් නෙවෙයි.
Applicable policy statements evaluate කරලා final authorization decision එක ගන්නවා. Explicit deny එකක් applicable නම් ඒක allow එක override කරනවා.
Policy document එකේ Resource element එක Actionට කලින් ලිව්වා කියලා result එක වෙනස් වෙන්නේ නැහැ.
IAM Policy එකක් ලියනකොට questions 5ක් අහන්න
Policy එකක් ලියන්න කලින් JSON ගැන හිතන්න එපා.
මුලින් human language එකෙන් questions අහන්න.
1. WHO?
Access ඕන කාටද?
2. WHAT?
මොන action එක කරන්න ඕනද?
3. WHERE?
මොන resource එකටද?
4. WHEN / UNDER WHAT CONDITIONS?
Additional restrictions තියෙනවද?
5. ALLOW OR DENY?
Expected result එක මොකක්ද?
ඊට පස්සේ JSON එකට translate කරන්න.
මේ method එකෙන් policies ලියන එක ගොඩක් ලේසි.
Bad Policy Example එකක්
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
Easy.
Everything works.
No AccessDenied errors.
හැබැයි security perspective එකෙන්?
Dangerous.
Identity එක compromise වුණොත් blast radius එක huge.
ඒ නිසා policy design කරනකොට question එක:
“කොහොමද error එක නැති කරන්නේ?”
නෙවෙයි.
Question එක:
“Minimum permissions වලින් මේ task එක complete කරන්නේ කොහොමද?”
Better Policy Thinking
Need එක:
“Application එකට one S3 bucket එකෙන් images read කරන්න ඕන.”
Bad thinking:
AdministratorAccess
Better thinking:
Allow s3:GetObject
on:
company-images/*
එච්චරයි.
Application එකට EC2 terminate කරන්න permission ඕන නෑ.
IAM create කරන්න permission ඕන නෑ.
Billing access ඕන නෑ.
S3 bucket delete කරන්නත් ඕන නෑ.
Give the application what it needs — and nothing more.
Why AccessDenied Sometimes Is Good News
Cloud ඉගෙනගන්නකොට:
AccessDenied
දැක්කම frustration.
“Why isn't this working?”
හැබැයි Security Engineer කෙනෙක්ට sometimes ඒ message එක beautiful.
මොකද IAM කියනවා:
“මේ identity එකට මේ action එක කරන්න authorization නැහැ.”
ඒක security control එක work කරනවා කියන indication එකක් වෙන්න පුළුවන්.
Solution එක immediately AdministratorAccess දෙන්න නෙවෙයි.
Investigate කරන්න:
Principal කවුද?
Action එක මොකක්ද?
Resource එක මොකක්ද?
Allow policy එක තියෙනවද?
Explicit Deny එකක් තියෙනවද?
Permissions Boundary තියෙනවද?
SCP එකක් restrict කරනවද?
Resource Policy එක මොකක්ද?
එතනින් තමයි real IAM troubleshooting පටන් ගන්නේ.
IAM Policies වල real power එක
IAM Policies වල power එක permissions දෙන එක විතරක් නෙවෙයි.
Boundaries create කරන එක.
Developerට:
“මේ environment එකේ වැඩ කරන්න.”
Application එකට:
“මේ bucket එක විතරක් read කරන්න.”
CI/CD Pipeline එකට:
“මේ application එක deploy කරන්න.”
Support Engineerට:
“Logs බලන්න, infrastructure modify කරන්න එපා.”
Security Team එකට:
“Security configurations inspect කරන්න.”
මේක තමයි secure cloud organization එකක් build කරන foundation එක.
User කෙනාට full access දීලා trust කරන්න බැරිද?
Trust කරන්න පුළුවන්.
හැබැයි security architecture build කරන්නේ:
“මගේ team එකේ කවුරුත් mistake කරන්නේ නැහැ.”
කියන assumption එක මත නෙවෙයි.
Good people mistakes කරනවා.
Credentials leak වෙනවා.
Accounts compromise වෙනවා.
Scripts වල bugs තියෙනවා.
Commands wrong environment එකේ run වෙනවා.
Security controls තියෙන්නේ people ගැන distrust නිසා විතරක් නෙවෙයි.
Mistakes වල impact එක limit කරන්න.
ඒක IAM වල beautiful purpose එකක්.
Imagine කරන්න Friday Evening...
Production environment.
Engineer කෙනෙක් tired.
Development S3 Bucket එක clean කරන්න command එකක් run කරන්න යනවා.
හැබැයි wrong bucket name එක.
Production Bucket.
Command:
Delete.
Policy එක developerට production deletes deny කරලා තියෙනවා.
AWS response:
AccessDenied.
Engineer initially annoyed වෙයි.
Seconds කිහිපයකට පස්සේ realize වෙනවා…
IAM Policy එක production data save කළා.
මේ තමයි good security.
People perfect වෙන්න expect කරන්නේ නැහැ.
System එක mistakes survive කරන්න design කරනවා.
මතක තියාගන්න ලේසිම Framework එක
IAM Policy එක දැක්කම මේ විදිහට read කරන්න:
Principal
WHO?
Action
DO WHAT?
Resource
TO WHAT?
Condition
UNDER WHAT CONDITIONS?
Effect
ALLOW OR DENY?
මේ questions ටිකෙන් JSON එක human language එකකට translate කරන්න.
AWS Decision එක මතක තියාගන්න
Simple version:
Request arrives
↓
Who is making it?
↓
What action?
↓
Which resource?
↓
Which policies apply?
↓
Any explicit DENY?
↙ ↘
YES NO
↓ ↓
DENY Applicable ALLOW?
↙ ↘
YES NO
↓ ↓
ALLOW DENY
මේ diagram එක IAM Policy Evaluation වල basic mental model එක.
Real AWS authorization scenarios තවත් layers include කරන්න පුළුවන්, නමුත් foundation එක මේක.
අවසානයේ...
IAM Policy එක පළවෙනි වතාවට දකිද්දී JSON characters set එකක් වගේ පේන්න පුළුවන්.
හැබැයි ටිකක් deeper බැලුවම…
ඒ JSON එක පිටිපස්සේ තියෙන්නේ simple conversation එකක්.
AWS: “කවුද ඔයා?”
Principal: “මේ මම.”
AWS: “මොනවද කරන්න හදන්නේ?”
Action: “මේ action එක.”
AWS: “මොන resource එකටද?”
Resource: “මේකට.”
AWS: “Special conditions තියෙනවද?”
Condition: “ඔව් / නැහැ.”
AWS: “Policy එක මොකක්ද කියන්නේ?”
Effect: “Allow.”
හැබැයි anywhere applicable explicit Deny එකක් තිබුණොත්…
AWS කියනවා:
“No.”
මේක තමයි IAM වල power එක.
IAM Policy එකක් code lines කිහිපයක් විතරයි.
හැබැයි ඒ lines කිහිපයෙන්:
Production Database එක protect කරන්න පුළුවන්.
Sensitive S3 Data protect කරන්න පුළුවන්.
Accidental deletes prevent කරන්න පුළුවන්.
Applications වල blast radius එක reduce කරන්න පුළුවන්.
Compromised credentials වල damage එක limit කරන්න පුළුවන්.
Cloud environment එකේ trust boundaries define කරන්න පුළුවන්.
ඒ නිසා IAM Policies කියන්නේ boring JSON files නෙවෙයි.
ඒවා Cloud එකේ Rules of Trust.
Good Cloud Engineer කෙනෙක්:
“මම මේකට permission දෙන්නේ කොහොමද?”
කියලා විතරක් අහන්නේ නැහැ.
ඔහු තවත් question එකක් අහනවා:
“මේ identity එකට ඇත්තටම කොච්චර permission ඕනද?”
එතනින් තමයි Least Privilege කියන security mindset එක පටන් ගන්නේ.
