AWS Builder Center

Your First Custom IAM Policy: Least Privilege in Practice

Stop giving everything AdministratorAccess. Write a policy that grants one job and nothing else, then validate and test it for $0 before you attach it.

Why this matters

If every identity in your account has AdministratorAccess, one leaked key is a full account takeover. Least privilege means each identity can do its one job and nothing else, so a leak stays small.
In my previous article, Securing a New AWS Account in Your First Hour ([LINK TO PART 1]), we locked down the root user and set up a daily admin identity. This is the next step: writing a custom policy by hand for a real task, and proving it works before anyone uses it.
By the end, you’ll have a policy that only lets a script upload backups to one folder of one S3 bucket, and you’ll know how to check it with two free tools: IAM Access Analyzer policy validation and the IAM policy simulator. IAM itself is free, and the S3 usage for a small test costs a few cents at most, covered by Free Tier credits.

The scenario

A backup script runs every night and uploads a file to S3. It needs to do exactly two things: upload files into the  backups/  folder of one bucket, and list what’s inside that folder to confirm the upload landed.
It should not be able to read other folders, download files, delete anything, or touch any other bucket or service. If its credentials leak, the worst an attacker can do is add files to one folder.
For this lab, the script uses an IAM user called  backup-uploader . In production, prefer an IAM role with temporary credentials, for example a role attached to an EC2 instance. The policy is the same either way.

Anatomy of a policy

An IAM policy is a JSON document with a list of statements. Each statement says whether to allow or deny ( Effect ), which API calls it covers ( Action ), which resources it applies to ( Resource ), and optionally under what circumstances ( Condition ).
Three rules explain almost every result you’ll see. Everything starts denied, so if no statement allows an action, it’s an implicit deny. A matching Allow grants the action. And an explicit Deny always wins: if any statement denies an action, no Allow can override it.
Always start with  "Version": "2012-10-17" . It’s the current policy language version, and policy variables like  ${aws:username}  only work with it.

Write the policy

This is the trap that catches most beginners in S3: listing and uploading act on different resources.  s3:ListBucket  acts on the bucket itself ( arn:aws:s3:::my-backups-bucket-2026 ), while  s3:PutObject  acts on the objects inside it ( arn:aws:s3:::my-backups-bucket-2026/backups/* ). If you put both in one statement with the wrong ARN, one of them silently fails. So we use two statements.
Save this as  backup-policy.json :
json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListOnlyBackupsFolder",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-backups-bucket-2026",
"Condition": {
"StringLike": { "s3:prefix": ["backups/*"] }
}
},
{
"Sid": "UploadToBackupsFolder",
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-backups-bucket-2026/backups/*"
}
]
}
The first statement allows listing, but the condition limits it to keys that start with  backups/ , so listing the root of the bucket is denied. The second allows uploads only inside  backups/ . There’s no  GetObject  and no  DeleteObject , so those stay implicitly denied.
Once it passes the checks in the next section, create it in the console under IAM → Policies → Create policy → JSON, or with the CLI:
bash
aws iam create-policy \
--policy-name BackupUploaderPolicy \
--policy-document file://backup-policy.json
Don’t attach it to anyone until it has been validated and tested.

Validate and test before you attach it

Access Analyzer finds problems in how the policy is written. The policy simulator shows what it actually allows. Both are free.
Validate with IAM Access Analyzer. Policy validation checks your JSON against the IAM policy grammar and AWS best practices, and reports errors, security warnings, general warnings and suggestions. It runs automatically in the console’s JSON policy editor. From the CLI:
bash
aws accessanalyzer validate-policy \
--policy-document file://backup-policy.json \
--policy-type IDENTITY_POLICY
An empty  findings  list means no problems were found. Try breaking the policy on purpose, for example by writing  s3:PutObjects , and run it again to see what an error looks like. Access Analyzer also offers custom policy checks such as “check for new access”, but those are billed per call, so you don’t need them for this lab.
Simulate with the IAM policy simulator. The simulator evaluates the policy against actions and resources you choose, without touching real data:
bash
aws iam simulate-custom-policy \
--policy-input-list file://backup-policy.json \
--action-names s3:PutObject s3:GetObject s3:DeleteObject \
--resource-arns arn:aws:s3:::my-backups-bucket-2026/backups/test.txt
Check the  EvalDecision  for each action.  s3:PutObject  should come back as  allowed , and both  s3:GetObject  and  s3:DeleteObject  as  implicitDeny . If you prefer a UI, the same tool is at policysim.aws.amazon.com. Keep in mind that the simulator only evaluates identity-based policies and the inputs you give it, so results can differ from a live request that also involves a bucket policy.
Test for real. Attach the policy to  backup-uploader , create an access key for it, and save it as a CLI profile. Then:
bash
# Should work
aws s3 cp backup.tar.gz s3://my-backups-bucket-2026/backups/ --profile backup-uploader
aws s3 ls s3://my-backups-bucket-2026/backups/ --profile backup-uploader

# Should fail with AccessDenied
aws s3 ls s3://my-backups-bucket-2026/ --profile backup-uploader
aws s3 rm s3://my-backups-bucket-2026/backups/backup.tar.gz --profile backup-uploader
The failures are the point. Every  AccessDenied  is a permission an attacker won’t have either.

Tighten it

The policy works, but two changes make it safer and reusable.
First, give each user their own folder. Replace the fixed path with the  ${aws:username}  policy variable. AWS fills it in with the name of the IAM user making the request, so one policy can serve several uploaders and none of them can write into another’s folder.
Second, add an explicit deny for destructive actions. Right now deletes are only implicitly denied. If someone later attaches a broader policy to the same user by mistake, those deletes would open up. An explicit Deny keeps them closed no matter what else is attached.
json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListOwnBackupsFolder",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-backups-bucket-2026",
"Condition": {
"StringLike": { "s3:prefix": ["backups/${aws:username}/*"] }
}
},
{
"Sid": "UploadToOwnBackupsFolder",
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-backups-bucket-2026/backups/${aws:username}/*"
},
{
"Sid": "NeverDeleteBackups",
"Effect": "Deny",
"Action": ["s3:DeleteObject", "s3:DeleteObjectVersion", "s3:DeleteBucket"],
"Resource": [
"arn:aws:s3:::my-backups-bucket-2026",
"arn:aws:s3:::my-backups-bucket-2026/*"
]
}
]
}
Now  backup-uploader  can only write to  backups/backup-uploader/ . Run the validation and the real tests again after any change. A policy you edited but didn’t test is a policy you’re guessing about.

Common mistakes

If  aws s3 ls  returns AccessDenied, you probably granted  s3:ListBucket  on the object ARN ( bucket/* ) instead of the bucket ARN. If uploads fail, it’s usually the opposite:  s3:PutObject  on the bucket ARN instead of one ending in  /* .
If uploads work but listing the folder fails, the path you’re listing doesn’t match the  s3:prefix  condition. Include the trailing slash, as in  s3://bucket/backups/ . If  ${aws:username}  doesn’t resolve, check that the policy starts with  "Version": "2012-10-17" .
If the bucket uses SSE-KMS encryption, uploads also need  kms:GenerateDataKey  on that specific key. And if everything works, including deletes, look for an  "Action": "s3:*"  or  "Resource": "*"  that slipped in.

Wrapping up

Least privilege isn’t a setting you turn on. It’s a habit: describe the job in plain words, grant only the exact actions and ARNs it needs, validate the policy, test both what should work and what should fail, and add an explicit Deny for anything destructive. Writing one policy by hand teaches more about IAM than attaching ten managed policies.
Next in this series, I’ll look at detecting unusual activity in an account. If a policy mistake ever cost you an afternoon, share it in the comments.
Sources: AWS IAM User Guide (Validate policies with IAM Access Analyzer; Checks for validating policies), IAM Access Analyzer pricing, SimulateCustomPolicy API reference, IAM policy simulator.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article