IAM and networking

Users, roles, policies, the instance role a service should use, VPCs, subnets, security groups, and what public actually means.

12 min read☁️ AWS for Backend Engineers

Two systems decide whether a request to an AWS resource succeeds, before any service logic runs: IAM, which decides who may do what, and the VPC, which decides what can reach what. A backend engineer meets both on the first day — an AccessDenied from S3, a timeout to RDS — and both are simpler than their documentation, once the model is clear.

IAM: principals, policies, and the evaluation

A principal is who is asking: a user (a person with a password and keys), a role (an identity a service or a person assumes for a while), or a service. A policy is a JSON document that allows or denies actions on resources, and the evaluation is one rule: everything is denied unless a policy allows it, and an explicit deny wins over any allow.

json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject", "s3:PutObject"],
    "Resource": "arn:aws:s3:::code10x-uploads/covers/*",
    "Condition": { "StringEquals": { "s3:x-amz-server-side-encryption": "AES256" } }
  }]
}

The ARN names the resource precisely, and * in a Resource is the line a security review will ask about. Condition narrows further — by source VPC, by tag, by time, by whether the request used TLS. Policies attach to principals (identity policies) or to resources (a bucket policy, a queue policy); a request needs an allow from one of them, and for cross-account access, from both.

The consequence for how you read an AccessDenied: it names the action and the resource, and the answer is always one of three — no policy allows it, an explicit deny forbids it, or the principal is not who you think (the wrong role was assumed, the credentials in the environment are a different user's). aws sts get-caller-identity answers the third before you touch a policy.

Roles for services: no keys in the application

A Java service on AWS should never hold an access key. It runs as a role: an EC2 instance profile, an ECS task role, an EKS service account mapped to a role (IRSA or Pod Identity), a Lambda execution role. The runtime hands the SDK temporary credentials for that role, rotated automatically, through the instance metadata service or an environment the platform sets; the AWS SDK's default credential chain finds them with no configuration, which is why S3Client.create() works in production with nothing in application.properties.

The role's policy is the service's permissions, and least privilege is the discipline: the orders service's role can put objects in the uploads bucket and read one Secrets Manager entry, and nothing else. A key in a properties file, by contrast, is long-lived, copied into every environment, in every developer's shell history, and the thing that turns a leaked repository into a leaked account. This repository's rule — never a credential in the repository, production values in an environment file the unit reads — is the on-premises version of the same principle; on AWS the environment file becomes a role.

For a person, the same idea: no long-lived keys on a laptop, but aws sso login or an identity provider that issues short-lived credentials for a role, and MFA on everything that can hold it.

The VPC: your own network

A VPC is a private network in a region, with an address range you choose (10.0.0.0/16), split into subnets, each in one availability zone. A subnet is public if its route table sends 0.0.0.0/0 to an internet gateway, which means an instance with a public IP in it can be reached from the internet; a subnet is private if it does not — its instances reach the internet only outbound, through a NAT gateway in a public subnet, and nothing reaches them from outside.

plaintext
VPC 10.0.0.0/16
├── public  10.0.1.0/24 (AZ a)   ALB, NAT gateway
├── public  10.0.2.0/24 (AZ b)   ALB
├── private 10.0.11.0/24 (AZ a)  the service (ECS tasks / EC2)
├── private 10.0.12.0/24 (AZ b)  the service
├── private 10.0.21.0/24 (AZ a)  RDS
└── private 10.0.22.0/24 (AZ b)  RDS standby

The shape every backend uses: the load balancer in public subnets, the service in private ones, the database in private ones that only the service's subnets can reach. Two of everything across two availability zones is what "multi-AZ" means, and the reason a service with one instance in one subnet is not highly available whatever its uptime has been. A NAT gateway is billed per hour and per gigabyte, and is the line item that surprises teams whose private services pull large images or call S3 heavily; VPC endpoints for S3, DynamoDB, ECR and the rest route that traffic inside AWS's network, which is cheaper, faster and does not need the NAT at all.

Security groups: the firewall on the instance

A security group is a stateful firewall attached to an instance, a task, a load balancer or a database: inbound rules say what may reach it, and the rules can name another security group as the source. That is the feature that makes the layout above expressible:

plaintext
sg-alb      inbound  443 from 0.0.0.0/0
sg-service  inbound  8080 from sg-alb
sg-rds      inbound  5432 from sg-service

The database accepts connections only from things carrying the service's security group, whatever their IP is today — the same idea as Kubernetes labels, applied to the network. Outbound is allowed by default and usually left so. Network ACLs are the stateless, subnet-level second layer; most teams leave them open and do the work in security groups.

When a connection times out — not refused, times out — from the service to RDS, it is this: the security group on the database does not allow the service's group, or the database is in a subnet the service's route table cannot reach. A refused connection is the database not listening; a timeout is the network never delivering the packet. nc -zv db.host 5432 from the service's host is the test, and the VPC's flow logs are the packet-level record when the answer is not obvious.

Public versus private, decided once

The rule to apply to every resource: is there a reason the internet must reach this directly? For the load balancer, yes. For the service, no — it is reached through the load balancer. For the database, never; a database with a public IP is a breach with a delay. For S3, presigned URLs (the file-upload brief in the system-design course) let a client upload or download directly without the bucket being public. The default for a new resource is private, and every exception is a line in a review.

Under the hood: how a policy decision is actually reached, and why a firewall can be "stateful"

"Least privilege" and "an AccessDenied means something specific" are only true because of two mechanisms worth knowing precisely, not as slogans.

Policy evaluation is a fixed order, and it is not "first match wins." For a single account, a request is evaluated against every policy that applies to it — the principal's identity-based policies (inline and attached, including any policy attached to a group or role it uses) and any resource-based policy on the target (an S3 bucket policy, for instance) — and the results are combined, not short-circuited on the first one found:

  1. Start from implicit deny. With no policy at all, the request is denied. This is why a brand-new IAM user with zero policies attached can do nothing at all, not "can do anything until told otherwise."
  2. Look for an explicit Deny anywhere in the applicable policies. If one exists and matches the action, resource and conditions, the request is denied immediately — an explicit deny always overrides any allow, no matter how specific or how recently attached that allow is.
  3. Otherwise, look for an explicit Allow. The identity-based policies and the resource-based policy are unioned: an allow in either is enough (a bucket policy can grant access a principal's own identity policy never mentions, and vice versa) — except for a cross-account request, where the accessing principal needs an allow from its own identity policy and the resource owner needs to allow it via the resource policy; one side alone is not enough across accounts.
  4. If nothing allowed it, it falls back to implicit deny.

That ordering is the reason AccessDenied collapses to exactly the three causes the lesson above lists, and never a fourth: there is no path through this algorithm that denies a request for any reason other than "no matching allow was found" or "a matching deny was found." An organization can add a further ceiling above all of this — a Service Control Policy that never grants anything, only caps what even an account's own admin-level allow can reach — but that is a separate layer from the request-level evaluation described here, not a fifth branch inside it.

Security groups are stateful; network ACLs are stateless — and that single word is the whole difference. A security group tracks the connection, not just the packet: if an outbound rule (or the default "all outbound allowed") lets a request leave an instance, the response to that specific connection is automatically permitted back in, whether or not an inbound rule would have allowed it on its own. That is what "stateful" means here — the firewall remembers the connection was initiated and waves the reply through. A network ACL has no such memory: it is a stateless, ordered list of numbered rules evaluated top to bottom (lowest number first, first match wins) applied independently to each direction. An ACL that allows outbound traffic on port 5432 to a database does nothing to admit the database's reply — that reply arrives on a high, ephemeral port chosen by the OS, and the ACL needs an explicit inbound rule opening the ephemeral port range (traditionally 1024–65535, though modern kernels use a narrower band) or the reply is silently dropped. This is the single most commonly mis-stated fact in AWS networking: engineers who understand security groups correctly often assume NACLs work the same way, add an outbound rule, and cannot explain why the connection still fails — the answer is always the missing inbound ephemeral-port rule, because NACLs never infer a return path the way security groups do.

Try it yourself

Trace an AccessDenied through the evaluation order

A service's role has an identity-based policy that allows s3:GetObject on arn:aws:s3:::code10x-uploads/* with no Deny anywhere on the role. The bucket code10x-uploads has a bucket policy that denies s3:GetObject from any request that did not arrive over TLS ("Condition": {"Bool": {"aws:SecureTransport": "false"}} paired with "Effect": "Deny"). A request from the service, made over plain HTTP because a client library defaulted to it, fails with AccessDenied. Walk through the four-step evaluation above and say exactly which step produces the denial, and why adding more Allow statements to the role's own policy would not fix it.

Where the denial actually happens

The role's identity policy allows the action — that satisfies step 3 in isolation, but evaluation does not stop at the first policy it looks at. Step 2 runs first, across all applicable policies including the resource-based one, and the bucket policy's Deny matches: the request did not use TLS, so aws:SecureTransport is false, and the condition is satisfied. That explicit deny is found and the request is denied at step 2, before step 3 (the search for an allow) would even matter. No amount of adding Allow statements to the role's identity policy changes this, because an explicit deny is not "outvoted" by a larger number of allows anywhere in the model — it is checked first and, if matched, ends the evaluation. The only fix is either making the request over TLS (the actual bug here — a misconfigured client) or narrowing or removing the bucket's deny condition, which is a security regression, not a fix. This is exactly the shape of a real, hard-to-spot outage: the code and the role both look correct in isolation, and the failure is a condition on a policy nobody was looking at.

Misconceptions

  • "IAM policies are evaluated top to bottom, and the first match wins." They are not evaluated in file order or attachment order at all. Every applicable policy — identity-based and resource-based — is checked, denies are collected first and win outright, and only if none match does the evaluator look for an allow. There is no "first" policy that short-circuits the rest.
  • "A security group and a network ACL are basically the same firewall at two levels." They differ in exactly the way that causes real outages: a security group remembers a connection it allowed out and admits the reply automatically; a NACL evaluates inbound and outbound independently with no memory of either, so an outbound rule for a database port needs a matching inbound rule for the ephemeral reply port or the connection silently times out.

Sources

  • AWS Identity and Access Management User Guide, "Policy evaluation logic" — the deny/allow/implicit-deny order and how identity-based and resource-based policies combine.
  • AWS Identity and Access Management User Guide, "Determining whether a request is allowed or denied within an account."
  • Amazon VPC User Guide, "Compare security groups and network ACLs" — statefulness, rule evaluation order, and the ephemeral-port requirement for NACLs.
Progress is saved on this device and to your account when signed in.