Lesson 7 of 12 AWS Lambda

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.

Execution role
AnswersWhat can the function do?
Attached toAn IAM role that Lambda assumes
ExampleWrite to DynamoDB and CloudWatch Logs
Fix it whenThe function gets AccessDenied calling a service
Resource-based policy
AnswersWho can invoke the function?
Attached toThe function itself
ExampleAllow S3, SNS or API Gateway to call lambda:InvokeFunction
Fix it whenA trigger cannot invoke the function

The 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 SourceArn and SourceAccount so 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 111122223333

The following diagram shows which policy governs each direction of access for the function in the example below.

The two policies of a Lambda function. The resource-based policy on the function decides who may invoke it, such as the S3 service for one bucket; without a statement the trigger never runs. The execution role, which Lambda assumes, decides which AWS APIs the code can call; without a permission the SDK returns AccessDenied.
The resource-based policy controls calls into the function, and the execution role controls calls the function makes.
Scenario questions

Lock down who can invoke

Exam skills 1.2.4 · 2.1.6

Practise the details that make a function's permissions both work and stay narrow.

Log in to try this activity Interactive activities are free with an account.

Permissions 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.

TriggerWho invokes the functionPermission needed
S3, SNS, EventBridge, API Gateway, function URLsThe service itselfA statement in the function's resource-based policy
SQS, Kinesis, DynamoDB Streams, Amazon MSKLambda's own pollerRead permissions in the execution role, such as sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes
Another application using the SDKAn IAM principallambda: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.

  • AWSLambdaBasicExecutionRole grants the CloudWatch Logs permissions every function needs; without it, the function runs but produces no logs.
  • AWSLambdaSQSQueueExecutionRole, AWSLambdaKinesisExecutionRole and AWSLambdaDynamoDBExecutionRole cover the common event source mappings.
  • A developer who creates a function with a role needs iam:PassRole on that role.
  • Callers of a function URL need lambda:InvokeFunctionUrl and, for URLs created since October 2025, lambda:InvokeFunction as well, restricted with the lambda:InvokedViaFunctionUrl condition key.
Sort into groups

Which policy needs fixing?

Exam skills 2.1.6 · 4.1.1

Learn to tell from the symptom which of the three policies is missing a permission.

Log in to try this activity Interactive activities are free with an account.
Worked example

Two different access denied errors

A developer sees two errors on the same day and fixes them in different places:

ErrorMeaningFix
S3 upload notifications to the function fail with an access errorS3 is not allowed to invoke the functionAdd 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 AccessDeniedExceptionThe function is not allowed to call DynamoDBAdd 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.

Check yourself

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

  • AccessDenied when 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 SourceArn condition.
  • A function that runs but writes no logs lacks the CloudWatch Logs permissions in AWSLambdaBasicExecutionRole.