AWS Builder Center

One bucket, one Lambda role, no wildcard

A Lambda function that reads one S3 prefix needs only GetObject on that prefix, plus separate log permissions. This article shows that small role and how to test the denial.

Full-stack Developer & Security Researcher
I kept seeing Lambda roles with s3:* on * in small projects. It works until one key leaks or one bucket name is wrong, then it works too well. Here is the shape I use instead, for a Lambda that only reads one prefix. Trust policy: only lambda.amazonaws.com can assume it, nothing else. Permissions: one Allow on s3:GetObject, resource arn:aws:s3:::my-bucket/uploads/* only. No ListBucket unless the code actually lists. No PutObject unless the code actually writes. If the function needs logs, add a separate statement for logs:CreateLogGroup, CreateLogStream, PutLogEvents on the function's log group only. Test it before shipping: call the function with a key outside the prefix and confirm it fails. Call it with a key inside and confirm it reads. If both pass, the boundary is real, not just written down. Two things that break this in practice. First, code that lists the bucket to check a key exists needs ListBucket on the bucket ARN, not on the object ARN, and people add s3:* to fix it fast. Second, a shared role reused by three functions grows back to a wildcard. One function, one role, one prefix. When the second function arrives, it gets its own role. That is the whole rule. Small scope, tested denial, no wildcard to clean up later.
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