The problem
NexaCore had around ten employees using a single AWS root account. The credentials were shared in chat, so there was no way to tell who did what, and a single leak would have handed over full control of the account, billing included.
What was wrong
- No accountability: every action showed up as “root”.
- No visibility: nothing recorded API activity across regions.
- Total blast radius: every person had unrestricted access to everything.
- Credentials lived in Teams and Slack message history.
Access design: what each team actually needs
Before writing any Terraform I mapped each team to the smallest set of permissions it needs to do its job, and wrote down what each one should not be able to do. Explicit denials matter as much as grants.
| Group | Users | Allowed | Explicitly not allowed |
| Developers | 4 | EC2 and S3 on development resources, read-only CloudWatch Logs | Any production access |
| Operations | 2 | Broad infrastructure management | IAM write actions (blocks privilege escalation) |
| Finance | 1 | AWS-managed Billing + ReadOnlyAccess | Any change to resources |
| Analysts | 3 | Read-only on one analytics bucket and RDS metadata | Everything else |
Architecture
BeforeShared root accountretired, locked away
↓ replaced by
Identity10 IAM usersfor_each · one per person
MFA policyattached to every group
Password policy14 chars · 90-day rotation
↓ member of
Groups · least privilegeDevelopers
Operations
Finance
Analysts
↓ every API call recorded by
AuditCloudTrailmulti-region · log validation
S3 log bucketpublic access blocked
SNSsecurity alerts
Problems I solved along the way
01
CloudTrail “worked” but delivered zero logs
SymptomThe trail created successfully, but no log files ever arrived in S3. Nothing errored.
FixCloudTrail needs an explicit bucket policy that lets its service principal write. I added one scoped to exactly that permission, and a depends_on so the policy exists before the trail, so logs flow from the very first run.
02
Operations could have granted themselves admin
Risk“Broad infrastructure management” included IAM, so an ops user could attach any policy to themselves.
FixAn explicit deny on IAM write actions for the Operations group. A deny always wins over an allow, so the escalation path is closed.
03
Copy-pasted user blocks would drift
RiskOne resource block per user means typos and inconsistent group membership as the team grows.
Fixfor_each over a list of names creates each user and its membership. Adding a person is a one-word change.
In the code
The trail and its dependency on the bucket policy, from main.tf:
resource "aws_cloudtrail" "main" {
name = "nexacore-audit-trail"
s3_bucket_name = aws_s3_bucket.cloudtrail.id
include_global_service_events = true
is_multi_region_trail = true
enable_log_file_validation = true
# without the bucket policy, the trail is created but no logs are delivered
depends_on = [aws_s3_bucket_policy.cloudtrail]
}
Users created from a list instead of copy-pasted blocks, from iam-user.tf:
resource "aws_iam_user" "developers" {
for_each = toset(["dev-1", "dev-2", "dev-3", "dev-4"])
name = each.key
}
resource "aws_iam_user_group_membership" "dev_membership" {
for_each = aws_iam_user.developers
user = each.value.name
groups = [aws_iam_group.developers.name]
}
How I verified it
I tested every group against real AWS resources and the IAM Policy Simulator, checking both sides of the matrix: allowed actions succeed, and denied actions fail loudly with AccessDenied instead of failing silently.
- Developers can manage dev EC2 and S3 but get
AccessDenied on production resources.
- Operations can manage infrastructure but cannot create or attach IAM policies.
- Finance can see billing but cannot change anything.
- Every test action appeared in CloudTrail under the individual user’s name.
What I learned
- Design the access matrix, denials included, before writing any policy.
- Least-privilege JSON policies with
jsonencode() and conditions are easier to review than console clicks.
- “Created successfully” is not “working”: check that logs actually arrive.
for_each keeps identity code consistent as teams grow.
Next case studyServiceHub: a three-tier platform built to stay up
Read →