aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Create Lambda in VPC intermittently errors when trying to CreateNetworkInterface

Open rjoseph-resilient opened this issue 6 years ago • 4 comments

:question: General Issue

The Question

Hi,

I have come across an intermittent error when using the CDK to create a Lambda within the VPC.

Intermittent error:

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

As stated, this error does not occur all of the time and the Lambda is created within the VPC as expected. Therefore (I believe) this seems to boil down to a timing issue, somewhere between Role creation, attaching policies to a Role and Lambda making use of that Role.

Please may you let me know as to how this can be resolved?

Kind Regards,

RJ

Environment

  • CDK CLI Version: 1.38.0 (build d5fa31f)
  • Module Version: all modules (cdk, ec2, iam, lambda) use version 1.38.0
  • OS: Ubuntu
  • Language: TypeScript

Other information

First thoughts, the Lambda is (potentially) invoking actions to create required interfaces before the assigned execution Role has had it's policies attached. If this is the case, it would appear that the CDK does not perform the following (expected/assumed) check: If a Policy has been defined with a Role, only attempt to construct the Lambda within the VPC, once the CDK has confirmed that the Lambda's assigned execution Role has the required Policy applied.

Additionally, for further context please see an example breakdown of how the Lambda and its Role are constructed within the same file below...

The Role is created like so:

    // Role
    const actionHandlerRole = new iam.Role(this, 'Role', {
      roleName: 'actionHandler-role',
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
    });

The PolicyStatements for the Role:

    // PolicyStatements
    const cloudWatchPolicyStatement = new iam.PolicyStatement({
      effect: iam.Effect.ALLOW,
      sid: 'AllowCloudWatch',
      resources: [`arn:aws:logs:AWS_REGION:AWS_ACCOUNT}:log-group:/aws/lambda/*:*:*`],
      actions: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents']
    });
    const ec2PolicyStatement = new iam.PolicyStatement({
      effect: iam.Effect.ALLOW,
      sid: 'AllowEc2',
      resources: ['*'],
      actions: ['ec2:CreateNetworkInterface', 'ec2:DescribeNetworkInterfaces', 'ec2:DeleteNetworkInterface']
    });

The Policy is created with the defined Role and PolicyStatements:

    // Policy
    const policy = new iam.Policy(this, 'Policy', {
      policyName: `actionHandler-policy`,
      roles: [actionHandlerRole],
      statements: [ec2PolicyStatement, cloudWatchPolicyStatement]
    });

Finally the Lambda is created like below:

    // Lambda
    const lamdaFn = new lambda.Function(this, 'Function', {
      functionName: 'actionHandler-function',
      runtime: lambda.Runtime.NODEJS_12_X,
      code: lambda.Code.fromAsset('./.build/bundled'),
      handler: 'action.handler',
      role: actionHandlerRole,
      timeout: cdk.Duration.seconds(15),
      vpc,
      securityGroups: [securityGroup]
    });
```
<!-- e.g. detailed explanation, stacktraces, related issues, suggestions on how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc -->

rjoseph-resilient avatar May 15 '20 12:05 rjoseph-resilient

To build on this more...

When the Lambda does successfully create and the below command is run immediately after:

aws lambda invoke --function-name actionHandler-function lambda-output.txt

I am seeing another intermittent issue that results in the command being successful or the command failing with either of the following errors:

An error occurred (ServiceException) when calling the Invoke operation (reached max retries: 4): 
Lambda was unable to decrypt the environment variables due to an internal service error. 

or

An error occurred (AccessDeniedException) when calling the Invoke operation: The role defined for the function cannot be assumed by Lambda.

rjoseph-resilient avatar May 15 '20 16:05 rjoseph-resilient

Howdy! I'm from Lambda, and this is surprisingly not a CDK issue in either case.

In the context of your first issue: Open up a support ticket with the arn of function, and time it occurred. Hopefully we can assist in making that experience better!

In relation to your last analysis: Have you tried this without a static iam role name? This is due to eventual consistency. Generally this is seen on recently "recreated" roles. By removing the static name, you don't get worse eventual consistency issues.

Specifically, if you have a random name chosen by cloudformation, you can be eventually consistent in 2 ways:

  1. Non-existence of role
  2. Existence of role

If you choose a static name, you can get 3 eventually consistent states (2 are failure states):

  1. Existence of old role
  2. Non-existence
  3. Existence of new role

At the very least, we can do a few retries on your behalf for the VPC errors to help in this case, so please open up a support ticket.

iph avatar May 17 '20 03:05 iph

Hi iph,

Thank you for the helpful response,

I have raised support tickets as advised.

I have not tried without a static Id, however I added lambdaFn.node.addDependency(policy) to ensure my Lambda would be created after the Policy had been created. So far this appears to have resolved the eventual consistency related issue I was seeing.

Kind Regards,

RJ

rjoseph-resilient avatar May 19 '20 14:05 rjoseph-resilient

Closing for now since the issue since to be resolved/is not actionable by the CDK team.

SomayaB avatar Sep 29 '20 23:09 SomayaB