Create the IAM execution role
Create an IAM role and provide an assume role (trust) policy that allows the Lambda service to assume the role:Version— The policy language version.Statement.Effect—Allowpermits the action.Statement.Principal— The AWS service or principal allowed to assume the role (lambda.amazonaws.comhere).Statement.Action—sts:AssumeRoleis the action that allows the principal to assume the role.
The assume role policy (also called the trust policy) establishes who can assume the role. The role still requires an identity permissions policy to specify what the Lambda function can do after assuming the role.
Grant only the permissions your Lambda function needs. Start with the managed policy
service-role/AWSLambdaBasicExecutionRole for CloudWatch Logs, then narrow permissions with a custom inline or managed policy to follow the principle of least privilege.Provide runtime permissions
A typical Lambda execution role needs permission to write logs to CloudWatch. You can grant this using the AWS managed policyAWSLambdaBasicExecutionRole or by creating a minimal custom policy that only allows the required actions.
Example: attach the AWS managed logging policy using the AWS CLI:
aws iam attach-role-policy --role-name name-picker-execution-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Alternatively, add a custom inline policy that grants only the specific permissions your function needs (for example, specific S3 or DynamoDB actions). Using managed policies is a convenient starting point; refine permissions later.
Verify the role and trust relationship
You can inspect the role and its trust relationship in the AWS Console:- Open the IAM service.
- Select “Roles”.
- Choose the role (for example,
name-picker-execution-role). - View the Trust relationships tab to confirm the assume role policy.
- View the Permissions tab to verify attached policies (for logging and any resource access).
- Every Lambda function assumes an IAM execution role that contains two parts:
- The trust policy (assume role policy) — who can assume the role (here, the Lambda service).
- The permissions policy — what the function is allowed to do (CloudWatch logs, S3, DynamoDB, etc.).
- Start with
service-role/AWSLambdaBasicExecutionRoleto enable logging, then apply least-privilege custom policies for additional resource access.