Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aws-lambda] lambda.Function.fromFunctionArn is not returning the role, role is undefined #10342

Closed
I2olanD opened this issue Sep 14, 2020 · 2 comments
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda guidance Question that needs advice or information.

Comments

@I2olanD
Copy link

I2olanD commented Sep 14, 2020

lambda.Function.fromFunctionArn is not returning the role

Reproduction Steps

const lambdaProperties = {...};

const myFn = lambda.Function.fromFunctionArn(....);

lambdaProperties.onFailure = new destinations.LambdaDestination(myFn);

new lambda.Function(scope, lambdaName, lambdaProperties);

What did you expect to happen?

fromFunctionArn() returns all information needed to add a destination.

What actually happened?

It throws following error: No child with id:....

Environment

  • CLI Version: latest
  • Framework Version: latest
  • Node.js Version: 10.19.0
  • OS :
  • Language (Version): [TypeScript (3.7.2)]

Other

It seems like, that the role information is getting lost after calling fromFunctionArn().

static fromFunctionArn(scope, id, functionArn) {
  return Function.fromFunctionAttributes(scope, id, { functionArn });
}

static fromFunctionAttributes(scope, id, attrs) {
  const functionArn = attrs.functionArn;
  const functionName = extractNameFromArn(attrs.functionArn);
  const role = attrs.role;
  console.log(role); // undefined since attrs only contains functionArn
  ......
}

return Function.fromFunctionAttributes(scope, id, { functionArn });


This is 🐛 Bug Report

@I2olanD I2olanD added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 14, 2020
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Sep 14, 2020
@nija-at
Copy link
Contributor

nija-at commented Sep 17, 2020

Hello @I2olanD -

Currently, the CDK cannot determine the IAM role you are using based on just the function ARN.

You will need to use fromFunctionAttributes() for your use case, and explicitly specify the IAM role. Most likely the IAM role is also defined outside of the CDK app, so it will also have to be imported. Your code will look something like -

const myFn = lambda.Function.fromFunctionArn(this, 'function', {
  functionArn: 'function-arn',
  role: iam.Role.fromRoleArn(this, 'role', 'role-arn'),
});

Let me know if I missed something here.

@nija-at nija-at added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Sep 17, 2020
@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Sep 18, 2020
@github-actions github-actions bot added closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Sep 22, 2020
@NGL321 NGL321 added guidance Question that needs advice or information. and removed bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. labels Dec 23, 2021
@alonsnir
Copy link

@nija-at

const myFn = lambda.Function.fromFunctionArn(this, 'function', {
  functionArn: 'function-arn',
  role: iam.Role.fromRoleArn(this, 'role', 'role-arn'),
});

How is this possible given the api?

/**
   * Import a lambda function into the CDK using its ARN
   */
  public static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction {
    return Function.fromFunctionAttributes(scope, id, { functionArn });
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

5 participants