Class Stack
A root construct which represents a single CloudFormation stack.
Implements
Namespace: Amazon.CDK
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public class Stack : Construct, ITaggable
Syntax (vb)
Public Class Stack
Inherits Construct
Implements ITaggable
Remarks
ExampleMetadata: infused
Examples
using Amazon.CDK;
using Amazon.CDK.AWS.S3;
IBucket bucket;
var app = new App();
var stack = new Stack(app, "Stack");
new Table(stack, "Table", new TableProps {
PartitionKey = new Attribute {
Name = "id",
Type = AttributeType.STRING
},
ImportSource = new ImportSourceSpecification {
CompressionType = InputCompressionType.GZIP,
InputFormat = InputFormat.Csv(new CsvOptions {
Delimiter = ",",
HeaderList = new [] { "id", "name" }
}),
Bucket = bucket,
KeyPrefix = "prefix"
}
});
Synopsis
Constructors
Stack(ByRefValue) | Used by jsii to construct an instance of this class from a Javascript-owned object reference |
Stack(DeputyBase.DeputyProps) | Used by jsii to construct an instance of this class from DeputyProps |
Stack(Construct, String, IStackProps) | Creates a new stack. |
Properties
Account | The AWS account into which this stack will be deployed. |
ArtifactId | The ID of the cloud assembly artifact for this stack. |
AvailabilityZones | Returns the list of AZs that are available in the AWS environment (account/region) associated with this stack. |
BundlingRequired | Indicates whether the stack requires bundling or not. |
Dependencies | Return the stacks this stack depends on. |
Environment | The environment coordinates in which this stack is deployed. |
Nested | Indicates if this is a nested stack, in which case |
NestedStackParent | If this is a nested stack, returns it's parent stack. |
NestedStackResource | If this is a nested stack, this represents its |
NotificationArns | Returns the list of notification Amazon Resource Names (ARNs) for the current stack. |
Partition | The partition in which this stack is defined. |
Region | The AWS region into which this stack will be deployed (e.g. |
StackId | The ID of the stack. |
StackName | The concrete CloudFormation physical stack name. |
Synthesizer | Synthesis method for this stack. |
Tags | Tags to be applied to the stack. |
TemplateFile | The name of the CloudFormation template file emitted to the output directory during synthesis. |
TemplateOptions | Options for CloudFormation template (like version, transform, description). |
TerminationProtection | Whether termination protection is enabled for this stack. |
UrlSuffix | The Amazon domain suffix for the region in which this stack is defined. |
Methods
AddDependency(Stack, String) | Add a dependency between this stack and another stack. |
AddMetadata(String, Object) | Adds an arbitary key-value pair, with information you want to record about the stack. |
AddTransform(String) | Add a Transform to this stack. A Transform is a macro that AWS CloudFormation uses to process your template. |
AllocateLogicalId(CfnElement) | Returns the naming scheme used to allocate logical IDs. |
ExportStringListValue(Object, IExportValueOptions) | Create a CloudFormation Export for a string list value. |
ExportValue(Object, IExportValueOptions) | Create a CloudFormation Export for a string value. |
FormatArn(IArnComponents) | Creates an ARN from components. |
GetLogicalId(CfnElement) | Allocates a stack-unique CloudFormation-compatible logical identity for a specific resource. |
IsStack(Object) | Return whether the given object is a Stack. |
Of(IConstruct) | Looks up the first stack scope in which |
RegionalFact(String, String) | Look up a fact value for the given fact for the region of this stack. |
RenameLogicalId(String, String) | Rename a generated logical identities. |
ReportMissingContextKey(IMissingContext) | Indicate that a context key was expected. |
Resolve(Object) | Resolve a tokenized value in the context of the current stack. |
SplitArn(String, ArnFormat) | Splits the provided ARN into its components. |
ToJsonString(Object, Nullable<Double>) | Convert an object, potentially containing tokens, to a JSON string. |
ToYamlString(Object) | Convert an object, potentially containing tokens, to a YAML string. |
Constructors
Stack(ByRefValue)
Used by jsii to construct an instance of this class from a Javascript-owned object reference
protected Stack(ByRefValue reference)
Parameters
- reference Amazon.JSII.Runtime.Deputy.ByRefValue
The Javascript-owned object reference
Stack(DeputyBase.DeputyProps)
Used by jsii to construct an instance of this class from DeputyProps
protected Stack(DeputyBase.DeputyProps props)
Parameters
- props Amazon.JSII.Runtime.Deputy.DeputyBase.DeputyProps
The deputy props
Stack(Construct, String, IStackProps)
Creates a new stack.
public Stack(Construct scope = null, string id = null, IStackProps props = null)
Parameters
- scope Constructs.Construct
Parent of this stack, usually an
App
or aStage
, but could be any construct.- id System.String
The construct ID of this stack.
- props IStackProps
Stack properties.
Properties
Account
The AWS account into which this stack will be deployed.
public virtual string Account { get; }
Property Value
System.String
Remarks
This value is resolved according to the following rules:
Preferably, you should use the return value as an opaque string and not
attempt to parse it to implement your logic. If you do, you must first
check that it is a concrete value an not an unresolved token. If this
value is an unresolved token (Token.isUnresolved(stack.account)
returns
true
), this implies that the user wishes that this stack will synthesize
into a account-agnostic template. In this case, your code should either
fail (throw an error, emit a synth error using Annotations.of(construct).addError()
) or
implement some other region-agnostic behavior.
ArtifactId
The ID of the cloud assembly artifact for this stack.
public virtual string ArtifactId { get; }
Property Value
System.String
AvailabilityZones
Returns the list of AZs that are available in the AWS environment (account/region) associated with this stack.
public virtual string[] AvailabilityZones { get; }
Property Value
System.String[]
Remarks
If the stack is environment-agnostic (either account and/or region are
tokens), this property will return an array with 2 tokens that will resolve
at deploy-time to the first two availability zones returned from CloudFormation's
Fn::GetAZs
intrinsic function.
If they are not available in the context, returns a set of dummy values and
reports them as missing, and let the CLI resolve them by calling EC2
DescribeAvailabilityZones
on the target environment.
To specify a different strategy for selecting availability zones override this method.
BundlingRequired
Indicates whether the stack requires bundling or not.
public virtual bool BundlingRequired { get; }
Property Value
System.Boolean
Dependencies
Return the stacks this stack depends on.
public virtual Stack[] Dependencies { get; }
Property Value
Stack[]
Environment
The environment coordinates in which this stack is deployed.
public virtual string Environment { get; }
Property Value
System.String
Remarks
In the form
aws://account/region
. Use stack.account
and stack.region
to obtain
the specific values, no need to parse.
You can use this value to determine if two stacks are targeting the same environment.
If either stack.account
or stack.region
are not concrete values (e.g.
Aws.ACCOUNT_ID
or Aws.REGION
) the special strings unknown-account
and/or
unknown-region
will be used respectively to indicate this stack is
region/account-agnostic.
Nested
Indicates if this is a nested stack, in which case parentStack
will include a reference to it's parent.
public virtual bool Nested { get; }
Property Value
System.Boolean
NestedStackParent
If this is a nested stack, returns it's parent stack.
public virtual Stack NestedStackParent { get; }
Property Value
NestedStackResource
If this is a nested stack, this represents its AWS::CloudFormation::Stack
resource.
public virtual CfnResource NestedStackResource { get; }
Property Value
Remarks
undefined
for top-level (non-nested) stacks.
NotificationArns
Returns the list of notification Amazon Resource Names (ARNs) for the current stack.
public virtual string[] NotificationArns { get; }
Property Value
System.String[]
Partition
The partition in which this stack is defined.
public virtual string Partition { get; }
Property Value
System.String
Region
The AWS region into which this stack will be deployed (e.g. us-west-2
).
public virtual string Region { get; }
Property Value
System.String
Remarks
This value is resolved according to the following rules:
Preferably, you should use the return value as an opaque string and not
attempt to parse it to implement your logic. If you do, you must first
check that it is a concrete value an not an unresolved token. If this
value is an unresolved token (Token.isUnresolved(stack.region)
returns
true
), this implies that the user wishes that this stack will synthesize
into a region-agnostic template. In this case, your code should either
fail (throw an error, emit a synth error using Annotations.of(construct).addError()
) or
implement some other region-agnostic behavior.
StackId
The ID of the stack.
public virtual string StackId { get; }
Property Value
System.String
Examples
// After resolving, looks like
"arn:aws:cloudformation:us-west-2:123456789012:stack/teststack/51af3dc0-da77-11e4-872e-1234567db123";
StackName
The concrete CloudFormation physical stack name.
public virtual string StackName { get; }
Property Value
System.String
Remarks
This is either the name defined explicitly in the stackName
prop or
allocated based on the stack's location in the construct tree. Stacks that
are directly defined under the app use their construct id
as their stack
name. Stacks that are defined deeper within the tree will use a hashed naming
scheme based on the construct path to ensure uniqueness.
If you wish to obtain the deploy-time AWS::StackName intrinsic,
you can use Aws.STACK_NAME
directly.
Synthesizer
Synthesis method for this stack.
public virtual IStackSynthesizer Synthesizer { get; }
Property Value
Tags
TemplateFile
The name of the CloudFormation template file emitted to the output directory during synthesis.
public virtual string TemplateFile { get; }
Property Value
System.String
Remarks
Example value: MyStack.template.json
TemplateOptions
Options for CloudFormation template (like version, transform, description).
public virtual ITemplateOptions TemplateOptions { get; }
Property Value
TerminationProtection
Whether termination protection is enabled for this stack.
public virtual bool TerminationProtection { get; set; }
Property Value
System.Boolean
UrlSuffix
The Amazon domain suffix for the region in which this stack is defined.
public virtual string UrlSuffix { get; }
Property Value
System.String
Methods
AddDependency(Stack, String)
Add a dependency between this stack and another stack.
public virtual void AddDependency(Stack target, string reason = null)
Parameters
- target Stack
- reason System.String
Remarks
This can be used to define dependencies between any two stacks within an app, and also supports nested stacks.
AddMetadata(String, Object)
Adds an arbitary key-value pair, with information you want to record about the stack.
public virtual void AddMetadata(string key, object value)
Parameters
- key System.String
- value System.Object
Remarks
These get translated to the Metadata section of the generated template.
See: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html
AddTransform(String)
Add a Transform to this stack. A Transform is a macro that AWS CloudFormation uses to process your template.
public virtual void AddTransform(string transform)
Parameters
- transform System.String
The transform to add.
Remarks
Duplicate values are removed when stack is synthesized.
See: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Examples
Stack stack;
stack.AddTransform("AWS::Serverless-2016-10-31");
AllocateLogicalId(CfnElement)
Returns the naming scheme used to allocate logical IDs.
protected virtual string AllocateLogicalId(CfnElement cfnElement)
Parameters
- cfnElement CfnElement
The element for which the logical ID is allocated.
Returns
System.String
Remarks
By default, uses
the HashedAddressingScheme
but this method can be overridden to customize
this behavior.
In order to make sure logical IDs are unique and stable, we hash the resource construct tree path (i.e. toplevel/secondlevel/.../myresource) and add it as a suffix to the path components joined without a separator (CloudFormation IDs only allow alphanumeric characters).
The result will be:
<path.join('')><md5(path.join('/')> "human" "hash"
If the "human" part of the ID exceeds 240 characters, we simply trim it so the total ID doesn't exceed CloudFormation's 255 character limit.
We only take 8 characters from the md5 hash (0.000005 chance of collision).
Special cases:
ExportStringListValue(Object, IExportValueOptions)
Create a CloudFormation Export for a string list value.
public virtual string[] ExportStringListValue(object exportedValue, IExportValueOptions options = null)
Parameters
- exportedValue System.Object
- options IExportValueOptions
Returns
System.String[]
Remarks
Returns a string list representing the corresponding Fn.importValue()
expression for this Export. The export expression is automatically wrapped with an
Fn::Join
and the import value with an Fn::Split
, since CloudFormation can only
export strings. You can control the name for the export by passing the name
option.
If you don't supply a value for name
, the value you're exporting must be
a Resource attribute (for example: bucket.bucketName
) and it will be
given the same name as the automatic cross-stack reference that would be created
if you used the attribute in another Stack.
One of the uses for this method is to remove the relationship between two Stacks established by automatic cross-stack references. It will temporarily ensure that the CloudFormation Export still exists while you remove the reference from the consuming stack. After that, you can remove the resource and the manual export.
See exportValue
for an example of this process.
ExportValue(Object, IExportValueOptions)
Create a CloudFormation Export for a string value.
public virtual string ExportValue(object exportedValue, IExportValueOptions options = null)
Parameters
- exportedValue System.Object
- options IExportValueOptions
Returns
System.String
Remarks
Returns a string representing the corresponding Fn.importValue()
expression for this Export. You can control the name for the export by
passing the name
option.
If you don't supply a value for name
, the value you're exporting must be
a Resource attribute (for example: bucket.bucketName
) and it will be
given the same name as the automatic cross-stack reference that would be created
if you used the attribute in another Stack.
One of the uses for this method is to remove the relationship between two Stacks established by automatic cross-stack references. It will temporarily ensure that the CloudFormation Export still exists while you remove the reference from the consuming stack. After that, you can remove the resource and the manual export.
Here is how the process works. Let's say there are two stacks,
producerStack
and consumerStack
, and producerStack
has a bucket
called bucket
, which is referenced by consumerStack
(perhaps because
an AWS Lambda Function writes into it, or something like that).
It is not safe to remove producerStack.bucket
because as the bucket is being
deleted, consumerStack
might still be using it.
Instead, the process takes two deployments:
Deployment 1: break the relationship:
Deployment 2: remove the bucket resource:
FormatArn(IArnComponents)
Creates an ARN from components.
public virtual string FormatArn(IArnComponents components)
Parameters
- components IArnComponents
Returns
System.String
Remarks
If partition
, region
or account
are not specified, the stack's
partition, region and account will be used.
If any component is the empty string, an empty string will be inserted into the generated ARN at the location that component corresponds to.
The ARN will be formatted as follows:
arn:{partition}:{service}:{region}:{account}:{resource}{sep}{resource-name}
The required ARN pieces that are omitted will be taken from the stack that the 'scope' is attached to. If all ARN pieces are supplied, the supplied scope can be 'undefined'.
GetLogicalId(CfnElement)
Allocates a stack-unique CloudFormation-compatible logical identity for a specific resource.
public virtual string GetLogicalId(CfnElement element)
Parameters
- element CfnElement
The CloudFormation element for which a logical identity is needed.
Returns
System.String
Remarks
This method is called when a CfnElement
is created and used to render the
initial logical identity of resources. Logical ID renames are applied at
this stage.
This method uses the protected method allocateLogicalId
to render the
logical ID for an element. To modify the naming scheme, extend the Stack
class and override this method.
IsStack(Object)
Return whether the given object is a Stack.
public static bool IsStack(object x)
Parameters
- x System.Object
Returns
System.Boolean
Remarks
We do attribute detection since we can't reliably use 'instanceof'.
Of(IConstruct)
Looks up the first stack scope in which construct
is defined.
public static Stack Of(IConstruct construct)
Parameters
- construct Constructs.IConstruct
The construct to start the search from.
Returns
Remarks
Fails if there is no stack up the tree.
RegionalFact(String, String)
Look up a fact value for the given fact for the region of this stack.
public virtual string RegionalFact(string factName, string defaultValue = null)
Parameters
- factName System.String
- defaultValue System.String
Returns
System.String
Remarks
Will return a definite value only if the region of the current stack is resolved. If not, a lookup map will be added to the stack and the lookup will be done at CDK deployment time.
What regions will be included in the lookup map is controlled by the
@aws-cdk/core:target-partitions
context value: it must be set to a list
of partitions, and only regions from the given partitions will be included.
If no such context key is set, all regions will be included.
This function is intended to be used by construct library authors. Application builders can rely on the abstractions offered by construct libraries and do not have to worry about regional facts.
If defaultValue
is not given, it is an error if the fact is unknown for
the given region.
RenameLogicalId(String, String)
Rename a generated logical identities.
public virtual void RenameLogicalId(string oldId, string newId)
Parameters
- oldId System.String
- newId System.String
Remarks
To modify the naming scheme strategy, extend the Stack
class and
override the allocateLogicalId
method.
ReportMissingContextKey(IMissingContext)
Indicate that a context key was expected.
public virtual void ReportMissingContextKey(IMissingContext report)
Parameters
- report IMissingContext
The set of parameters needed to obtain the context.
Remarks
Contains instructions which will be emitted into the cloud assembly on how the key should be supplied.
Resolve(Object)
Resolve a tokenized value in the context of the current stack.
public virtual object Resolve(object obj)
Parameters
- obj System.Object
Returns
System.Object
SplitArn(String, ArnFormat)
Splits the provided ARN into its components.
public virtual IArnComponents SplitArn(string arn, ArnFormat arnFormat)
Parameters
- arn System.String
the ARN to split into its components.
- arnFormat ArnFormat
the expected format of 'arn' - depends on what format the service 'arn' represents uses.
Returns
Remarks
Works both if 'arn' is a string like 'arn:aws:s3:::bucket', and a Token representing a dynamic CloudFormation expression (in which case the returned components will also be dynamic CloudFormation expressions, encoded as Tokens).
ToJsonString(Object, Nullable<Double>)
Convert an object, potentially containing tokens, to a JSON string.
public virtual string ToJsonString(object obj, Nullable<double> space = null)
Parameters
- obj System.Object
- space System.Nullable<System.Double>
Returns
System.String
ToYamlString(Object)
Convert an object, potentially containing tokens, to a YAML string.
public virtual string ToYamlString(object obj)
Parameters
- obj System.Object
Returns
System.String