Class NestedStack
A CloudFormation nested stack.
Inherited Members
Namespace: Amazon.CDK
Assembly: Amazon.CDK.dll
Syntax (csharp)
public class NestedStack : Stack, IConstruct, IDependable, ITaggable
Syntax (vb)
Public Class NestedStack
Inherits Stack
Implements IConstruct, IDependable, ITaggable
Remarks
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.
ExampleMetadata: lit=test/integ.restapi-import.lit.ts infused
Examples
using Amazon.CDK;
using Constructs;
using Amazon.CDK.AWS.APIGateway;
/**
* 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
*/
class RootStack : Stack
{
public RootStack(Construct scope) : base(scope, "integ-restapi-import-RootStack")
{
var restApi = new RestApi(this, "RestApi", new RestApiProps {
Deploy = false
});
restApi.Root.AddMethod("ANY");
var petsStack = new PetsStack(this, new ResourceNestedStackProps {
RestApiId = restApi.RestApiId,
RootResourceId = restApi.RestApiRootResourceId
});
var booksStack = new BooksStack(this, new ResourceNestedStackProps {
RestApiId = restApi.RestApiId,
RootResourceId = restApi.RestApiRootResourceId
});
new DeployStack(this, new DeployStackProps {
RestApiId = restApi.RestApiId,
Methods = petsStack.Methods.Concat(booksStack.Methods)
});
new CfnOutput(this, "PetsURL", new CfnOutputProps {
Value = $"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/pets"
});
new CfnOutput(this, "BooksURL", new CfnOutputProps {
Value = $"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/books"
});
}
}
class ResourceNestedStackProps : NestedStackProps
{
public string RestApiId { get; set; }
public string RootResourceId { get; set; }
}
class PetsStack : NestedStack
{
public readonly Method[] Methods = new [] { };
public PetsStack(Construct scope, ResourceNestedStackProps props) : base(scope, "integ-restapi-import-PetsStack", props)
{
var api = RestApi.FromRestApiAttributes(this, "RestApi", new RestApiAttributes {
RestApiId = props.RestApiId,
RootResourceId = props.RootResourceId
});
var method = api.Root.AddResource("pets").AddMethod("GET", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse {
StatusCode = "200"
} },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } }
});
Methods.Push(method);
}
}
class BooksStack : NestedStack
{
public readonly Method[] Methods = new [] { };
public BooksStack(Construct scope, ResourceNestedStackProps props) : base(scope, "integ-restapi-import-BooksStack", props)
{
var api = RestApi.FromRestApiAttributes(this, "RestApi", new RestApiAttributes {
RestApiId = props.RestApiId,
RootResourceId = props.RootResourceId
});
var method = api.Root.AddResource("books").AddMethod("GET", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse {
StatusCode = "200"
} },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } }
});
Methods.Push(method);
}
}
class DeployStackProps : NestedStackProps
{
public string RestApiId { get; set; }
public Method[]? Methods { get; set; }
}
class DeployStack : NestedStack
{
public DeployStack(Construct scope, DeployStackProps props) : base(scope, "integ-restapi-import-DeployStack", props)
{
var deployment = new Deployment(this, "Deployment", new DeploymentProps {
Api = RestApi.FromRestApiId(this, "RestApi", props.RestApiId)
});
if (props.Methods)
{
for (var method in props.Methods)
{
deployment.Node.AddDependency(method);
}
}
new Stage(this, "Stage", new StageProps { Deployment = deployment });
}
}
new RootStack(new App());
Synopsis
Constructors
NestedStack(ByRefValue) | Used by jsii to construct an instance of this class from a Javascript-owned object reference |
NestedStack(DeputyBase.DeputyProps) | Used by jsii to construct an instance of this class from DeputyProps |
NestedStack(Construct, String, INestedStackProps) |
Properties
NestedStackResource | If this is a nested stack, this represents its |
StackId | An attribute that represents the ID of the stack. |
StackName | An attribute that represents the name of the nested stack. |
TemplateFile | The name of the CloudFormation template file emitted to the output directory during synthesis. |
Methods
IsNestedStack(Object) | Checks if |
SetParameter(String, String) | Assign a value to one of the nested stack parameters. |
Constructors
NestedStack(ByRefValue)
Used by jsii to construct an instance of this class from a Javascript-owned object reference
protected NestedStack(ByRefValue reference)
Parameters
- reference Amazon.JSII.Runtime.Deputy.ByRefValue
The Javascript-owned object reference
NestedStack(DeputyBase.DeputyProps)
Used by jsii to construct an instance of this class from DeputyProps
protected NestedStack(DeputyBase.DeputyProps props)
Parameters
- props Amazon.JSII.Runtime.Deputy.DeputyBase.DeputyProps
The deputy props
NestedStack(Construct, String, INestedStackProps)
public NestedStack(Construct scope, string id, INestedStackProps props = null)
Parameters
- scope Constructs.Construct
- id System.String
- props INestedStackProps
Properties
NestedStackResource
If this is a nested stack, this represents its AWS::CloudFormation::Stack
resource.
public override CfnResource NestedStackResource { get; }
Property Value
Overrides
Remarks
undefined
for top-level (non-nested) stacks.
StackId
An attribute that represents the ID of the stack.
public override string StackId { get; }
Property Value
System.String
Overrides
Remarks
This is a context aware attribute:
Example value: arn:aws:cloudformation:us-east-2:123456789012:stack/mystack-mynestedstack-sggfrhxhum7w/f449b250-b969-11e0-a185-5081d0136786
Attribute: true
StackName
An attribute that represents the name of the nested stack.
public override string StackName { get; }
Property Value
System.String
Overrides
Remarks
This is a context aware attribute:
Example value: mystack-mynestedstack-sggfrhxhum7w
Attribute: true
TemplateFile
The name of the CloudFormation template file emitted to the output directory during synthesis.
public override string TemplateFile { get; }
Property Value
System.String
Overrides
Remarks
Example value: MyStack.template.json
Methods
IsNestedStack(Object)
Checks if x
is an object of type NestedStack
.
public static bool IsNestedStack(object x)
Parameters
- x System.Object
Returns
System.Boolean
SetParameter(String, String)
Assign a value to one of the nested stack parameters.
public virtual void SetParameter(string name, string value)
Parameters
- name System.String
The parameter name (ID).
- value System.String
The value to assign.