Skip to content

Security groups

2 min read · main part only; folded sections are optional

After this you can answer: How does a security group decide who may connect, and how do you read one rule?

flowchart TB
NET((Internet)) -->|"443, 80"| ALBSG[alb-sg]
ALBSG -->|"3000"| WEBSG[web-sg]
ALBSG -->|"4000"| APISG[api-sg]
APISG -->|"5432"| DBSG[db-sg]
APISG -->|"6379"| REDISSG[redis-sg]

Each arrow is one inbound rule on the group it points to.

  1. Inbound rules say who may connect in. Each rule has a protocol, a port and a source. The source is an address range (CIDR) or another security group.
  2. A group as the source means “tasks that use that group”. Task IPs change on every restart; the group stays the same.
  3. Security groups are stateful. If a request is allowed in, its reply is allowed out automatically. So db-sg needs no outbound rule to answer the api.
  4. There is no “deny” rule. A packet that matches no rule is dropped silently. The caller sees a timeout, not an error.

How to read one rule, out loud: “minitickets-dev-db-sg has an inbound rule, TCP 5432, source minitickets-dev-api-sg.” That means only resources using the API security group can open a Postgres connection. The api tasks and the migration task use it.

GroupInbound rules
alb-sgTCP 443 and 80 from allowedCidrs (default 0.0.0.0/0)
web-sgTCP 3000 from alb-sg; TCP 13000 from api-sg
api-sgTCP 4000 from alb-sg; TCP 14000 from web-sg
db-sgTCP 5432 from api-sg; no outbound rules
redis-sgTCP 6379 from api-sg; no outbound rules

Ports 13000 and 14000 are the Service Connect proxies (see ECS on Fargate). The groups are made in infra/src/security.ts → createSecurityGroups.

See the database security group on the map →

EC2 → Security Groups → minitickets-dev-db-sg → Inbound rules: PostgreSQL, 5432, Source = the api group’s id.

SymptomCauseWhere to look
API log: timeouts to the database on 5432db-sg’s rule names another sourcedb-sg → Inbound rules
Every /trpc/* call returns 504api-sg has no TCP 4000 rule from alb-sgapi target group → Targets
https:// times out, http:// redirectsalb-sg has no TCP 443 rulealb-sg → Inbound rules
Why reference a group instead of the subnet's CIDR?

A CIDR like 10.0.0.0/16 allows every machine in the VPC, including ones that should never touch the database. A group reference allows exactly the tasks using that group.

How is a security group different from a network ACL?

A network ACL sits on a subnet, is stateless (replies need their own rule) and can deny. A security group sits on a network card, is stateful and only allows. MiniTickets uses the default ACLs and does all filtering with security groups.

What changes in CloudFront mode?

alb-sg then allows only TCP 80 from the AWS managed prefix list com.amazonaws.global.cloudfront.origin-facing, and a secret header on the listener proves the request came from our own distribution.

Why do db-sg and redis-sg have no outbound rules?

Postgres and Valkey never start a connection. Replies still flow because groups are stateful. An empty egress list means a compromised database cannot call out.

Advanced: Rule names and the CloudFront mode

Each rule is its own resource with a name like minitickets-dev-redis-in-6379, created with referencedSecurityGroupId. The ALB’s public rules live in infra/src/entry/alb-public-ingress.ts → createAlbPublicIngress and depend on the mode. The alb, web and api groups allow all outbound traffic, so the ALB reaches the tasks and the tasks reach AWS APIs.

Answer these without scrolling up. If one feels hard, reread that section.

  1. What does “stateful” mean for a reply from Postgres?
  2. What does a caller see when no rule matches?
  3. Read the db-sg rule out loud, with port and source.
  4. Why are ports 13000 and 14000 open, and between which groups?
  5. Which two pieces use the API security group?

Redraw it from memory: the chain of five security groups with the port on each arrow

Try this: on the map, select the API security group, turn on only the Security groups layer, and read its two inbound rules out loud

Read this book online: https://book.theversehub.in/