Sub topic 4
Giving a function permissions with an execution role
This chapter assumes you know what an IAM role and policy are, can read JSON policy documents, and have written a basic Lambda handler. You will learn how a function gets its permissions, how to scope them, and how to diagnose the errors you will see when they are wrong.
Two directions of permission
Lambda permissions run in two directions, and mixing them up is the most common source of confusion.
- The execution role controls what your function can do. It is an IAM role that Lambda assumes when it runs your code. Calls your code makes with the AWS SDK use temporary credentials from this role.
- The resource-based policy on the function controls who can invoke it. S3, API Gateway or another account needs permission here before it can call your function.
The diagram shows both.
The two parts of an execution role
Every execution role has two parts.
- A trust policy that lets the Lambda service assume the role.
- One or more permissions policies that say which actions the function may perform.
A minimal trust policy looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "lambda.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}If the trust policy is missing or names the wrong principal, Lambda cannot create the function with that role.
Writing a least-privilege permissions policy
Grant only the actions and resources the function needs. The handler from earlier chapters writes one item to a DynamoDB table, so it needs dynamodb:PutItem on that table and nothing else.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "dynamodb:PutItem",
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders"
}
]
}The managed policy AWSLambdaBasicExecutionRole adds the log permissions logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents. Attach it so the function can write to CloudWatch Logs. Without it the function still runs, but you get no logs, which makes later debugging hard.
Roles in SAM
In the SAM template from the topic plan, the Policies property builds the role for you:
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTableDynamoDBCrudPolicy is a SAM policy template. SAM expands it into a scoped policy on the named table and creates the role, the trust policy and the basic logging permissions automatically. Use the Role property instead only when you need to bring your own role.
Granting access to invokers
When API Gateway, S3 or SNS triggers your function, the service needs permission to call it. This is a resource-based policy statement added with lambda:AddPermission:
aws lambda add-permission \
--function-name orders-function \
--statement-id allow-s3 \
--action lambda:InvokeFunction \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::my-upload-bucketThe --source-arn restricts the grant to one bucket. Without it, any bucket in any account could invoke your function. SAM and the console add this statement for you when you declare an event.
Poll-based sources such as SQS differ. Lambda itself polls the queue, so the execution role needs sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes on the queue. The managed policy AWSLambdaSQSQueueExecutionRole covers these.
Diagnosing permission errors
| Symptom | Likely cause |
|---|---|
| `AccessDeniedException` or `AccessDenied` in the function's logs | The execution role lacks an action or resource that the SDK call needs |
| Trigger never fires, no invocations | The resource-based policy does not allow the calling service |
| Function runs but writes no logs | The role lacks the CloudWatch Logs permissions |
| Event source mapping fails to create or stays disabled | The role lacks permissions to read from the queue or stream |
The error message names the denied action and the role ARN. Add exactly that action on exactly that resource, then retry. Note that IAM changes can take a few seconds to take effect.
Exercise
Your function reads messages from an SQS queue and writes results to an S3 bucket. The logs show AccessDenied on s3:PutObject. Which policy do you change, the execution role or the function's resource-based policy, and why?
Show answer
Change the execution role. The failing call is made by your code to S3, which is an outbound action, so the role Lambda assumes must allow s3:PutObject on the bucket's objects (for example arn:aws:s3:::my-bucket/*). The resource-based policy only controls who can invoke the function.