Cloud identity and least privilege

Intermediate · 22 min

Principals, roles and policies in cloud IAM: how a request is allowed or denied, and how to write a policy that grants a job only what it needs.

Before thisCompute, storage and networking building blocksSSH keys, bastions and remote access

Be A Better Dev · 22 min

Cloud identity and least privilege

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Be A Better DevReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every cloud API call is checked against a principal (who), an action (what), a resource (on what) and sometimes conditions. IAM is the system that answers yes or no.
  • Give machines roles, not keys. A role hands out short-lived credentials to whatever assumes it, so there is no long-lived secret to leak.
  • In AWS-style evaluation an explicit deny always wins, then any allow, and anything not allowed is denied by default.
  • Least privilege means the policy names the specific actions and resources a job needs. Wildcards like s3:* on * are how one compromised build server becomes a compromised account.
  • Least privilege is a loop, not a one-off. Start narrow, read the access-denied errors and the access logs, and add only what is missing.

The problem: one account, many hands

A cloud account starts with one all-powerful login, the root user on AWS or the subscription owner on Azure. Everything that touches the account after that, people, CI pipelines, VMs, functions, needs some access. If they all share the root credentials, then any one of them leaking means losing everything. That includes the CI server running third-party build plugins and the laptop left in a taxi.

Identity and access management (IAM) splits that power. It gives every actor its own identity and lets you say exactly what each one may do. The guiding rule is least privilege: each identity gets the permissions its job needs and nothing else. A badge that opens only the server room, and only for the on-call engineer, limits what a stolen badge can do.

The four questions

Every API call the cloud receives is checked against four things:

  • Principal: who is asking. A human user, a group, or a workload identity.
  • Action: what they want to do, such as s3:GetObject or compute.instances.delete.
  • Resource: what they want to do it to, such as one bucket or one VM.
  • Condition: extra context, such as the source IP, the time, or whether MFA was used.

A policy is a document that says which combinations are allowed or denied. The vocabulary differs by provider, but the shape is the same.

AWS Azure Google Cloud
Workload identity IAM role Managed identity Service account
Bundle of permissions Policy Role definition Role
Granting it Attach policy to role/user Role assignment at a scope IAM binding on a resource

Roles, not keys

For workloads the most important rule is: do not give machines long-lived keys. A VM, a function or a CI job should assume a role. The cloud hands it temporary credentials that expire within hours, and the platform refreshes them. There is nothing to copy into an environment variable and nothing to forget to rotate. For CI on GitHub this is done with OIDC federation, covered in deploying from Actions with OIDC.

Humans should sign in through a single identity provider with MFA, get access through groups, and use elevated roles only when needed.

How a request is decided

AWS documents the evaluation order, and the core of it is three rules:

  1. If any applicable statement explicitly denies the request, it is denied.
  2. Otherwise, if any statement allows it, it is allowed.
  3. Otherwise it is denied by default.

The full logic adds more layers (organization-wide guardrails, permission boundaries, resource policies, session policies). Azure and Google Cloud both have deny policies too, layered on top of their allow-based roles.

A worked example: evaluating a policy

A nightly billing job needs to read reports from one bucket and write results under its own prefix. It should never delete. Here is the policy, in AWS’s JSON format:

{
  "Version": "2012-10-17",
  "Statement": [
    {"Sid": "ReadReports", "Effect": "Allow",
     "Action": ["s3:GetObject", "s3:ListBucket"],
     "Resource": ["arn:aws:s3:::acme-reports", "arn:aws:s3:::acme-reports/*"]},
    {"Sid": "WriteOwnPrefix", "Effect": "Allow",
     "Action": "s3:PutObject",
     "Resource": "arn:aws:s3:::acme-reports/billing-job/*"},
    {"Sid": "NeverDelete", "Effect": "Deny",
     "Action": "s3:Delete*",
     "Resource": "*"}
  ]
}

I have no AWS account, so I did not attach this to a real role. I wrote a 25-line Python evaluator that applies the three rules above, with shell-style wildcard matching, and ran five requests through it:

$ python3 evaluate.py policy.json
s3:GetObject     acme-reports/2026/11.csv               -> ALLOW (ReadReports)
s3:PutObject     acme-reports/billing-job/out.csv       -> ALLOW (WriteOwnPrefix)
s3:PutObject     acme-reports/2026/11.csv               -> DENY (implicit, nothing matched)
s3:DeleteObject  acme-reports/billing-job/out.csv       -> DENY (explicit, NeverDelete)
s3:GetObject     acme-payroll/salaries.csv              -> DENY (implicit, nothing matched)

The job can write only under its prefix, can’t touch the payroll bucket, and can’t delete even where it may write. The evaluator is a teaching toy. Real AWS evaluation also handles conditions, policy variables, resource policies and organization guardrails, and treats action names as case-insensitive. To test a real policy, use the provider’s own tools, such as the AWS IAM policy simulator or Google’s Policy Troubleshooter.

Getting to least privilege in practice

Nobody writes a perfect policy first time. A workable loop:

  1. Start from the provider’s narrowest predefined role that fits, or a hand-written policy like the one above.
  2. Run the job. Read the access-denied errors. Each one names the missing action.
  3. Add exactly that action on exactly that resource.
  4. Later, use the provider’s access analysis (AWS IAM Access Analyzer, Google’s role recommender, Azure’s access reviews) to find permissions granted but never used, and remove them.

The trap to avoid is fixing one access-denied error with "Action": "*". It works at once, and then it stays forever.

About the video

“AWS IAM Core Concepts You NEED to Know” is from Be A Better Dev, published September 2021, 21 minutes 40 seconds. The description says it explains IAM through a practical example, covering policies, roles, permissions, groups and trust relationships. It is AWS-only and from 2021, so the console will look different, but the concepts have not changed. English is judged from the title, description and channel. Google Cloud Tech’s “What is Cloud IAM?” covers the same ideas on Google Cloud, with chapters on inherited permissions and configuring minimum permissions. AWS’s policy evaluation page is the reference for the rules above.

Where this fits

Identity controls who may call an API. The next layer controls which packets may reach a machine at all: virtual networks, subnets and security groups.

Check yourself

  1. 01A policy allows s3:* on a bucket, and another policy attached to the same role denies s3:DeleteObject. Can the role delete objects?

    No. An explicit deny overrides any allow.

  2. 02Why is a role better than an access key for a VM or a CI job?

    The role issues temporary credentials that expire on their own and are fetched by the workload, so there is no permanent key to store, copy, leak or rotate.

  3. 03What happens to a request that matches no statement at all?

    It is denied. Cloud IAM denies by default, so access exists only where a policy grants it.

  4. 04What are the main IAM building blocks called on Google Cloud and Azure?

    Google Cloud binds principals to roles on a resource in an IAM policy and uses service accounts for workloads. Azure uses role assignments of a role definition at a scope, with managed identities for workloads.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark