Granting Permissions with Execution Roles and Resource-Based Policies
This lesson explains the two policies that control a function's security: the execution role decides what the function can do, and the resource-based policy decides who can invoke it. Most Lambda access errors come from fixing the wrong one.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish the execution role from the function's resource-based policy.
- Identify which policy needs changing when a function cannot call a service or a service cannot invoke the function.
- Explain how a resource-based policy is restricted to a specific source, such as one Amazon S3 bucket.
Execution role vs resource-based policy
Two separate policies govern the security of a Lambda function, one for each direction of access.
The execution role is an IAM role that Lambda assumes when it runs the function. The temporary credentials of that role are provided to the code through environment variables, and the AWS SDK uses them automatically, so the role's permissions decide which AWS APIs the code can call. If the function receives AccessDenied when writing to DynamoDB or reading from S3, the execution role is missing a permission. The role's trust policy must allow lambda.amazonaws.com to assume it.
The resource-based policy is attached to the function itself and lists the principals that may invoke it, such as the S3 service for a particular bucket, an SNS topic or another AWS account. If a trigger is configured but the function never runs, the resource-based policy usually lacks a statement for that trigger. Granting another account access through the resource-based policy also avoids the need for a cross-account role.
The following table contrasts the two policies.
AccessDenied calling a servicelambda:InvokeFunctionThe following list describes the recommended practices for each of them.
- Scope the execution role to the specific tables, buckets and queues used; SAM policy templates help.
- The console adds resource-based policies for you. In infrastructure as code you must define them yourself.
- Restrict resource-based policies with
SourceArnandSourceAccountso only the intended resource can invoke the function.
Example. Allow one S3 bucket in one account to invoke a function:
aws lambda add-permission --function-name resize-image \
--statement-id s3-uploads-invoke \
--action lambda:InvokeFunction \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::photo-uploads \
--source-account 111122223333Permissions for each kind of trigger
Where a trigger's permission belongs depends on which party calls lambda:InvokeFunction, and that depends on the invocation model.
Services that push events, such as S3, SNS, EventBridge and API Gateway, call the function themselves. They are not IAM principals in your account, so they cannot be given an identity policy; instead, the function's resource-based policy must allow the service principal, ideally restricted to the specific bucket, topic, rule or API by its ARN.
For services that are polled, such as SQS, Kinesis and DynamoDB Streams, the caller is Lambda's own event source mapping, which runs with the function's execution role. No resource-based policy statement is needed, but the execution role must allow the poller to read from the source and, for SQS, to delete processed messages. A missing permission here appears as an event source mapping that fails to enable or stays in a failed state.
An application or user that calls the Invoke API directly with the SDK is an ordinary IAM principal, so its own identity policy must allow lambda:InvokeFunction on the function. The following table summarises the three cases.
| Trigger | Who invokes the function | Permission needed |
|---|---|---|
| S3, SNS, EventBridge, API Gateway, function URLs | The service itself | A statement in the function's resource-based policy |
| SQS, Kinesis, DynamoDB Streams, Amazon MSK | Lambda's own poller | Read permissions in the execution role, such as sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes |
| Another application using the SDK | An IAM principal | lambda:InvokeFunction in that principal's identity policy |
AWS provides managed policies that grant the permissions required by common triggers, and a few further permissions are needed in specific situations. The following list describes them.
AWSLambdaBasicExecutionRolegrants the CloudWatch Logs permissions every function needs; without it, the function runs but produces no logs.AWSLambdaSQSQueueExecutionRole,AWSLambdaKinesisExecutionRoleandAWSLambdaDynamoDBExecutionRolecover the common event source mappings.- A developer who creates a function with a role needs
iam:PassRoleon that role. - Callers of a function URL need
lambda:InvokeFunctionUrland, for URLs created since October 2025,lambda:InvokeFunctionas well, restricted with thelambda:InvokedViaFunctionUrlcondition key.
Two different access denied errors
A developer sees two errors on the same day and fixes them in different places:
| Error | Meaning | Fix |
|---|---|---|
| S3 upload notifications to the function fail with an access error | S3 is not allowed to invoke the function | Add a statement to the function's resource-based policy for s3.amazonaws.com with the bucket's ARN as source |
The function's call to dynamodb:PutItem returns AccessDeniedException | The function is not allowed to call DynamoDB | Add dynamodb:PutItem on the table to the function's execution role |
The rule is that the resource-based policy states who may invoke the function, and the execution role states what the function may do. Each error is fixed in exactly one of the two places.
A function cannot write to a DynamoDB table, and separately, S3 cannot invoke the function. Which policy fixes each problem?
Show answer
The execution role needs the DynamoDB write permission, because it controls what the function can call. The function's resource-based policy needs to allow S3 to call lambda:InvokeFunction, because it controls who can invoke the function.
Exam Tips
Remember for the exam
AccessDeniedwhen the function calls DynamoDB: fix the execution role.- S3 cannot trigger the function: fix the function's resource-based policy.
- An SQS trigger fails with access denied: the execution role needs the SQS receive, delete and attribute permissions.
- Only one bucket should invoke the function: add a
SourceArncondition. - A function that runs but writes no logs lacks the CloudWatch Logs permissions in
AWSLambdaBasicExecutionRole.