Skip to content

fix(lambda): imported Function still has region and account from its Stack, instead of its ARN - #18255

Merged
mergify[bot] merged 2 commits into
aws:masterfrom
skinny85:lambda-import-region-from-arn
Jan 4, 2022
Merged

mergify[bot] merged 2 commits into
aws:masterfrom
skinny85:lambda-import-region-from-arn

Conversation

@skinny85

@skinny85 skinny85 commented Jan 3, 2022

Copy link
Copy Markdown
Contributor

Fixes #18228


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io

gitpod-io Bot commented Jan 3, 2022

Copy link
Copy Markdown

@github-actions github-actions Bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jan 3, 2022
@mergify mergify Bot added the contribution/core This is a PR that came from AWS. label Jan 3, 2022
@skinny85
skinny85 force-pushed the lambda-import-region-from-arn branch 2 times, most recently from b862ef3 to 3773c09 Compare January 4, 2022 01:47
@skinny85
skinny85 force-pushed the lambda-import-region-from-arn branch from 3773c09 to 15a0718 Compare January 4, 2022 03:24

@kaizencc kaizencc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks @skinny85

@mergify

mergify Bot commented Jan 4, 2022

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation

Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 7c4b136
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
mergify Bot merged commit 01bbe4c into aws:master Jan 4, 2022
@mergify

mergify Bot commented Jan 4, 2022

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@skinny85
skinny85 deleted the lambda-import-region-from-arn branch January 4, 2022 20:54
@jhoscar1

jhoscar1 commented Jan 6, 2022

Copy link
Copy Markdown

@skinny85 I'm seeing that this was released in the 2.x version of cdk. Is it possible to get this released in 1.x or is there a plan to?

@skinny85

skinny85 commented Jan 6, 2022

Copy link
Copy Markdown
Contributor Author

@jhoscar1 looks like this just barely missed the release window for v1.138.0. There will be a new release (v1.139.0) next week, and then this will be included in that one for sure.

@jhoscar1

jhoscar1 commented Jan 6, 2022

Copy link
Copy Markdown

Great, thanks!

@glc-gplassard

glc-gplassard commented Jan 12, 2022 •

Copy link
Copy Markdown

Hello, I think this breaks some of my usages when used in conjunction with cdk.ScopedAws & the aws-events package (at least in unit tests)

const lambdaFunction = lambda.Function.fromFunctionArn(this, 'Lambda', `arn:aws:lambda:${new cdk.ScopedAws(this).region}:${new cdk.ScopedAws(this).accountId}:function:${functionName}`);

const pipeline = new codepipeline.Pipeline(this, 'Codepipeline', {
      artifactBucket: "",
      pipelineName: "pipeline"
    });

pipeline.onStateChange('Notify', {target: new eventsTarget.LambdaFunction(lambdaFunction)});
Cannot create a cross-account or cross-region rule for an imported resource (create a stack with the right environment for the imported resource)
      at Rule.obtainMirrorRuleScope (node_modules/@aws-cdk/aws-events/lib/rule.ts:326:11)
      at Rule.addTarget (node_modules/@aws-cdk/aws-events/lib/rule.ts:170:38)
      at Pipeline.onEvent (node_modules/@aws-cdk/aws-codepipeline/lib/pipeline.ts:91:10)
      at Pipeline.onStateChange (node_modules/@aws-cdk/aws-codepipeline/lib/pipeline.ts:101:23)

It used to work with cdk 1.138.0

I can try to create a clean reproducer if you want

EDIT : or maybe I'm using it wrong

@skinny85

Copy link
Copy Markdown
Contributor Author

@cbm-gplassard when you say this used to work, do you mean it used to synth / deploy in the previous CDK version, or do you mean it deployed, and the Function was actually invoked each time the pipeline's state changed? Because I have a feeling that wouldn't work anyway in the old version of the CDK.

@glc-gplassard

Copy link
Copy Markdown

Hello @skinny85 ,
the stack was deployed & the function was correctly invoked.

I created a sample reproducer stack : https://github.com/cbm-gplassard/cdk-lambda-events-reproducer (one branch with cdk 1.138.0 and another with 1.139.0)

@skinny85

Copy link
Copy Markdown
Contributor Author

@cbm-gplassard I wonder - are you experiencing the same issue as described in #18405?

If so, can we consolidate the conversation there?

@glc-gplassard

Copy link
Copy Markdown

Seems like the same issue, indeed! 👍

skinny85 added a commit to skinny85/aws-cdk that referenced this pull request Feb 21, 2022
After aws#18255 has been merged, the account and region of the Function's imported ARN is correctly recognized.
Unfortunately, this has surfaced a few cases where that causes problems,
like when using an imported Function as the target of a CodePipeline invoke Action
(the pipeline construct needs to verify that the target Function is in the same account and region as the pipeline).

Add a `Function.fromFunctionName()` method that allows you to import a Function in the same account and region.

Fixes aws#19031
mergify Bot pushed a commit that referenced this pull request Feb 21, 2022
After #18255 has been merged, the account and region of the Function's imported ARN is correctly recognized.
Unfortunately, this has surfaced a few cases where that causes problems,
like when using an imported Function as the target of a CodePipeline invoke Action
(the pipeline construct needs to verify that the target Function is in the same account and region as the pipeline).

Add a `Function.fromFunctionName()` method that allows you to import a Function in the same account and region.

Fixes #19031

----

*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 pull request Feb 21, 2022
…Stack, instead of its ARN (aws#18255)

Fixes aws#18228

----

*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 pull request Feb 21, 2022
After aws#18255 has been merged, the account and region of the Function's imported ARN is correctly recognized.
Unfortunately, this has surfaced a few cases where that causes problems,
like when using an imported Function as the target of a CodePipeline invoke Action
(the pipeline construct needs to verify that the target Function is in the same account and region as the pipeline).

Add a `Function.fromFunctionName()` method that allows you to import a Function in the same account and region.

Fixes aws#19031

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
mergify Bot pushed a commit that referenced this pull request Aug 10, 2022
Fixes #21464. KMS keys imported using `fromKeyArn()` currently take the environment of the stack, not the environment from the arn.

This PR follows the precedent set in #19026 and #18255. It is essentially the same code change and tests. Ideally, we would have a mechanism for testing all `fromXxxArn` APIs to ensure they have the correct behavior. There are still many places where it does not. However, given the significant overhead of creating such a mechanism, I'm creating this one-off PR to unblock users in KMS.

----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
ali-n4i pushed a commit to ali-n4i/aws-cdk that referenced this pull request Jul 29, 2026
…Stack, instead of its ARN (aws#18255)

Fixes aws#18228

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
ali-n4i pushed a commit to ali-n4i/aws-cdk that referenced this pull request Jul 29, 2026
After aws#18255 has been merged, the account and region of the Function's imported ARN is correctly recognized.
Unfortunately, this has surfaced a few cases where that causes problems,
like when using an imported Function as the target of a CodePipeline invoke Action
(the pipeline construct needs to verify that the target Function is in the same account and region as the pipeline).

Add a `Function.fromFunctionName()` method that allows you to import a Function in the same account and region.

Fixes aws#19031

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
ali-n4i pushed a commit to ali-n4i/aws-cdk that referenced this pull request Jul 29, 2026
Fixes aws#21464. KMS keys imported using `fromKeyArn()` currently take the environment of the stack, not the environment from the arn.

This PR follows the precedent set in aws#19026 and aws#18255. It is essentially the same code change and tests. Ideally, we would have a mechanism for testing all `fromXxxArn` APIs to ensure they have the correct behavior. There are still many places where it does not. However, given the significant overhead of creating such a mechanism, I'm creating this one-off PR to unblock users in KMS.

----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*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 contribution/core This is a PR that came from AWS.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(aws-codepipelines-actions): Cannot set region on LambdaInvokeAction

5 participants