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

core: Return concrete values for physical names and ARNs if explicitly stated #21487

Open
1 of 2 tasks
melnikalex opened this issue Aug 6, 2022 · 6 comments
Open
1 of 2 tasks
Labels
@aws-cdk/aws-cloudformation Related to AWS CloudFormation @aws-cdk/core Related to core CDK functionality effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p2

Comments

@melnikalex
Copy link

Describe the feature

Re-opening this issue: #4093

❗️There is a problem: CloudFormation references implicitly indicate that there is a dependency between the consuming resource and the referenced resource. If we return a concrete name (string), this dependency will now be lost forever. Is there anything we can do about it? (@rix0rrr?)

A solution to this could be to return physical names only if embedded in a cross-environment stack. Otherwise return the token.

Use Case

Trying to reference resources across environments. This should be possible if I name the resources at compile-time. However cdk always tokenizes it regardless of if a static name is provided or not.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

latest

Environment details (OS name and version, etc.)

mac

@melnikalex melnikalex added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 6, 2022
@github-actions github-actions bot added the @aws-cdk/aws-cloudformation Related to AWS CloudFormation label Aug 6, 2022
@rittneje
Copy link

Even within a single stack, I ought to be able to reference, say, the aws_iam.User.user_name in an if statement without needing an extra variable.

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 2, 2022

What are you trying to do?

@rix0rrr rix0rrr added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Sep 2, 2022
@github-actions
Copy link

github-actions bot commented Sep 4, 2022

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Sep 4, 2022
@rittneje
Copy link

rittneje commented Sep 4, 2022

@rix0rrr Two days is not "a while". Please fix your GitHub action configuration to be more reasonable.

With regards to your question, we have a shared account in which we have to generate resource names with a unique suffix. (We can't always use the auto-generated names that CDK would pick.) In these situations we sometimes need to later feed this chosen name into some conditional logic. Due to the current way CDK works, the only way to do this is to save the name in a separate variable, which is very annoying. It is part of a larger issue where CDK does not support CloudFormation conditions, only host language ones, which means anything not resolved at synth time is a problem.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Sep 4, 2022
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 9, 2022

I understand why this is frustrating. The problem is it cannot really be solved easily. We need the reference to imply a dependency, but also be readable at synth time. We want both of the following:

const bucket = new Bucket(..., { bucketName: 'deterministic' });

// At deploy time, BucketUser must be sequenced AFTER Bucket
const user = new BucketUser(..., { bucketName: bucket.bucketName });

// But right now, I also want to know the literal value
if (bucket.bucketName !== 'deterministic') {
  throw new Error('But I told you what it was!');
}

There is not a single value we can populate bucket.bucketName with that will satisfy both use cases:

  • If we pick 'deterministic', we will fail to imply the deploy-time dependency
    • If we try to invent a magic JavaScript object that behaves like a string but can be uniquely distinguished from other strings, that magic will not survive the jsii-JavaScript transition.
  • If we pick the tokenized value, we will fail the if.

Something that might work for you is to use one of:

// Probably works for resources that have properly implemented 'physicalName' support
function physicalNameOf(resource: Resource) {
  const resolvedName = Stack.of(resource).resolve(resource.physicalName);
  if (resolvedName === undefined) {
    throw new Error(`Should have given a physical name to ${source.node.path}`);
  }
  return resolvedName;
}

// Or dig into the L1 properties of each type
function physicalNameOfBucket(bucket: Bucket) {
  const resolvedName = Stack.of(bucket).resolve((resource.node.defaultChild as CfnBucket).bucketName);
  if (resolvedName === undefined) {
    throw new Error(`Bucket needs a physical name ${source.node.path}`);
  }
  return resolvedName;
}

function physicalNameOfUser(bucket: User) { ... }
// etc.

@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 9, 2022

I have been toying with the idea of making bucket.bucketName be a tokenized value that both resolves to a literal value, as well as implies a dependency in all resources where it is used, giving us (mostly) the best of both worlds. You could then get the value you wanted via:

const probablyResolved = stack.resolve(bucket.bucketName); // Either 'deterministic' or { Ref: 'MyBucket' }
if (typeof probablyResolved === 'string') {
  // ... 
}

Which is also not fantastic, but at least slightly more construct-agnostic than the other mechanism.

Not likely to come any time soon though.

@rix0rrr rix0rrr changed the title (module name): Return concrete values for physical names and ARNs if explicitly stated core: Return concrete values for physical names and ARNs if explicitly stated Nov 9, 2022
@rix0rrr rix0rrr added effort/large Large work item – several weeks of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Nov 9, 2022
@rix0rrr rix0rrr removed their assignment Nov 9, 2022
@khushail khushail added the @aws-cdk/core Related to core CDK functionality label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudformation Related to AWS CloudFormation @aws-cdk/core Related to core CDK functionality effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

4 participants