aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.37k stars 3.78k forks source link

CDK NestedStack: Accessing a nested stack resource in a stack resolves to Amazon.JSII.Runtime.Deputy.AnonymousObject and cannot be casted to CfnStack #30497

Open hakenmt opened 1 month ago

hakenmt commented 1 month ago

Describe the bug

I have use case that I need to add parameters being provided to a nested stack that is created by an AWS provided EKS construct (in this case the nested stacks containing the Kubectl and resource provider lambda functions). I can find the resources in the node tree, but they cannot be casted to a CfnStack, they are of type Amazon.JSII.Runtime.Deputy.AnonymousObject and I cannot access the parameters property of the stack. This results in having to manipulate the rendered CloudFormation template to achieve the desired outcome.

Expected Behavior

Be able to access a nested stack resource and cast it to a CfnStack.

Current Behavior

The only available type is Amazon.JSII.Runtime.Deputy.AnonymousObject.

Reproduction Steps

FixUpResourceProvider("@aws-cdk--aws-eks.ClusterResourceProvider");
FixUpResourceProvider("@aws-cdk--aws-eks.KubectlProvider");

private void FixUpResourceProvider(string name)
{
            IConstruct resourceProviderNestedStack = this.Node.TryFindChild(name);

            // Add the parameters to the actual nested stack so it can receive them
            if (resourceProviderNestedStack != null)
            {
                NestedStack nestedStack = resourceProviderNestedStack as NestedStack;

                var nestedStackResource = nestedStack.NestedStackResource as CfnStack;

                nestedStackResource.Parameters.GetType() == typeof(Amazon.JSII.Runtime.Deputy.AnonymousObject)
            }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.138.0

Framework Version

No response

Node.js Version

v20.9.0

OS

darwin

Language

.NET

Language Version

No response

Other information

No response

pahud commented 3 weeks ago

Sounds related to JSII but I am wondering what you are trying to do with aws-eks construct?

hakenmt commented 3 weeks ago

The automatically created Lambda functions get created in nested stacks. For my use case, I can't use the automatically defined CDK bucket, the bucket name is provided as a parameter to the main stack at runtime. So I do this in my synthesizer:

Synthesizer = new DefaultStackSynthesizer(new DefaultStackSynthesizerProps() {
                    FileAssetsBucketName = "${AssetsBucket}",
                    BucketPrefix = "${AssetsBucketPrefix}",
                    Qualifier = null,
                    GenerateBootstrapVersionRule = false              
                })

Then, the asset s3 code path is set to use this variable when synth is run. In stacks/nested stacks I control, I can pass the received value of the parent stack parameter to those nested stacks (add CfnParameter objects to the nested stack and also specify parameters in the nested stack resource props). For the nested stacks created by the EKS construct I cannot. Trying to manipulate the nested stack resource through node tree isn't successful since I can't actually cast the nested stack resource to something that I can manipulate to add the parameters property.