Search code examples
amazon-web-servicesamazon-iampolicy

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


I know that I can fix this policy with AWS managed role or wildcard on resource, but my question is: ¿How can I specify what is the resource performing those actions over ec2:?

I want to set specific lambda to perform those actions in my policy, like this. I've tried, but is throwing same error above, it only works if I place * for resource.

{
    Action   = [
        "ec2:AttachNetworkInterface",
        "ec2:CreateNetworkInterface",
        "ec2:DeleteNetworkInterface",
        "ec2:DescribeInstances",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DetachNetworkInterface",
        "ec2:ModifyNetworkInterfaceAttribute",
        "ec2:ResetNetworkInterfaceAttribute",
    ]
    Effect   = "Allow"
    Resource = "arn:aws:lambda:<region>:<acc_id>:function:myfunction"
}

Solution

  • First of all, when defining an IAM policy, the Resource is where these actions are applied to, rather than who can perform these actions. So the Resource here wouldn't be a lambda, but would be some EC2 resources. The resource that performs these actions (your lambda) is where you attach the IAM role.

    That being said, not all EC2 actions have resource-level permissions, and DescribeNetworkInterfaces is one that doesn't. This means that you wouldn't be able to specify a specific resource for this action, and have to use '*'.

    References:

    https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policy-structure.html#ec2-supported-iam-actions-resources

    https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonec2.html