A team creates one role called lambda-shared-role for its first function. Each new function reuses it, and each one adds the permissions it needs. A year later the function that resizes thumbnails can also read the orders table and send email. One vulnerable package in any function now gives an attacker everything that all of them can do.
A Lambda function runs with the permissions of its execution role. AWS says it plainly: sharing an IAM role within more than one Lambda function will likely violate least-privileged access.
Create the role in the same stack or module as the function and name it after the function. The role then changes in the same pull request as the code, and is deleted when the function is.
IAM does not charge for roles. An account can hold 1,000 by default, and you can ask for up to 10,000.
Northwind's 14 functions all use lambda-shared-role, which has AmazonS3FullAccess and AmazonDynamoDBFullAccess attached.
❌ Figure: Bad example - Bad example - Every function can read and delete every bucket and table in the account
resize-thumbnail has its own role. It can write its logs, read from northwind-uploads and write to northwind-thumbnails.
✅ Figure: Good example - Good example - A compromised function can only touch the two buckets it works with
Every function needs to write logs. The AWSLambdaBasicExecutionRole managed policy allows that and nothing else. Start there, then add the specific actions on the specific resources:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": ["dynamodb:GetItem", "dynamodb:Query"],"Resource": "arn:aws:dynamodb:ap-southeast-2:111122223333:table/orders"}]}
Stay away from managed policies that end in FullAccess, and from "Action": "*" or "Resource": "*" in your own.
You rarely have to write the policy by hand:
DynamoDBReadPolicy that are scoped to one tabletable.grants.readData(fn)The execution role says what the function can do. The function's resource-based policy says who can call it.
When you let a service such as S3 invoke the function, add aws:SourceAccount and aws:SourceArn conditions, so only your bucket in your account can trigger it. Security Hub CSPM flags any function whose policy allows public access.
The same principle applies to people and to AI resources. See Do you give users least privileges? and Do you give least-privilege access to your AI resources?