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

(lambda): fromFunctionArn no longer working #18967

Closed
alamathe1 opened this issue Feb 14, 2022 · 4 comments
Closed

(lambda): fromFunctionArn no longer working #18967

alamathe1 opened this issue Feb 14, 2022 · 4 comments
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@alamathe1
Copy link

What is the problem?

We are facing an error with CDK version 1.143.0 with the following usage:

const isCompleteHandler = Function.fromFunctionArn(
      this,
      'IsCompleteHandler',
      options.isCompleteHandlerArn,
    );
    const onEventHandler = Function.fromFunctionArn(
      this,
      'OnEventHandler',
      options.onEventHandlerArn,
    );

The above code fails when sameEnviroment flag is not explicitly set to true. Workaround used today:

const isCompleteHandler = Function.fromFunctionAttributes(this, 'IsCompleteHandler', { functionArn: options.isCompleteHandlerArn, sameEnvironment: true });

const onEventHandler = Function.fromFunctionAttributes(this, 'OnEventHandler', { functionArn: options.onEventHandlerArn, sameEnvironment: true});

NOTE: The issue is not seen on previously used CDK version 1.111.0.

Reproduction Steps

This is used by an internal amazon package. Following is an error snippet with the CDK package is complied:

What did you expect to happen?

Expected the CDK package to compile successfully without any errors.

What actually happened?

Build failed with the error:

/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/aws-lambda/lib/function-base.js:145
                            throw new Error('Cannot modify permission to lambda function. Function is either imported or $LATEST version. '
                            ^

Error: Cannot modify permission to lambda function. Function is either imported or $LATEST version. If the function is imported from the same account use `fromFunctionAttributes()` API with the `sameEnvironment` flag.
    at Object.addToResourcePolicy (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/aws-lambda/lib/function-base.js:145:35)
    at Function.addToPrincipalOrResource (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/aws-iam/lib/grant.js:72:49)
    at Import.grantInvoke (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/aws-lambda/lib/function-base.js:130:31)
    at Provider.createFunction (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/custom-resources/lib/provider-framework/provider.js:87:29)
    at new Provider (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/monocdk/lib/custom-resources/lib/provider-framework/provider.js:41:38)
    at GetParametersStack.createResource (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/@amzn/ringleader-constructs/dist/stacks/GetParameters.js:30:26)
    at new GetParametersStack (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/@amzn/ringleader-constructs/dist/stacks/GetParameters.js:25:20)
    at Object.generateReleaseStacks [as default] (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/dist/lib/generateReleaseStacks.js:35:28)
    at stackGeneratorWithTests (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/dist/lib/createInfrastructure.js:19:211)
    at addEnvStackToDeploymentGroupStage (/Volumes/workplace/Pipe/src/IvyEdgeReleaseCDK/node_modules/@amzn/herbie-console-common-cdk/dist/createPipeline/createDeploymentGroup.js:18:41)
Subprocess exited with error 1
    Error: Failed to run cdk synth:
    null
                                                            
                        BUILD FAILED                        
                                                            
                                                                         
  *** command 'cdk-build' with arguments '' exited with return code '1'  
                                                                         
3c22fb02d71a:IvyEdgeReleaseCDK lamathe$

CDK CLI Version

1.143.0

Framework Version

No response

Node.js Version

12.22.9

OS

macos

Language

Typescript

Language Version

4.1.3

Other information

No response

@alamathe1 alamathe1 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 14, 2022
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Feb 14, 2022
@kaizencc
Copy link
Contributor

Related to #18781, please see #18781 (comment) for my take on what is going on.

@alamathe1, a couple of questions:

  • the above code doesn't fail as is right? It should fail when you try to do something with the lambda functions.
  • do the functions come from the same region/account or different region/account (environment)

@kaizencc kaizencc added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 14, 2022
@kaizencc kaizencc changed the title (FunctionAttributes.sameenvironment): build error on CDK version 1.143.0 (lambda): fromFunctionArn no longer working Feb 14, 2022
@alamathe1
Copy link
Author

@kaizen3031593

  • The code fails when we try to do something with the lambda functions.
  • The functions are from different accounts.

@kaizencc
Copy link
Contributor

Cool! Going to close this as a duplicate of #18781, since it is the same use case. There's some good information on what's happening in that thread.

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

mergify bot pushed a commit that referenced this issue Feb 16, 2022
…ured permissions (#18979)

There have been a few reports of regressions after #18228 was merged in v1.139.0: #18781, #18967.

AFAIK, #18228 fixed a bug in our environment configuring logic for `fromFunctionAttributes()`, so the environment is now being properly passed to the `functionBase` that CDK creates. The bug was being relied on by CDK customers for a particular use case: calling `grantInvoke()` on imported cross-account lambdas with pre-configured permissions. In general, `grantInvoke()` modifies the trust policy of the resource if it is not from the same environment. In the case of cross-account lambdas, this is impossible, and results in a synth-time error. CDK users know this and have modified the lambda permissions in question to have the correct permissions. These users do not need permissions added to the cross-account lambda, so it is not an error.

To support this use case, I'm introducing `skipPermissions` as a property for `fromFunctionAttributes()`. When set, we skip this synth-time check all together. Beware, users who set this property are now on their own for the permissions of their cross-account lambda.

```ts
const fn = lambda.Function.fromFunctionAttributes(stack, 'Function', {
  functionArn: 'arn:aws:lambda:us-east-1:123456789012:function:MyFn',
  skipPermissions: true,
});
```

Closes #18781. 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
…ured permissions (aws#18979)

There have been a few reports of regressions after aws#18228 was merged in v1.139.0: aws#18781, aws#18967.

AFAIK, aws#18228 fixed a bug in our environment configuring logic for `fromFunctionAttributes()`, so the environment is now being properly passed to the `functionBase` that CDK creates. The bug was being relied on by CDK customers for a particular use case: calling `grantInvoke()` on imported cross-account lambdas with pre-configured permissions. In general, `grantInvoke()` modifies the trust policy of the resource if it is not from the same environment. In the case of cross-account lambdas, this is impossible, and results in a synth-time error. CDK users know this and have modified the lambda permissions in question to have the correct permissions. These users do not need permissions added to the cross-account lambda, so it is not an error.

To support this use case, I'm introducing `skipPermissions` as a property for `fromFunctionAttributes()`. When set, we skip this synth-time check all together. Beware, users who set this property are now on their own for the permissions of their cross-account lambda.

```ts
const fn = lambda.Function.fromFunctionAttributes(stack, 'Function', {
  functionArn: 'arn:aws:lambda:us-east-1:123456789012:function:MyFn',
  skipPermissions: true,
});
```

Closes aws#18781. 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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 bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

No branches or pull requests

2 participants