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): ExportValue does not automatically detect attribute name #12918

Closed
McDoit opened this issue Feb 8, 2021 · 3 comments · Fixed by #13052
Closed

(core): ExportValue does not automatically detect attribute name #12918

McDoit opened this issue Feb 8, 2021 · 3 comments · Fixed by #13052
Assignees
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort needs-triage This issue or PR still needs to be triaged. p1

Comments

@McDoit
Copy link
Contributor

McDoit commented Feb 8, 2021

Not possible to add ExportValue without specified name, even if the specific error says it should work this way

Reproduction Steps

Have two stacks, one with a backup bucket that i used the name from in another stack.
Removed the usage and added the ExportValue to avoid getting the cross stack error

this.ExportValue(destinationStack.BackupBucket.BucketName);

Reproduceable steps using the original BucketStack directly:

public class BucketStack : Stack
    {
        public readonly Alias BackupKeyAlias;
        //public readonly string BackupBucketArn;
        public readonly Bucket BackupBucket;
        public BucketStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var keyAlias = StackName + "-backup-key";
            var backupKey = new Key(this, "backup-key", new KeyProps
            {
                TrustAccountIdentities = true,
                Description = "Key that is used for encrypting backups",
                RemovalPolicy = RemovalPolicy.DESTROY
            });

            var backupKeyAlias = backupKey.AddAlias(keyAlias);

            BackupKeyAlias = backupKeyAlias;

            var backupBucket = new Bucket(this, this.StackName + "-backups", new BucketProps
            {
                Encryption = BucketEncryption.KMS,
                EncryptionKey = backupKeyAlias,
                BlockPublicAccess = BlockPublicAccess.BLOCK_ALL,
                BucketName = StackName.ToLower() + "-backups",
                Versioned = true,
                BucketKeyEnabled = true,
                AutoDeleteObjects = true,
                RemovalPolicy = RemovalPolicy.DESTROY
            });

            BackupBucket = backupBucket;

            this.ExportValue(backupBucket.BucketName);
        }
    }

What did you expect to happen?

To create a ExportValue without having to add a name

What actually happened?

Unhandled exception. Amazon.JSII.Runtime.JsiiException: exportValue: either supply 'name' or make sure to export a resource attribute (like 'bucket.bucketName')

Environment

  • CDK CLI Version : 1.88
  • Framework Version: 1.88
  • Node.js Version: v14.13.1
  • OS : Windows 10
  • Language (Version): C# (3.1)

Other

Workaround is the add a name to the ExportValueOptions


This is 🐛 Bug Report

@McDoit McDoit added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 8, 2021
@nija-at nija-at changed the title (module name): short issue description (core): short issue description Feb 8, 2021
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Feb 8, 2021
@rix0rrr
Copy link
Contributor

rix0rrr commented Feb 8, 2021

Hi!

Without having seen more code (such as the definition of BackupBucket), which would definitely help...

... can I guess that BackupBucket is a new CfnBucket(), as opposed to a new Bucket() ? (Notice the absence of Cfn there).

If not, please supply a minimal working example.

@rix0rrr rix0rrr changed the title (core): short issue description (core): ExportValue does not automatically detect attribute name Feb 8, 2021
@rix0rrr rix0rrr added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 8, 2021
@McDoit
Copy link
Contributor Author

McDoit commented Feb 8, 2021

Sadly no, here is an actual example I used, in a similar way:

public class BucketStack : Stack
    {
        public readonly Alias BackupKeyAlias;
        //public readonly string BackupBucketArn;
        public readonly Bucket BackupBucket;
        public BucketStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var keyAlias = StackName + "-backup-key";
            var backupKey = new Key(this, "backup-key", new KeyProps
            {
                TrustAccountIdentities = true,
                Description = "Key that is used for encrypting backups",
                RemovalPolicy = RemovalPolicy.DESTROY
            });

            var backupKeyAlias = backupKey.AddAlias(keyAlias);

            BackupKeyAlias = backupKeyAlias;

            var backupBucket = new Bucket(this, this.StackName + "-backups", new BucketProps
            {
                Encryption = BucketEncryption.KMS,
                EncryptionKey = backupKeyAlias,
                BlockPublicAccess = BlockPublicAccess.BLOCK_ALL,
                BucketName = StackName.ToLower() + "-backups",
                Versioned = true,
                BucketKeyEnabled = true,
                AutoDeleteObjects = true,
                RemovalPolicy = RemovalPolicy.DESTROY
            });

            BackupBucket = backupBucket;

            this.ExportValue(backupBucket.BucketName);
        }
    }

Difference here is that I use the ExportValue directly on the stack, in my original problem i used it on another stack that referenced this BucketStack

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 9, 2021
@rix0rrr rix0rrr added effort/medium Medium work item – several days of effort p1 labels Feb 15, 2021
rix0rrr added a commit that referenced this issue Feb 15, 2021
Most L2 resources employ the "PhysicalName" protocol, which checks usage of
resource names across environment borders, and can automatically turn
auto-named resources into physically-named resources if the situation calls for
it.

Unfortunately, this wrapped token is a generic IResolvable, not a Reference,
and so did not work with the `exportValue()` automatic reference detection.

Make the token returned by `getResourceNameAttribute()` etc. a `Reference`
that mimics the underlying `Reference` to make this work out.

Fixes #13002, fixes #12918.
@mergify mergify bot closed this as completed in #13052 Feb 16, 2021
mergify bot pushed a commit that referenced this issue Feb 16, 2021
Most L2 resources employ the "PhysicalName" protocol, which checks usage of
resource names across environment borders, and can automatically turn
auto-named resources into physically-named resources if the situation calls for
it.

Unfortunately, this wrapped token is a generic IResolvable, not a Reference,
and so did not work with the `exportValue()` automatic reference detection.

Make the token returned by `getResourceNameAttribute()` etc. a `Reference`
that mimics the underlying `Reference` to make this work out.

Fixes #13002, fixes #12918.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@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.

NovakGu pushed a commit to NovakGu/aws-cdk that referenced this issue Feb 18, 2021
Most L2 resources employ the "PhysicalName" protocol, which checks usage of
resource names across environment borders, and can automatically turn
auto-named resources into physically-named resources if the situation calls for
it.

Unfortunately, this wrapped token is a generic IResolvable, not a Reference,
and so did not work with the `exportValue()` automatic reference detection.

Make the token returned by `getResourceNameAttribute()` etc. a `Reference`
that mimics the underlying `Reference` to make this work out.

Fixes aws#13002, fixes aws#12918.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
eladb pushed a commit that referenced this issue Feb 22, 2021
Most L2 resources employ the "PhysicalName" protocol, which checks usage of
resource names across environment borders, and can automatically turn
auto-named resources into physically-named resources if the situation calls for
it.

Unfortunately, this wrapped token is a generic IResolvable, not a Reference,
and so did not work with the `exportValue()` automatic reference detection.

Make the token returned by `getResourceNameAttribute()` etc. a `Reference`
that mimics the underlying `Reference` to make this work out.

Fixes #13002, fixes #12918.


----

*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/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort needs-triage This issue or PR still needs to be triaged. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants