AWS Lambda:The provided execution role does not have permissions to call DescribeNetworkInterfaces on EC2

Amazon Web-ServicesAws LambdaAmazon IamPolicyRole

Amazon Web-Services Problem Overview


Today I have a new AWS Lambda question, and can't find anywhere in Google.

I new a Lambda function, there is no question. But when I input any code in this function[eg. console.log();] and click "Save", error is occured: "The provided execution role does not have permissions to call DescribeNetworkInterfaces on EC2"

exports.handler = (event, context, callback) => {
    callback(null, 'Hello from Lambda');
    console.log();  // here is my code   
}; 

I bound the function with Role: lambda_excute_execution(Policy:AmazonElasticTranscoderFullAccess) And this function is not bound with any triggers now.

And then, I give the role "AdministratorAccess" Policy, I can save my source code correctly.

This role can run Functions successfully before today.

Is anyone know this error?

Thanks Very much!

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

This error is common if you try to deploy a Lambda in a VPC without giving it the required network interface related permissions ec2:DescribeNetworkInterfaces, ec2:CreateNetworkInterface, and ec2:DeleteNetworkInterface (see AWS Forum).

For example, this a policy that allows to deploy a Lambda into a VPC:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeNetworkInterfaces",
        "ec2:CreateNetworkInterface",
        "ec2:DeleteNetworkInterface",
        "ec2:DescribeInstances",
        "ec2:AttachNetworkInterface"
      ],
      "Resource": "*"
    }
  ]
}

Solution 2 - Amazon Web-Services

If you are using terraform, just add:

resource "aws_iam_role_policy_attachment" "AWSLambdaVPCAccessExecutionRole" {
    role       = aws_iam_role.lambda.name
    policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}

Solution 3 - Amazon Web-Services

via Managed Policy

  • To grant Lambda necessary permissions to dig in to a VPC where a production RDS db resides in a private subnet.
  • As mentioned by @portatlas above, the AWSLambdaVPCAccessExecutionRole managed policy fits like a glove (and we all know use of IAM Managed Policies is an AWS-recommended best-practice).
  • This is for Lambdas with a service role already attached.

AWS CLI

1. Get Lambda Service Role
  • Ask Lambda API for function configuration, query the role from that, output to text for an unquoted return.
    aws lambda get-function-configuration \
        --function-name <<your function name or ARN here>> \
        --query Role \
        --output text
    
  • return, take your-service-role-name to #2
    your-service-role-name
    
2. Attach Managed Policy AWSLambdaVPCAccessExecutionRole to Service Role
aws iam attach-role-policy \
    --role-name your-service-role-name \
    --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole

CDK 2 TypeScript

const lambdaVPCExecutionRole:iam.Role = new iam.Role(this, `createLambdaVPCExecutionRole`, {
    roleName        : `lambdaVPCExecutionRole`,
    assumedBy       : new iam.ServicePrincipal(`lambda.amazonaws.com`),
    description     : `Lambda service role to operate within a VPC`,
    managedPolicies : [
        iam.ManagedPolicy.fromAwsManagedPolicyName(`service-role/AWSLambdaVPCAccessExecutionRole`),
    ],
});

const lambdaFunction:lambda.Function = new lambda.Function(this, `createLambdaFunction`, {
    runtime : lambda.Runtime.NODEJS_14_X,
    handler : `lambda.handler`,
    code    : lambda.AssetCode.fromAsset(`./src`),
    vpc     : vpc,
    role    : lambdaVPCExecutionRole,
});

Solution 4 - Amazon Web-Services

This is actually such a common issue.

You can resolve this by adding a custom Inline Policy to the Lambda execution role under the Permissions tab.

Just add this:

  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeNetworkInterfaces",
        "ec2:CreateNetworkInterface",
        "ec2:DeleteNetworkInterface",
        "ec2:DescribeInstances",
        "ec2:AttachNetworkInterface"
      ],
      "Resource": "*"
    }
  ]
}

There's a full tutorial with pictures here if you need more information or are confused: https://ao.ms/the-provided-execution-role-does-not-have-permissions-to-call-createnetworkinterface-on-ec2/

Additionally, a more recent sequence of steps follows:

  1. Under your Lambda Function, select "Configuration" Lambda Configuration

  2. Select "Permissions" Permissions

  3. Select the execution role: Role Selection

  4. Select "Add Permissions" Add Permissions

  5. Create Inline Policy Inline Policy

  6. Select "JSON" JSON

  7. Paste the JSON above and select Review.

Solution 5 - Amazon Web-Services

It seems like this has been answered many different ways already but as of this posting, AWS has a managed policy. If you just search for the AWSLambdaVPCAccessExecutionRole you will be able to attached that, and this method worked for me.

Here is the arn:

arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole

Solution 6 - Amazon Web-Services

Just go to execution role -> Attach policy -> Search for 'AWSLambdaVPCAccessExecutionRole' and add it.

Solution 7 - Amazon Web-Services

An example for Cloudformation and AWS SAM users.

This example lambda role definition adds the managed AWSLambdaVPCAccessExecutionRole and solves the issue:

Type: "AWS::IAM::Role"
Properties:
  RoleName: "lambda-with-vpc-access"
  ManagedPolicyArns:
    - "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
  AssumeRolePolicyDocument:
    Version: "2012-10-17"
    Statement:
      - Effect: Allow
        Action:
          - sts:AssumeRole
        Principal:
          Service:
            - lambda.amazonaws.com

Solution 8 - Amazon Web-Services

Here's a quick and dirty way of resolving the error.

Open IAM on AWS console, select the role that's attached to the Lambda function and give it the EC2FullAccess permission.

This will let you update the Lambda VPC by granting EC2 control access. Be sure to remove the permission from the role, the function still runs.

Is it more or less secure than leaving some permissions attached permanently? Debatable.

Solution 9 - Amazon Web-Services

It is definitely a strange error, but are you sure the example code you added is the one you're using in your lambda?

Because in your code, you are trying to log something in your lambda after returning control via the callback. In other words, first you told your lambda that you're done. Next, while it is busy shutting down and returning your results, you try to do some logging...

So first, I'd try this:

exports.handler = (event, context, callback) => {
    console.log('this is a test');
    // do stuff
    callback(null, 'Hello from Lambda'); // only do a callback *after* you've run all your code
};

And see if that fixes the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfisheepView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesPhilipp ClaßenView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesStéphane BruckertView Answer on Stackoverflow
Solution 3 - Amazon Web-Servicesfusion27View Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesAO_View Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesCullen DView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesJackSparrow63View Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesJani SiivolaView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesIsland_ZeroView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesHieronView Answer on Stackoverflow