fish
fish

Reputation: 2513

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

When I input any code in this function (e.g. console.log();) and click "Save", an error occurs:

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 could run Functions successfully before today.

Does anyone know this error?

Upvotes: 205

Views: 223664

Answers (13)

Philipp Claßen
Philipp Claßen

Reputation: 43979

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.

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": "*"
    }
  ]
}

Upvotes: 315

Andy B
Andy B

Reputation: 1

Also check your permission boundaries on your lambda execution role, that one got me.

Upvotes: 0

AO_
AO_

Reputation: 2874

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 (Terraform, CloudFormation, and AWS Console) or are confused: https://ataiva.com/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.

Upvotes: 57

Ari Waisberg
Ari Waisberg

Reputation: 1303

If you are using SAM you just need to add to the Globals in the Template, like this:

Globals:
  Function:
    VpcConfig:
      SecurityGroupIds:
        - sg-01eeb769XX2d6cc9b
      SubnetIds:
        - subnet-1a0XX614
        - subnet-c6dXXb8b
        - subnet-757XX92a
        - subnet-8afXX9ab
        - subnet-caeXX7ac
        - subnet-b09XXd81

(of course, you can put all in variables, or parameters!)

and then, to the Lambda Function, add Policies to the Properties, like this:

  BasicFunction:
    Type: AWS::Serverless::Function
    Properties:
      Policies:
      - AWSLambdaVPCAccessExecutionRole
      - AWSLambdaBasicExecutionRole

Upvotes: 1

Rizxcviii
Rizxcviii

Reputation: 118

After a bit of experimentation, here is a solution using "least privilege". It's written in Python, for the AWS CDK. However the same could be applied to normal JSON

iam.PolicyDocument(
    statements=[
        iam.PolicyStatement(
            effect=iam.Effect.ALLOW,
            actions=["ec2:DescribeNetworkInterfaces"],
            resources=["*"],
        ),
        iam.PolicyStatement(
            effect=iam.Effect.ALLOW,
            actions=["ec2:CreateNetworkInterface"],
            resources=[
                f"arn:aws:ec2:{region}:{account_id}:subnet/{subnet_id}"
                f"arn:aws:ec2:{region}:{account_id}:security-group/{security_group_id}",
                f"arn:aws:ec2:{region}:{account_id}:network-interface/*",
            ],
        ),
        iam.PolicyStatement(
            effect=iam.Effect.ALLOW,
            actions=["ec2:DeleteNetworkInterface"],
            resources=[f"arn:aws:ec2:{region}:{account_id}:*/*"],
        ),
    ],
),

Upvotes: 4

Reece
Reece

Reputation: 691

Just cause there aren't enough answers already ;) I think this is the easiest way. If you're using the web admin console, when you're creating your Lambda function in the first place, down the bottom just expand 'Advanced Settings' and check 'Enable VPC' & choose your vpc... Simple! Before doing this, my connection to my RDS proxy was timing out. After doing this (and nothing else) - works great! Image of VPC setup for new Lambda function

Upvotes: 2

fusion27
fusion27

Reputation: 2636

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,
});

Upvotes: 67

JackSparrow63
JackSparrow63

Reputation: 131

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

Upvotes: 13

Jani Siivola
Jani Siivola

Reputation: 730

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

Upvotes: 12

Cullen D
Cullen D

Reputation: 561

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

Upvotes: 24

user13025316
user13025316

Reputation:

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.

Upvotes: 1

St&#233;phane Bruckert
St&#233;phane Bruckert

Reputation: 22903

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"
}

Upvotes: 118

Hieron
Hieron

Reputation: 459

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.

Upvotes: 0

Related Questions