95

I'm getting the error "The role defined for the function cannot be assumed by Lambda" when I'm trying to create a lambda function with create-function command.

aws lambda create-function
--region us-west-2
--function-name HelloPython
--zip-file fileb://hello_python.zip
--role arn:aws:iam::my-acc-account-id:role/default
--handler hello_python.my_handler
--runtime python2.7
--timeout 15
--memory-size 512

Zanon
  • 29,231
  • 20
  • 113
  • 126
Midhun Sudhakar
  • 1,238
  • 2
  • 10
  • 14

11 Answers11

120

I got the error "The role defined for the function cannot be assumed by Lambda" because i had not updated the roles "Trust Relationship" config file. I didn't encounter the timeout issues as in the linked answer in the comments.

The comments in the above answers pointed out that you need to add the following.

  1. Go to 'IAM > Roles > YourRoleName'
    • (Note: if your role isn't listed, then you need to create it.)
  2. Select the 'Trust Relationships' tab
  3. Select 'Edit Trust Relationship'

Mine ended up like the below.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      <your other rules>
    },
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
MarkHu
  • 1,694
  • 16
  • 29
Emile
  • 11,451
  • 5
  • 50
  • 63
  • 2
    I have the same issue, even attached to this Role policy. I have configured lambda function basic settings, Go to Lambda Console, Edit Lambda Execution Role -> Edit basic Settings -> Existing Role -> Choose the role created for lambda -> re-attach role to lambda function -> save -> it works – Xin Cai Aug 28 '20 at 05:15
  • Thanks! this saved me a lot of time! – Yor Jaggy Jan 19 '22 at 14:55
  • 2
    For everyone else with similar problem, check your "Principal.Service", in my case, I misconfigured it as "ec2.amazonws.com" and obviously it won't work for a lambda. – robsonrosa Feb 08 '22 at 11:24
53

I'm also encountering this error. Have not got a definitive answer (yet) but figured I'd pass along a couple of hints that may help you and/or anyone else hitting this problem.

A) If you build the Role ARN by putting together your account ID and role name, I think the account ID needs to be without any dashes

B) If you just created the role, and possibly added policies to it, there seems to be a (small) window of time in which the role will trigger this error. Sleeping 5 or 6 seconds between the last operation on the role and the create-function call allowed me to bypass the issue (but of course, the timing may be variable so this is at best a work-around).

FOR
  • 4,260
  • 2
  • 25
  • 36
  • 38
    +1 "Sleeping 5 or 6 seconds" is the solution here. I've had the same issue using the JavaScript SDK and waiting fixed for me. I've posted a self-answered [question](http://stackoverflow.com/q/37503075/1476885) with code example for Node. – Zanon May 28 '16 at 19:42
  • 2
    @Zanon - thanks for the pointer to your Q/A. If I had time I'd try and pass this on to the official forums, as usually when resources need time to be ready, we get a status we can query (and even waiter objects if you're using boto3 ), so this seems like an odd (undocumented afaik) inconsistency – FOR May 30 '16 at 14:50
  • 1
    `retrying` python package was a quick rescue - just decorated the creation function with the following: `@retrying.retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)` – Zaar Hai Dec 08 '16 at 15:56
  • 1
    +1 for sleeping. I don't get what the deal is with this. I have to do this with aws sdk (nodejs), and sleeping for 10 seconds works. If I create a new inline policy on aws console it shows up instantly.. hmmhmm. – ferr Jul 08 '18 at 20:36
  • 3
    I'm having trouble with it taking about 5 minutes to work sometimes. Other times it works right away, – Francis Upton IV Oct 20 '19 at 16:47
  • I was googling this because I was seeing it in terraform debug output although it would eventually work. Your B explanation explains why terraform keeps retrying and eventually it succeeds. – user1169420 Feb 05 '20 at 18:43
  • Same here . I just had to wait a bit – Mamun Feb 25 '20 at 17:25
  • I have experienced this problem again today and although I experienced it around 18 months ago I'd forgotten that waiting seems to resolve the issue so thanks for the reminder. I have a step function that includes 8 lambdas and I can keep running executions over and over after a single deployment and each time I can get one lambda stage further without changing anything. It's annoying but just have to be patient. – Mike G Apr 01 '21 at 18:22
26

For me, the issue was that I had an incomplete name for the role. I set

--role arn:aws:iam::000000000000:role/MyRoleName

when it should have been

--role arn:aws:iam::000000000000:role/service-role/MyRoleName

(of course my aws id isn't actually 000000000000)

I discovered this by running

aws iam get-role --role-name MyRoleName

and looking at the "Arn" property in the result set.

alexanderbird
  • 3,847
  • 1
  • 26
  • 35
6

I got this problem while testing lambda function.

What worked for me was formatting JSON.

Marcin Rapacz
  • 616
  • 8
  • 12
4

I am just learning to use the AWS CLI and ran into this issue.

I am using a series of PowerShell scripts to deploy an entire AWS architecture. My createRole.ps1 script contains:

aws iam create-role `
--role-name $roleName `
--assume-role-policy-document file://myRoleTrustPolicy.json

The file myRoleTrustPolicy.json contains:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "elasticmapreduce.amazonaws.com",
          "datapipeline.amazonaws.com",
          "lambda.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

It is the "lambda.amazonaws.com" line that was missing from Service list that was causing the issue.

Once I fixed that, the invocation of aws lambda create-function worked great.

aws lambda create-function `
--function-name $fn `
--runtime java8 `
--role $currentRoleARN `
--handler "handleRequest" `
--memory-size 128 `
--zip-file $jarFile 
djhallx
  • 690
  • 6
  • 17
2

For me, the issue was that I had set the wrong default region environment key.

jsta
  • 3,216
  • 25
  • 35
2

Had the same issue although my IAM role did have the right policy and trust relationship. Lambda creation worked fine when done through CLI the problem was when using lambda module after just creating the IAM role.

I also tried to "pause" for few seconds but it didn't help.

Ended up adding retry and delay until registerdLambda.code was defined. Usually it works after 1-2 tries.

example:

 - name: creating lambda function
   lambda:
     state: present
     name: "{{ lambdaName }}"
     zip_file: "{{ lambdaZipFile }}"
     runtime: "{{ lambdaRuntime }}"
     role: "{{ lambdaRole }}"
     description: "{{ lambdaDescription }}"
     handler: "{{ lambdaHandler }}"
   register: lambdaFunc
   retries: 3
   delay: 10
   until: "{{ lambdaFunc.code is defined }}"
balaganAtomi
  • 550
  • 8
  • 13
  • 1
    I had a similar error... all dependencies were correctly declared but the IAM role would be reported as non-existing or couldn't be assumed by lambda even though it shows up as created before the function started to create in the cloudformation event log. My hack to get around this was to declare yet another dependency on a resource that wasn't needed by the lambda function but would "waste time" creating it and that was long enough for the IAM role to get into a state where it would not be a problem to be used by the function. Its a hack and it seems like an AWS issue. BTW this was w/SAM. – sjwoodr Aug 26 '21 at 16:54
2

I had this error simply because I had a typo in the role ARN. I really wish the error was more explicit and said something along the lines of "this role doesn't exist", but alas.

radu.ciorba
  • 1,024
  • 1
  • 8
  • 14
0

Most people end up in this error because of giving the wrong Role ARN in CloudFormation while creating the Lambda Function.

Make sure the role is completed first by using "DependsOn" and use the intrinsic function """{ "Fn::GetAtt" : [ "your-role-logical-name", "Arn" ] }"""

Vignesh
  • 1
  • 1
0

I was running into this error with terraform and needed to add an assume role policy and apply it to the role that lambda assumes.

data "aws_iam_policy_document" "lambda_assume_role_policy" {

  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = [
        "lambda.amazonaws.com"
      ]
    }
  }

resource "aws_iam_role" "lambda_rotation_role" {
  name               = "lambda-rotation-role"
  assume_role_policy = "${data.aws_iam_policy_document.lambda_assume_role_policy.json}"
}
Michael
  • 1,577
  • 1
  • 18
  • 33
0

It could be that the Lambda is missing an execution role. Or this role has been deleted.

In console you can see the status at Lambda > Functions > YourFunction > Permissions. Even an IAM empty role with no policies is enough to make it work.

inmyth
  • 8,880
  • 4
  • 47
  • 52