Lambda execution role - AWS Lambda

Lambda execution role

A Lambda function's execution role is an AWS Identity and Access Management (IAM) role that grants the function permission to access AWS services and resources. You provide this role when you create a function, and Lambda assumes the role when your function is invoked. You can create an execution role for development that has permission to send logs to Amazon CloudWatch and to upload trace data to AWS X-Ray.

To view a function's execution role

  1. Open the Functions page of the Lambda console.

  2. Choose the name of a function.

  3. Choose Configuration, and then choose Permissions.

  4. Under Resource summary, review the services and resources that the function can access.

  5. Choose a service from the dropdown list to see permissions related to that service.

You can add or remove permissions from a function's execution role at any time, or configure your function to use a different role. Add permissions for any services that your function calls with the AWS SDK, and for services that Lambda uses to enable optional features.

When you add permissions to your function, update its code or configuration as well. This forces running instances of your function, which have outdated credentials, to stop and be replaced.

Creating an execution role in the IAM console

By default, Lambda creates an execution role with minimal permissions when you create a function in the Lambda console. You can also create an execution role in the IAM console.

To create an execution role in the IAM console

  1. Open the Roles page in the IAM console.

  2. Choose Create role.

  3. Under Common use cases, choose Lambda.

  4. Choose Next: Permissions.

  5. Under Attach permissions policies, choose the AWS managed policies AWSLambdaBasicExecutionRole and AWSXRayDaemonWriteAccess.

  6. Choose Next: Tags.

  7. Choose Next: Review.

  8. For Role name, enter lambda-role.

  9. Choose Create role.

For detailed instructions, see Creating a role for an AWS service (console) in the IAM User Guide.

Grant least privilege access to your Lambda execution role

When you first create an IAM role for your Lambda function during the development phase, you might sometimes grant permissions beyond what is required. Before publishing your function in the production environment, as a best practice, adjust the policy to include only the required permissions. For more information, see Apply least-privilege permissions in the IAM User Guide.

Use IAM Access Analyzer to help identify the required permissions for the IAM execution role policy. IAM Access Analyzer reviews your AWS CloudTrail logs over the date range that you specify and generates a policy template with only the permissions that the function used during that time. You can use the template to create a managed policy with fine-grained permissions, and then attach it to the IAM role. That way, you grant only the permissions that the role needs to interact with AWS resources for your specific use case.

For more information, see Generate policies based on access activity in the IAM User Guide.

Managing roles with the IAM API

To create an execution role with the AWS Command Line Interface (AWS CLI), use the create-role command.

In the following example, you specify the trust policy inline. Requirements for escaping quotes in the JSON string vary depending on your shell.

aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": ""}, "Action": "sts:AssumeRole"}]}'

You can also define the trust policy for the role using a JSON file. In the following example, trust-policy.json is a file in the current directory. This trust policy allows Lambda to use the role's permissions by giving the service principal permission to call the AWS Security Token Service (AWS STS) AssumeRole action.

Example trust-policy.json

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "" }, "Action": "sts:AssumeRole" } ] }
aws iam create-role --role-name lambda-ex --assume-role-policy-document file://trust-policy.json

You should see the following output:

{ "Role": { "Path": "/", "RoleName": "lambda-ex", "RoleId": "AROAQFOXMPL6TZ6ITKWND", "Arn": "arn:aws:iam::123456789012:role/lambda-ex", "CreateDate": "2020-01-17T23:19:12Z", "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "" }, "Action": "sts:AssumeRole" } ] } } }

To add permissions to the role, use the attach-policy-to-role command. Start by adding the AWSLambdaBasicExecutionRole managed policy.

aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Session duration for temporary security credentials

Lambda assumes the execution role associated with your function to fetch temporary security credentials which are then available as environment variables during a function's invocation. The temporary credentials have a limited lifetime and might not be valid after a request is processed. The maximum session duration setting for an IAM role doesn't limit sessions that are assumed by AWS services such as Lambda. Use the sts:AssumeRole action if you need more control over session duration.

AWS managed policies for Lambda features

The following AWS managed policies provide permissions that are required to use Lambda features.

Change Description Date

AWSLambdaBasicExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaBasicExecutionRole grants permissions to upload logs to CloudWatch.

February 14, 2022

AWSLambdaDynamoDBExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaDynamoDBExecutionRole grants permissions to read records from an Amazon DynamoDB stream and write to CloudWatch Logs.

February 14, 2022

AWSLambdaKinesisExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaKinesisExecutionRole grants permissions to read events from an Amazon Kinesis data stream and write to CloudWatch Logs.

February 14, 2022

AWSLambdaMSKExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaMSKExecutionRole grants permissions to read and access records from an Amazon Managed Streaming for Apache Kafka (Amazon MSK) cluster, manage elastic network interfaces (ENIs), and write to CloudWatch Logs.

February 14, 2022

AWSLambdaSQSQueueExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaSQSQueueExecutionRole grants permissions to read a message from an Amazon Simple Queue Service (Amazon SQS) queue and write to CloudWatch Logs.

February 14, 2022

AWSLambdaVPCAccessExecutionRole – Lambda started tracking changes to this policy.

AWSLambdaVPCAccessExecutionRole grants permissions to manage ENIs within an Amazon VPC and write to CloudWatch Logs.

February 14, 2022

AWSXRayDaemonWriteAccess – Lambda started tracking changes to this policy.

AWSXRayDaemonWriteAccess grants permissions to upload trace data to X-Ray.

February 14, 2022

CloudWatchLambdaInsightsExecutionRolePolicy – Lambda started tracking changes to this policy.

CloudWatchLambdaInsightsExecutionRolePolicy grants permissions to write runtime metrics to CloudWatch Lambda Insights.

February 14, 2022

AmazonS3ObjectLambdaExecutionRolePolicy – Lambda started tracking changes to this policy.

AmazonS3ObjectLambdaExecutionRolePolicy grants permissions to interact with Amazon Simple Storage Service (Amazon S3) object Lambda and to write to CloudWatch Logs.

February 14, 2022

For some features, the Lambda console attempts to add missing permissions to your execution role in a customer managed policy. These policies can become numerous. To avoid creating extra policies, add the relevant AWS managed policies to your execution role before enabling features.

When you use an event source mapping to invoke your function, Lambda uses the execution role to read event data. For example, an event source mapping for Kinesis reads events from a data stream and sends them to your function in batches. You can use event source mappings with the following services:

In addition to the AWS managed policies, the Lambda console provides templates for creating a custom policy with permissions for additional use cases. When you create a function in the Lambda console, you can choose to create a new execution role with permissions from one or more templates. These templates are also applied automatically when you create a function from a blueprint, or when you configure options that require access to other services. Example templates are available in this guide's GitHub repository.

Working with Lambda execution environment credentials

It's common for your Lambda function code to make API requests to other AWS services. To make these requests, Lambda generates an ephemeral set of credentials by assuming your function's execution role. Lambda also automatically injects the source function Amazon Resource Name (ARN) into the credentials context used to make the API request from your function code.

Lambda injects the source function ARN into the credentials context only if the request is an AWS API request that comes from within your execution environment. AWS API calls that Lambda makes outside of your execution environment using the same execution role, even if done on your behalf, don't contain the source function ARN. Examples of such API calls outside the execution environment include:

  • Calls to CloudWatch for logging.

With the source function ARN in the credentials context, you can verify whether a call to your resource came from a specific Lambda function's code. To verify this, use the lambda:sourceFunctionArn condition key in an IAM resource-based policy. Using this condition key in your policies, you can implement security controls for the API actions that your function code makes to other AWS services. This has a few key security applications, such as helping you identify the source of a credential leak.


The lambda:SourceFunctionArn condition key is different from the lambda:FunctionArn condition key. The lambda:FunctionArn condition key applies only to event source mappings and helps define which functions your event source can invoke. The lambda:SourceFunctionArn condition key can apply to any AWS API action in a resource-based policy.

You can use the lambda:SourceFunctionArn condition key in any resource-based policy for any AWS resource that might be the target of a Lambda function invocation. To use lambda:SourceFunctionArn in your policy, include it as a condition with any of the ARN condition operators. The value of the key must be a valid ARN.

For example, suppose your Lambda function code makes an s3:PutObject call that targets a specific Amazon S3 bucket. You might want to allow only one specific Lambda function to have s3:PutObject access that bucket. In this case, you might have an S3 resource policy that looks like this:

Example policy granting a specific Lambda function access to an Amazon S3 resource

{ "Version": "2012-10-17", "Statement": [ { "Sid": "ExampleSourceFunctionArn", "Effect": "Allow", "Principal": { "AWS": "123456789012" }, "Action": "s3:PutObject", "Resource": "arn:aws:s3:::lambda_bucket/*", "Condition": { "ArnEquals": { "lambda:SourceFunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:source_lambda" } } } ] }

This policy allows only s3:PutObject access if the source is the Lambda function with ARN arn:aws:lambda:us-east-1:123456789012:function:source_lambda. This policy doesn't allow s3:PutObject access to any other calling identity. This is true even if a different function or entity makes an s3:PutObject call with the same execution role.

You can also use lambda:SourceFunctionArn together with other condition keys to fine-tune access to your resources. For example, suppose you want to restrict access to your bucket to either a single Lambda function's code or to a few specific IP addresses. The following policy includes lambda:SourceFunctionArn along with the aws:SourceIp condition key.

Example policy denying access to Amazon S3 under specific conditions

{ "Version": "2012-10-17", "Statement": [ { "Sid": "ExampleMultipleConditionDeny", "Effect": "Deny", "Principal": { "AWS": "123456789012" }, "Action": "s3:*", "Resource": "arn:aws:s3:::lambda_bucket/*", "Condition": { "NotIpAddressIfExists": { "aws:SourceIp": [ "", "" ] }, "ArnNotEqualsIfExists": { "lambda:SourceFunctionArn": "arn:aws:lambda:*:123456789012:function:source_lambda" } } } ] }

This policy denies all S3 actions unless they come from a specific Lambda function with ARN arn:aws:lambda:*:123456789012:function:source_lambda, or unless they come from the specified IP range. The NotIpAddressIfExists operator tells IAM to process this condition only if the aws:SourceIp key is present in the request. Similarly, IAM considers the ArnNotEqualsIfExists operator only if the lambda:SourceFunctionArn exists.