Skip to content

Conversation

@rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Jan 7, 2026

All CfnXxxx classes have a helper to return the ARN for a given resource, given an IXxxRef. For example:

declare const bucket: IBucketRef;
console.log(CfnBucket.arnForBucket(bucket));

This will use the already-existing ARN if available (as a CloudFormation attribute), but may also construct a fresh ARN from the ARN components if they are not available.

In the second case, this would always use the Stack's account and region, which might be incorrect in case of a resource referenced by ARN. The following can happen:

const stack = new Stack(..., { account: '11111' });
const resource = Resource.fromResourceArn(stack, ..., '...:222222:...');

console.log(CfnResource.arnForResource(resource)); // Should return 22222 not 11111

This PR fixes that.


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

All `CfnXxxx` classes have a helper to return the ARN for a given
resource, given an `IXxxRef`. For example:

```ts
declare const bucket: IBucketRef;
console.log(CfnBucket.arnForBucket(bucket));
```

This will use the already-existing ARN if available (as a CloudFormation
attribute), but may also construct a fresh ARN from the ARN components
if they are not available.

In the second case, this would always use the Stack's account and
region, which might be incorrect in case of a resource referenced
by ARN. The following can happen:

```ts
const stack = new Stack(..., { account: '11111' });
const resource = Resource.fromResourceArn(stack, ..., '...:222222:...');

console.log(CfnResource.arnForResource(resource)); // Should return 22222 not 11111
```

This PR fixes that.
@rix0rrr rix0rrr requested a review from a team January 7, 2026 11:37
@aws-cdk-automation aws-cdk-automation requested a review from a team January 7, 2026 11:37
@github-actions github-actions bot added the p2 label Jan 7, 2026
@rix0rrr rix0rrr changed the title fix(core): arnForXxxx() helpers always use Stack environment fix(core): arnForXxxx() helpers ignore environments from referenced resources Jan 7, 2026
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 7, 2026
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

(This review is outdated)

Region: stackOfResource.prop('region'),
Account: stackOfResource.prop('account'),
...mapValues(this.decider.resourceReference.arnVariables!, (propName) => $E(resourceIdentifier)[refAttributeName][propName]),
Partition: CDK_CORE.Aws.PARTITION,
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Stack has some fancy logic around the partition. Is there any particular reason we need to bypass this logic now?

public get partition(): string {
// Return a non-scoped partition intrinsic when the stack's region is
// unresolved or unknown. Otherwise we will return the partition name as
// a literal string.
if (!FeatureFlags.of(this).isEnabled(cxapi.ENABLE_PARTITION_LITERALS) || Token.isUnresolved(this.region)) {
return Aws.PARTITION;
} else {
const partition = RegionInfo.get(this.region).partition;
return partition ?? Aws.PARTITION;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we could use the same logic with resourceIdentifier.env.region instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's an idea. I wasn't aware Stack had this prettification logic.

I guess we could rely on the Stack to fill in the partition, and it should be 99% equivalent or better.

Only additional requirement then is that the (potentially referenced) resource is not scoped underneath a Stack. Stack.of() would throw an exception in that case.

I'm not sure if there's an absolute need for it to be in a Stack, and I'd like to avoid constraining unnecessarily. Let's see what I can do.

Copy link
Contributor Author

@rix0rrr rix0rrr Jan 7, 2026

Choose a reason for hiding this comment

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

Aha -- you know what; I was about to write why the same logic isn't necessary for region -- and the reason is because we already require that referenced resources are imported in the context of a Stack. We can loosen that restriction in the future, maybe, but for now relying on Stacks is not making the situation worse. So we can do the partition thing as well.


As for why it's not necessary for region: the resource's region will be default-filled from the containing Stack's region already, so whatever logic applies to Stack applies to region as well.

@rix0rrr rix0rrr added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Jan 7, 2026
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 7, 2026 12:46

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@mergify
Copy link
Contributor

mergify bot commented Jan 7, 2026

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

@mergify
Copy link
Contributor

mergify bot commented Jan 7, 2026

Merge Queue Status

✅ The pull request has been merged at 67ffcef

This pull request spent 39 minutes 46 seconds in the queue, including 39 minutes 36 seconds running CI.
The checks were run in-place.

Required conditions to merge

@mergify
Copy link
Contributor

mergify bot commented Jan 7, 2026

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

@mergify mergify bot merged commit 4744c59 into main Jan 7, 2026
17 of 18 checks passed
@mergify mergify bot deleted the huijbers/arn-for-xxx branch January 7, 2026 14:25
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants