Node

class constructs.Node(host, scope, id)

Bases: object

Represents the construct node in the scope tree.

Parameters:

Methods

add_dependency(*deps)

Add an ordering dependency on another construct.

An IDependable

Parameters:

deps (IDependable) –

Return type:

None

add_metadata(type, data, *, stack_trace=None, trace_from_function=None)

Adds a metadata entry to this construct.

Entries are arbitrary values and will also include a stack trace to allow tracing back to the code location for when the entry was added. It can be used, for example, to include source mapping in CloudFormation templates to improve diagnostics.

Parameters:
  • type (str) – a string denoting the type of metadata.

  • data (Any) – the value of the metadata (can be a Token). If null/undefined, metadata will not be added.

  • stack_trace (Optional[bool]) – Include stack trace with metadata entry. Default: false

  • trace_from_function (Optional[Any]) – A JavaScript function to begin tracing from. This option is ignored unless stackTrace is true. Default: addMetadata()

Return type:

None

add_validation(validation)

Adds a validation to this construct.

When node.validate() is called, the validate() method will be called on all validations and all errors will be returned.

Parameters:

validation (IValidation) – The validation object.

Return type:

None

find_all(order=None)

Return this construct and all of its children in the given order.

Parameters:

order (Optional[ConstructOrder]) –

Return type:

List[IConstruct]

find_child(id)

Return a direct child by id.

Throws an error if the child is not found.

Parameters:

id (str) – Identifier of direct child.

Return type:

IConstruct

Returns:

Child with the given id.

get_all_context(defaults=None)

Retrieves the all context of a node from tree context.

Context is usually initialized at the root, but can be overridden at any point in the tree.

Parameters:

defaults (Optional[Mapping[Any, Any]]) – Any keys to override the retrieved context.

Return type:

Any

Returns:

The context object or an empty object if there is discovered context

get_context(key)

Retrieves a value from tree context if present. Otherwise, would throw an error.

Context is usually initialized at the root, but can be overridden at any point in the tree.

Parameters:

key (str) – The context key.

Return type:

Any

Returns:

The context value or throws error if there is no context value for this key

lock()

Locks this construct from allowing more children to be added.

After this call, no more children can be added to this construct or to any children.

Return type:

None

set_context(key, value)

This can be used to set contextual values.

Context must be set before any children are added, since children may consult context info during construction. If the key already exists, it will be overridden.

Parameters:
  • key (str) – The context key.

  • value (Any) – The context value.

Return type:

None

try_find_child(id)

Return a direct child by id, or undefined.

Parameters:

id (str) – Identifier of direct child.

Return type:

Optional[IConstruct]

Returns:

the child if found, or undefined

try_get_context(key)

Retrieves a value from tree context.

Context is usually initialized at the root, but can be overridden at any point in the tree.

Parameters:

key (str) – The context key.

Return type:

Any

Returns:

The context value or undefined if there is no context value for this key.

try_remove_child(child_name)

(experimental) Remove the child with the given name, if present.

Parameters:

child_name (str) –

Return type:

bool

Returns:

Whether a child with the given name was deleted.

Stability:

experimental

validate()

Validates this construct.

Invokes the validate() method on all validations added through addValidation().

Return type:

List[str]

Returns:

an array of validation error messages associated with this construct.

Attributes

PATH_SEP = '/'
addr

Returns an opaque tree-unique address for this construct.

Addresses are 42 characters hexadecimal strings. They begin with “c8” followed by 40 lowercase hexadecimal characters (0-9a-f).

Addresses are calculated using a SHA-1 of the components of the construct path.

To enable refactorings of construct trees, constructs with the ID Default will be excluded from the calculation. In those cases constructs in the same tree may have the same addreess.

Example:

c83a2846e506bcc5f10682b564084bca2d275709ee
children

All direct children of this construct.

default_child

Returns the child construct that has the id Default or Resource".

This is usually the construct that provides the bulk of the underlying functionality. Useful for modifications of the underlying construct that are not available at the higher levels. Override the defaultChild property.

This should only be used in the cases where the correct default child is not named ‘Resource’ or ‘Default’ as it should be.

If you set this to undefined, the default behavior of finding the child named ‘Resource’ or ‘Default’ will be used.

Returns:

a construct or undefined if there is no default child

Throws:

if there is more than one child

dependencies

Return all dependencies registered on this node (non-recursive).

id

The id of this construct within the current scope.

This is a scope-unique id. To obtain an app-unique id for this construct, use addr.

locked

Returns true if this construct or the scopes in which it is defined are locked.

metadata

An immutable array of metadata objects associated with this construct.

This can be used, for example, to implement support for deprecation notices, source mapping, etc.

path

The full, absolute path of this construct in the tree.

Components are separated by ‘/’.

root

Returns the root of the construct tree.

Returns:

The root of the construct tree.

scope

Returns the scope in which this construct is defined.

The value is undefined at the root of the construct scope tree.

scopes

All parent scopes of this construct.

Returns:

a list of parent scopes. The last element in the list will always be the current construct and the first element will be the root of the tree.

Static Methods

classmethod of(construct)

(deprecated) Returns the node associated with a construct.

Parameters:

construct (IConstruct) – the construct.

Deprecated:

use construct.node instead

Stability:

deprecated

Return type:

Node