Class NestedStack
- All Implemented Interfaces:
IConstruct
,IDependable
,ITaggable
,software.amazon.jsii.JsiiSerializable
,software.constructs.IConstruct
- Direct Known Subclasses:
KubectlProvider
,NestedStack
When you apply template changes to update a top-level stack, CloudFormation updates the top-level stack and initiates an update to its nested stacks. CloudFormation updates the resources of modified nested stacks, but does not update the resources of unmodified nested stacks.
Furthermore, this stack will not be treated as an independent deployment artifact (won't be listed in "cdk list" or deployable through "cdk deploy"), but rather only synthesized as a template and uploaded as an asset to S3.
Cross references of resource attributes between the parent stack and the nested stack will automatically be translated to stack parameters and outputs.
Example:
import software.amazon.awscdk.core.App; import software.amazon.awscdk.core.CfnOutput; import software.amazon.awscdk.core.NestedStack; import software.amazon.awscdk.core.NestedStackProps; import software.amazon.awscdk.core.Stack; import software.constructs.Construct; import software.amazon.awscdk.services.apigateway.Deployment; import software.amazon.awscdk.services.apigateway.Method; import software.amazon.awscdk.services.apigateway.MockIntegration; import software.amazon.awscdk.services.apigateway.PassthroughBehavior; import software.amazon.awscdk.services.apigateway.RestApi; import software.amazon.awscdk.services.apigateway.Stage; /** * This file showcases how to split up a RestApi's Resources and Methods across nested stacks. * * The root stack 'RootStack' first defines a RestApi. * Two nested stacks BooksStack and PetsStack, create corresponding Resources '/books' and '/pets'. * They are then deployed to a 'prod' Stage via a third nested stack - DeployStack. * * To verify this worked, go to the APIGateway */ public class RootStack extends Stack { public RootStack(Construct scope) { super(scope, "integ-restapi-import-RootStack"); RestApi restApi = RestApi.Builder.create(this, "RestApi") .deploy(false) .build(); restApi.root.addMethod("ANY"); PetsStack petsStack = new PetsStack(this, new ResourceNestedStackProps() .restApiId(restApi.getRestApiId()) .rootResourceId(restApi.getRestApiRootResourceId()) ); BooksStack booksStack = new BooksStack(this, new ResourceNestedStackProps() .restApiId(restApi.getRestApiId()) .rootResourceId(restApi.getRestApiRootResourceId()) ); new DeployStack(this, new DeployStackProps() .restApiId(restApi.getRestApiId()) .methods(petsStack.methods.concat(booksStack.getMethods())) ); CfnOutput.Builder.create(this, "PetsURL") .value(String.format("https://%s.execute-api.%s.amazonaws.com/prod/pets", restApi.getRestApiId(), this.region)) .build(); CfnOutput.Builder.create(this, "BooksURL") .value(String.format("https://%s.execute-api.%s.amazonaws.com/prod/books", restApi.getRestApiId(), this.region)) .build(); } } public class ResourceNestedStackProps extends NestedStackProps { private String restApiId; public String getRestApiId() { return this.restApiId; } public ResourceNestedStackProps restApiId(String restApiId) { this.restApiId = restApiId; return this; } private String rootResourceId; public String getRootResourceId() { return this.rootResourceId; } public ResourceNestedStackProps rootResourceId(String rootResourceId) { this.rootResourceId = rootResourceId; return this; } } public class PetsStack extends NestedStack { public final Method[] methods; public PetsStack(Construct scope, ResourceNestedStackProps props) { super(scope, "integ-restapi-import-PetsStack", props); IRestApi api = RestApi.fromRestApiAttributes(this, "RestApi", RestApiAttributes.builder() .restApiId(props.getRestApiId()) .rootResourceId(props.getRootResourceId()) .build()); Method method = api.root.addResource("pets").addMethod("GET", MockIntegration.Builder.create() .integrationResponses(List.of(IntegrationResponse.builder() .statusCode("200") .build())) .passthroughBehavior(PassthroughBehavior.NEVER) .requestTemplates(Map.of( "application/json", "{ \"statusCode\": 200 }")) .build(), MethodOptions.builder() .methodResponses(List.of(MethodResponse.builder().statusCode("200").build())) .build()); this.methods.push(method); } } public class BooksStack extends NestedStack { public final Method[] methods; public BooksStack(Construct scope, ResourceNestedStackProps props) { super(scope, "integ-restapi-import-BooksStack", props); IRestApi api = RestApi.fromRestApiAttributes(this, "RestApi", RestApiAttributes.builder() .restApiId(props.getRestApiId()) .rootResourceId(props.getRootResourceId()) .build()); Method method = api.root.addResource("books").addMethod("GET", MockIntegration.Builder.create() .integrationResponses(List.of(IntegrationResponse.builder() .statusCode("200") .build())) .passthroughBehavior(PassthroughBehavior.NEVER) .requestTemplates(Map.of( "application/json", "{ \"statusCode\": 200 }")) .build(), MethodOptions.builder() .methodResponses(List.of(MethodResponse.builder().statusCode("200").build())) .build()); this.methods.push(method); } } public class DeployStackProps extends NestedStackProps { private String restApiId; public String getRestApiId() { return this.restApiId; } public DeployStackProps restApiId(String restApiId) { this.restApiId = restApiId; return this; } private Method[] methods; public Method[] getMethods() { return this.methods; } public DeployStackProps methods(Method[] methods) { this.methods = methods; return this; } } public class DeployStack extends NestedStack { public DeployStack(Construct scope, DeployStackProps props) { super(scope, "integ-restapi-import-DeployStack", props); Deployment deployment = Deployment.Builder.create(this, "Deployment") .api(RestApi.fromRestApiId(this, "RestApi", props.getRestApiId())) .build(); if (props.getMethods()) { for (Object method : props.getMethods()) { deployment.node.addDependency(method); } } Stage.Builder.create(this, "Stage").deployment(deployment).build(); } } new RootStack(new App());
-
Nested Class Summary
Nested classes/interfaces inherited from class software.amazon.jsii.JsiiObject
software.amazon.jsii.JsiiObject.InitializationMode
Nested classes/interfaces inherited from interface software.amazon.awscdk.core.IConstruct
IConstruct.Jsii$Default, IConstruct.Jsii$Proxy
Nested classes/interfaces inherited from interface software.constructs.IConstruct
software.constructs.IConstruct.Jsii$Default, software.constructs.IConstruct.Jsii$Proxy
Nested classes/interfaces inherited from interface software.amazon.awscdk.core.ITaggable
ITaggable.Jsii$Default, ITaggable.Jsii$Proxy
-
Constructor Summary
ModifierConstructorDescriptionprotected
NestedStack
(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) protected
NestedStack
(software.amazon.jsii.JsiiObjectRef objRef) NestedStack
(software.constructs.Construct scope, String id) NestedStack
(software.constructs.Construct scope, String id, NestedStackProps props) -
Method Summary
Modifier and TypeMethodDescriptionIf this is a nested stack, this represents itsAWS::CloudFormation::Stack
resource.An attribute that represents the ID of the stack.An attribute that represents the name of the nested stack.The name of the CloudFormation template file emitted to the output directory during synthesis.static Boolean
Checks ifx
is an object of typeNestedStack
.void
setParameter
(String name, String value) Assign a value to one of the nested stack parameters.Methods inherited from class software.amazon.awscdk.core.Stack
addDependency, addDependency, addDockerImageAsset, addFileAsset, addTransform, allocateLogicalId, exportValue, exportValue, formatArn, getAccount, getArtifactId, getAvailabilityZones, getBundlingRequired, getDependencies, getEnvironment, getLogicalId, getNested, getNestedStackParent, getNotificationArns, getParentStack, getPartition, getRegion, getSynthesizer, getTags, getTemplateOptions, getTerminationProtection, getUrlSuffix, isStack, of, parseArn, parseArn, parseArn, prepareCrossReference, regionalFact, regionalFact, renameLogicalId, reportMissingContext, reportMissingContextKey, resolve, splitArn, toJsonString, toJsonString
Methods inherited from class software.amazon.awscdk.core.Construct
getNode, isConstruct, onPrepare, onSynthesize, onValidate, prepare, synthesize, validate
Methods inherited from class software.constructs.Construct
toString
Methods inherited from class software.amazon.jsii.JsiiObject
jsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSet
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface software.amazon.jsii.JsiiSerializable
$jsii$toJson
-
Constructor Details
-
NestedStack
protected NestedStack(software.amazon.jsii.JsiiObjectRef objRef) -
NestedStack
protected NestedStack(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) -
NestedStack
@Stability(Stable) public NestedStack(@NotNull software.constructs.Construct scope, @NotNull String id, @Nullable NestedStackProps props) - Parameters:
scope
- This parameter is required.id
- This parameter is required.props
-
-
NestedStack
@Stability(Stable) public NestedStack(@NotNull software.constructs.Construct scope, @NotNull String id) - Parameters:
scope
- This parameter is required.id
- This parameter is required.
-
-
Method Details
-
isNestedStack
Checks ifx
is an object of typeNestedStack
.- Parameters:
x
- This parameter is required.
-
setParameter
Assign a value to one of the nested stack parameters.- Parameters:
name
- The parameter name (ID). This parameter is required.value
- The value to assign. This parameter is required.
-
getStackId
An attribute that represents the ID of the stack.This is a context aware attribute:
- If this is referenced from the parent stack, it will return
{ "Ref": "LogicalIdOfNestedStackResource" }
. - If this is referenced from the context of the nested stack, it will return
{ "Ref": "AWS::StackId" }
Example value:
arn:aws:cloudformation:us-east-2:123456789012:stack/mystack-mynestedstack-sggfrhxhum7w/f449b250-b969-11e0-a185-5081d0136786
- Overrides:
getStackId
in classStack
- If this is referenced from the parent stack, it will return
-
getStackName
An attribute that represents the name of the nested stack.This is a context aware attribute:
- If this is referenced from the parent stack, it will return a token that parses the name from the stack ID.
- If this is referenced from the context of the nested stack, it will return
{ "Ref": "AWS::StackName" }
Example value:
mystack-mynestedstack-sggfrhxhum7w
- Overrides:
getStackName
in classStack
-
getTemplateFile
The name of the CloudFormation template file emitted to the output directory during synthesis.Example value:
MyStack.template.json
- Overrides:
getTemplateFile
in classStack
-
getNestedStackResource
If this is a nested stack, this represents itsAWS::CloudFormation::Stack
resource.undefined
for top-level (non-nested) stacks.- Overrides:
getNestedStackResource
in classStack
-