AWS Builder Center

A small IAM lab: give Lambda access to one S3 object

Practice least privilege with two S3 objects and a Lambda execution role. Test that one read succeeds and the other still fails.

A good IAM exercise starts with a request that should fail. In this lab, a Lambda function reads one sample object from a private S3 bucket. The goal is to make that read work and prove the role still cannot read a second object.

Set up two test objects

In a sandbox account, create a private bucket and upload two harmless files named  allowed.txt  and  denied.txt . Use a dedicated Lambda execution role. Keep its basic logging permissions and start with no S3 permissions. For the simplest version of this exercise, keep everything in the same account and use S3-managed encryption.
Have the function call S3 GetObject with the bucket and key. Invoke it against  allowed.txt  and confirm that the read fails. Check which execution role the function actually uses before changing any policy.

Grant one read and test both keys

Add an identity policy to the execution role that allows  s3:GetObject  on  arn:aws:s3:::YOUR-BUCKET/allowed.txt . Replace  YOUR-BUCKET  with your test bucket's name. This call reads a known key, so the function does not need  s3:ListBucket .
Invoke the same function against both files. The allowed file should be readable and the second read should still fail. Use existing keys for both tests so a typo or missing object does not muddy the result. If the first call still fails, check the role, the object ARN, and any applicable explicit deny before broadening the policy.
Record the results, then delete the test files, bucket, function, role, and log group if you created them for this exercise. The part worth keeping is the two-test pattern. Each time you add a permission, write down one action that should succeed and a nearby action that should still fail.
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