How to show cfnoutput from nested stack in parent stack? #34829
Replies: 1 comment
|
CloudFormation does not automatically promote every child output into the root stack's There are two separate use cases: 1. The parent only needs to consume the valueExpose the resource attribute as a property of your interface LoadBalancerNestedStackProps extends cdk.NestedStackProps {
vpc: ec2.IVpc;
}
class LoadBalancerNestedStack extends cdk.NestedStack {
public readonly dnsName: string;
constructor(scope: Construct, id: string, props: LoadBalancerNestedStackProps) {
super(scope, id, props);
const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', {
vpc: props.vpc,
internetFacing: true,
});
this.dnsName = lb.loadBalancerDnsName;
}
}
class ParentStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'Vpc');
const networking = new LoadBalancerNestedStack(this, 'Networking', { vpc });
// Use networking.dnsName in another construct in the parent.
}
}The 2. You want the value displayed as a root-stack outputThen the root template must contain an output, so one explicit parent new cdk.CfnOutput(this, 'LoadBalancerDnsName', {
value: networking.dnsName,
description: 'Public DNS name of the application load balancer',
});This is not creating or copying the load balancer value at synthesis time; it creates the parent output whose value resolves through the nested-stack reference. Underlying CloudFormation represents that as approximately: Outputs:
LoadBalancerDnsName:
Value: !GetAtt NetworkingNestedStack.Outputs.<generated-output-name>CloudFormation documents that nested outputs are addressed as If you do not want any extra root output, the alternative is operational rather than declarative: list the child stacks and read their individual outputs with So the clean CDK pattern is: expose resource attributes from the nested-stack class, let CDK synthesize private cross-stack plumbing, and declare only the small set of parent If this clarifies why the output is not promoted automatically, please mark it as the accepted answer so future CDK users can find the pattern quickly. |
Uh oh!
There was an error while loading. Please reload this page.
Is there any way to show the CfnOutput from nested stack in parent stack output?
For eg. If I have LB as nested stack with CfnOutput the dns name, is there any way for the parent stack to print it automatically without having to explicitly again do a CfnOutput?
All reactions