Chapter 93: AWS Lambda and Serverless for DevOps
93.4 Execution role and least privilege
Story (fictional teaching scenario). At a fictional Kochi training team, Kabir nearly gives a thumbnail function administrator access. Amira from Rabat asks which exact object prefix it needs to read instead.
Why
One function bug should not become permission to change the whole account.
What
- IAM is AWS Identity and Access Management.
- Execution role is the identity the function assumes to call AWS services.
- Policy lists allowed actions and resources.
- Least privilege means only the required actions on the required resources.
- Trust policy answers who may assume a role; an access policy answers what that role may do. Permission to invoke the function is separate from its execution role.
How
-
List the exact business operations.
-
Restrict the resource path.
-
Keep AWS keys out of code and environment variables; use the execution role for AWS access.
Example 1: Read-only permission fragment
- Read the input and the code.
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::example-training-input/incoming/*"}
- Read the result.
Output (illustrative; not executed on AWS)
Read incoming/photo.jpg: allowed if other policy checks permit
Delete incoming/photo.jpg: not granted by this statement
- What each part means:
Effectpermits rather than denies.Actionallows object reads only.Resourcenarrows the bucket and prefix. This is a teaching fragment, not a complete deployable role. The example bucket is not a real resource being accessed. Logging permissions must also be scoped as appropriate.
Example 2: Trust and invocation are different
- Read the input and the code.
Trust: Lambda service can assume the execution role.
Invocation: only the chosen event source/caller may invoke the function.
- Read the result.
Output (illustrative; not executed on AWS)
Caller invokes -> Lambda runs -> execution role authorizes AWS API calls
- What each part means: The caller is not automatically granted the role permissions. Conversely, adding S3 read access to the role does not wire S3 events or allow everyone to invoke the function.
Ravindra Bagale's Tip
A role is permission, not a connection to an event source.
Role म्हणजे permission; event source ची जोडणी नाही.
Role permission है, event source का connection नहीं.
Did you understand? Let us try it once more.