- Implements: IConstruct
Represents the building block of the construct graph.
All constructs besides the root construct must be created within the scope of another construct.
import { Construct } from 'constructs'
new Construct(scope: Construct, id: string)
Name | Type | Description |
---|---|---|
scope |
Construct |
The scope in which to define this construct. |
id |
string |
The scoped construct ID. |
- Type: Construct
The scope in which to define this construct.
- Type: string
The scoped construct ID.
Must be unique amongst siblings. If
the ID includes a path separator (/
), then it will be replaced by double
dash --
.
Name | Description |
---|---|
toString |
Returns a string representation of this construct. |
public toString(): string
Returns a string representation of this construct.
Name | Description |
---|---|
isConstruct |
Checks if x is a construct. |
import { Construct } from 'constructs'
Construct.isConstruct(x: any)
Checks if x
is a construct.
Use this method instead of instanceof
to properly detect Construct
instances, even when the construct library is symlinked.
Explanation: in JavaScript, multiple copies of the constructs
library on
disk are seen as independent, completely different libraries. As a
consequence, the class Construct
in each copy of the constructs
library
is seen as a different class, and an instance of one class will not test as
instanceof
the other class. npm install
will not create installations
like this, but users may manually symlink construct libraries together or
use a monorepo tool: in those cases, multiple copies of the constructs
library can be accidentally installed, and instanceof
will behave
unpredictably. It is safest to avoid using instanceof
, and using
this type-testing method instead.
- Type: any
Any object.
Name | Type | Description |
---|---|---|
node |
Node |
The tree node. |
public readonly node: Node;
- Type: Node
The tree node.
Creates a new root construct node.
The root construct represents the top of the construct tree and is not contained within a parent scope itself. For root constructs, the id is optional.
import { RootConstruct } from 'constructs'
new RootConstruct(id?: string)
Name | Type | Description |
---|---|---|
id |
string |
The scoped construct ID. |
- Type: string
The scoped construct ID.
Must be unique amongst siblings. If
the ID includes a path separator (/
), then it will be replaced by double
dash --
.
Name | Description |
---|---|
toString |
Returns a string representation of this construct. |
public toString(): string
Returns a string representation of this construct.
Name | Description |
---|---|
isConstruct |
Checks if x is a construct. |
import { RootConstruct } from 'constructs'
RootConstruct.isConstruct(x: any)
Checks if x
is a construct.
Use this method instead of instanceof
to properly detect Construct
instances, even when the construct library is symlinked.
Explanation: in JavaScript, multiple copies of the constructs
library on
disk are seen as independent, completely different libraries. As a
consequence, the class Construct
in each copy of the constructs
library
is seen as a different class, and an instance of one class will not test as
instanceof
the other class. npm install
will not create installations
like this, but users may manually symlink construct libraries together or
use a monorepo tool: in those cases, multiple copies of the constructs
library can be accidentally installed, and instanceof
will behave
unpredictably. It is safest to avoid using instanceof
, and using
this type-testing method instead.
- Type: any
Any object.
Name | Type | Description |
---|---|---|
node |
Node |
The tree node. |
public readonly node: Node;
- Type: Node
The tree node.
An entry in the construct metadata table.
import { MetadataEntry } from 'constructs'
const metadataEntry: MetadataEntry = { ... }
Name | Type | Description |
---|---|---|
data |
any |
The data. |
type |
string |
The metadata entry type. |
trace |
string[] |
Stack trace at the point of adding the metadata. |
public readonly data: any;
- Type: any
The data.
public readonly type: string;
- Type: string
The metadata entry type.
public readonly trace: string[];
- Type: string[]
- Default: no trace information
Stack trace at the point of adding the metadata.
Only available if addMetadata()
is called with stackTrace: true
.
Options for construct.addMetadata()
.
import { MetadataOptions } from 'constructs'
const metadataOptions: MetadataOptions = { ... }
Name | Type | Description |
---|---|---|
stackTrace |
boolean |
Include stack trace with metadata entry. |
traceFromFunction |
any |
A JavaScript function to begin tracing from. |
public readonly stackTrace: boolean;
- Type: boolean
- Default: false
Include stack trace with metadata entry.
public readonly traceFromFunction: any;
- Type: any
- Default: addMetadata()
A JavaScript function to begin tracing from.
This option is ignored unless stackTrace
is true
.
Trait for IDependable.
Traits are interfaces that are privately implemented by objects. Instead of showing up in the public interface of a class, they need to be queried explicitly. This is used to implement certain framework features that are not intended to be used by Construct consumers, and so should be hidden from accidental use.
Example
// Usage
const roots = Dependable.of(construct).dependencyRoots;
// Definition
Dependable.implement(construct, {
dependencyRoots: [construct],
});
import { Dependable } from 'constructs'
new Dependable()
Name | Type | Description |
---|
Name | Description |
---|---|
get |
Return the matching Dependable for the given class instance. |
implement |
Turn any object into an IDependable. |
of |
Return the matching Dependable for the given class instance. |
import { Dependable } from 'constructs'
Dependable.get(instance: IDependable)
Return the matching Dependable for the given class instance.
- Type: IDependable
import { Dependable } from 'constructs'
Dependable.implement(instance: IDependable, trait: Dependable)
Turn any object into an IDependable.
- Type: IDependable
- Type: Dependable
import { Dependable } from 'constructs'
Dependable.of(instance: IDependable)
Return the matching Dependable for the given class instance.
- Type: IDependable
Name | Type | Description |
---|---|---|
dependencyRoots |
IConstruct[] |
The set of constructs that form the root of this dependable. |
public readonly dependencyRoots: IConstruct[];
- Type: IConstruct[]
The set of constructs that form the root of this dependable.
All resources under all returned constructs are included in the ordering dependency.
- Implements: IDependable
A set of constructs to be used as a dependable.
This class can be used when a set of constructs which are disjoint in the construct tree needs to be combined to be used as a single dependable.
import { DependencyGroup } from 'constructs'
new DependencyGroup(deps: ...IDependable[])
Name | Type | Description |
---|---|---|
deps |
...IDependable[] |
No description. |
- Type: ...IDependable[]
Name | Description |
---|---|
add |
Add a construct to the dependency roots. |
public add(scopes: ...IDependable[]): void
Add a construct to the dependency roots.
- Type: ...IDependable[]
Represents the construct node in the scope tree.
import { Node } from 'constructs'
new Node(host: Construct, scope: IConstruct, id: string)
Name | Type | Description |
---|---|---|
host |
Construct |
No description. |
scope |
IConstruct |
No description. |
id |
string |
No description. |
- Type: Construct
- Type: IConstruct
- Type: string
Name | Description |
---|---|
addDependency |
Add an ordering dependency on another construct. |
addMetadata |
Adds a metadata entry to this construct. |
addValidation |
Adds a validation to this construct. |
findAll |
Return this construct and all of its children in the given order. |
findChild |
Return a direct child by id. |
getAllContext |
Retrieves the all context of a node from tree context. |
getContext |
Retrieves a value from tree context if present. Otherwise, would throw an error. |
lock |
Locks this construct from allowing more children to be added. |
setContext |
This can be used to set contextual values. |
tryFindChild |
Return a direct child by id, or undefined. |
tryGetContext |
Retrieves a value from tree context. |
tryRemoveChild |
Remove the child with the given name, if present. |
validate |
Validates this construct. |
public addDependency(deps: ...IDependable[]): void
Add an ordering dependency on another construct.
An IDependable
- Type: ...IDependable[]
public addMetadata(type: string, data: any, options?: MetadataOptions): void
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.
Note that construct metadata is not the same as CloudFormation resource metadata and is never written to the CloudFormation template.
The metadata entries are written to the Cloud Assembly Manifest if the treeMetadata
property is specified in the props of the App that contains this Construct.
- Type: string
a string denoting the type of metadata.
- Type: any
the value of the metadata (can be a Token).
If null/undefined, metadata will not be added.
- Type: MetadataOptions
options.
public addValidation(validation: IValidation): void
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.
- Type: IValidation
The validation object.
public findAll(order?: ConstructOrder): IConstruct[]
Return this construct and all of its children in the given order.
- Type: ConstructOrder
public findChild(id: string): IConstruct
Return a direct child by id.
Throws an error if the child is not found.
- Type: string
Identifier of direct child.
public getAllContext(defaults?: object): any
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.
- Type: object
Any keys to override the retrieved context.
public getContext(key: string): any
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.
- Type: string
The context key.
public lock(): void
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.
public setContext(key: string, value: any): void
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.
- Type: string
The context key.
- Type: any
The context value.
public tryFindChild(id: string): IConstruct
Return a direct child by id, or undefined.
- Type: string
Identifier of direct child.
public tryGetContext(key: string): any
Retrieves a value from tree context.
Context is usually initialized at the root, but can be overridden at any point in the tree.
- Type: string
The context key.
public tryRemoveChild(childName: string): boolean
Remove the child with the given name, if present.
- Type: string
public validate(): string[]
Validates this construct.
Invokes the validate()
method on all validations added through
addValidation()
.
Name | Description |
---|---|
of |
Returns the node associated with a construct. |
import { Node } from 'constructs'
Node.of(construct: IConstruct)
Returns the node associated with a construct.
- Type: IConstruct
the construct.
Name | Type | Description |
---|---|---|
addr |
string |
Returns an opaque tree-unique address for this construct. |
children |
IConstruct[] |
All direct children of this construct. |
dependencies |
IConstruct[] |
Return all dependencies registered on this node (non-recursive). |
id |
string |
The id of this construct within the current scope. |
locked |
boolean |
Returns true if this construct or the scopes in which it is defined are locked. |
metadata |
MetadataEntry[] |
An immutable array of metadata objects associated with this construct. |
path |
string |
The full, absolute path of this construct in the tree. |
root |
IConstruct |
Returns the root of the construct tree. |
scopes |
IConstruct[] |
All parent scopes of this construct. |
scope |
IConstruct |
Returns the scope in which this construct is defined. |
defaultChild |
IConstruct |
Returns the child construct that has the id Default or Resource" . |
public readonly addr: string;
- Type: string
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 refactoring 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 address.
Example
c83a2846e506bcc5f10682b564084bca2d275709ee
public readonly children: IConstruct[];
- Type: IConstruct[]
All direct children of this construct.
public readonly dependencies: IConstruct[];
- Type: IConstruct[]
Return all dependencies registered on this node (non-recursive).
public readonly id: string;
- Type: string
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
.
public readonly locked: boolean;
- Type: boolean
Returns true if this construct or the scopes in which it is defined are locked.
public readonly metadata: MetadataEntry[];
- Type: MetadataEntry[]
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.
public readonly path: string;
- Type: string
The full, absolute path of this construct in the tree.
Components are separated by '/'.
public readonly root: IConstruct;
- Type: IConstruct
Returns the root of the construct tree.
public readonly scopes: IConstruct[];
- Type: IConstruct[]
All parent scopes of this construct.
public readonly scope: IConstruct;
- Type: IConstruct
Returns the scope in which this construct is defined.
The value is undefined
at the root of the construct scope tree.
public readonly defaultChild: IConstruct;
- Type: IConstruct
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.
Name | Type | Description |
---|---|---|
PATH_SEP |
string |
Separator used to delimit construct path components. |
public readonly PATH_SEP: string;
- Type: string
Separator used to delimit construct path components.
-
Extends: IDependable
-
Implemented By: Construct, RootConstruct, cdklabs-projen-project-types.yarn.CdkLabsMonorepo, cdklabs-projen-project-types.yarn.Monorepo, cdklabs-projen-project-types.yarn.MonorepoRelease, cdklabs-projen-project-types.yarn.TypeScriptWorkspace, cdklabs-projen-project-types.yarn.WorkspaceRelease, cdklabs-projen-project-types.CdkConstructLibrary, cdklabs-projen-project-types.CdkJsiiProject, cdklabs-projen-project-types.CdkTypeScriptProject, cdklabs-projen-project-types.CdklabsConstructLibrary, cdklabs-projen-project-types.CdklabsJsiiProject, cdklabs-projen-project-types.CdklabsTypeScriptProject, cdklabs-projen-project-types.Rosetta, projen.awscdk.AutoDiscover, projen.awscdk.AwsCdkConstructLibrary, projen.awscdk.AwsCdkDeps, projen.awscdk.AwsCdkDepsJava, projen.awscdk.AwsCdkDepsJs, projen.awscdk.AwsCdkDepsPy, projen.awscdk.AwsCdkJavaApp, projen.awscdk.AwsCdkPythonApp, projen.awscdk.AwsCdkTypeScriptApp, projen.awscdk.CdkConfig, projen.awscdk.CdkTasks, projen.awscdk.ConstructLibraryAws, projen.awscdk.EdgeLambdaAutoDiscover, projen.awscdk.IntegrationTest, projen.awscdk.IntegrationTestAutoDiscover, projen.awscdk.LambdaAutoDiscover, projen.awscdk.LambdaExtension, projen.awscdk.LambdaExtensionAutoDiscover, projen.awscdk.LambdaFunction, projen.build.BuildWorkflow, projen.cdk.AutoDiscoverBase, projen.cdk.ConstructLibrary, projen.cdk.IntegrationTestAutoDiscoverBase, projen.cdk.IntegrationTestBase, projen.cdk.JsiiDocgen, projen.cdk.JsiiProject, projen.cdk8s.AutoDiscover, projen.cdk8s.Cdk8sDeps, projen.cdk8s.Cdk8sDepsPy, projen.cdk8s.Cdk8sPythonApp, projen.cdk8s.Cdk8sTypeScriptApp, projen.cdk8s.ConstructLibraryCdk8s, projen.cdk8s.IntegrationTest, projen.cdk8s.IntegrationTestAutoDiscover, projen.cdktf.ConstructLibraryCdktf, projen.circleci.Circleci, projen.github.AutoApprove, projen.github.AutoMerge, projen.github.AutoQueue, projen.github.Dependabot, projen.github.GitHub, projen.github.GitHubProject, projen.github.GithubWorkflow, projen.github.MergeQueue, projen.github.Mergify, projen.github.PullRequestBackport, projen.github.PullRequestLint, projen.github.PullRequestTemplate, projen.github.Stale, projen.github.TaskWorkflow, projen.github.TaskWorkflowJob, projen.gitlab.CiConfiguration, projen.gitlab.GitlabConfiguration, projen.gitlab.NestedConfiguration, projen.java.JavaProject, projen.java.Junit, projen.java.MavenCompile, projen.java.MavenPackaging, projen.java.MavenSample, projen.java.Pom, projen.java.Projenrc, projen.javascript.Bundler, projen.javascript.Eslint, projen.javascript.Jest, projen.javascript.LicenseChecker, projen.javascript.NodePackage, projen.javascript.NodeProject, projen.javascript.NpmConfig, projen.javascript.Prettier, projen.javascript.Projenrc, projen.javascript.TypescriptConfig, projen.javascript.UpgradeDependencies, projen.javascript.Yarnrc, projen.python.Pip, projen.python.Poetry, projen.python.PoetryPyproject, projen.python.Projenrc, projen.python.Pytest, projen.python.PytestSample, projen.python.PythonProject, projen.python.PythonSample, projen.python.RequirementsFile, projen.python.SetupPy, projen.python.Setuptools, projen.python.Venv, projen.release.Publisher, projen.release.Release, projen.typescript.Projenrc, projen.typescript.ProjenrcTs, projen.typescript.TypeScriptAppProject, projen.typescript.TypeScriptLibraryProject, projen.typescript.TypeScriptProject, projen.vscode.DevContainer, projen.vscode.VsCode, projen.vscode.VsCodeLaunchConfig, projen.vscode.VsCodeRecommendedExtensions, projen.vscode.VsCodeSettings, projen.web.NextComponent, projen.web.NextJsProject, projen.web.NextJsTypeScriptProject, projen.web.ReactComponent, projen.web.ReactProject, projen.web.ReactTypeDef, projen.web.ReactTypeScriptProject, projen.Component, projen.Dependencies, projen.DockerCompose, projen.FileBase, projen.GitAttributesFile, projen.Gitpod, projen.IgnoreFile, projen.IniFile, projen.JsonFile, projen.License, projen.Logger, projen.Makefile, projen.ObjectFile, projen.Project, projen.ProjectBuild, projen.ProjectTree, projen.Projenrc, projen.ProjenrcFile, projen.ProjenrcJson, projen.Renovatebot, projen.SampleDir, projen.SampleFile, projen.SampleReadme, projen.SourceCode, projen.Tasks, projen.TextFile, projen.TomlFile, projen.Version, projen.XmlFile, projen.YamlFile, IConstruct
Represents a construct.
Name | Type | Description |
---|---|---|
node |
Node |
The tree node. |
public readonly node: Node;
- Type: Node
The tree node.
- Implemented By: Construct, DependencyGroup, RootConstruct, cdklabs-projen-project-types.yarn.CdkLabsMonorepo, cdklabs-projen-project-types.yarn.Monorepo, cdklabs-projen-project-types.yarn.MonorepoRelease, cdklabs-projen-project-types.yarn.TypeScriptWorkspace, cdklabs-projen-project-types.yarn.WorkspaceRelease, cdklabs-projen-project-types.CdkConstructLibrary, cdklabs-projen-project-types.CdkJsiiProject, cdklabs-projen-project-types.CdkTypeScriptProject, cdklabs-projen-project-types.CdklabsConstructLibrary, cdklabs-projen-project-types.CdklabsJsiiProject, cdklabs-projen-project-types.CdklabsTypeScriptProject, cdklabs-projen-project-types.Rosetta, projen.awscdk.AutoDiscover, projen.awscdk.AwsCdkConstructLibrary, projen.awscdk.AwsCdkDeps, projen.awscdk.AwsCdkDepsJava, projen.awscdk.AwsCdkDepsJs, projen.awscdk.AwsCdkDepsPy, projen.awscdk.AwsCdkJavaApp, projen.awscdk.AwsCdkPythonApp, projen.awscdk.AwsCdkTypeScriptApp, projen.awscdk.CdkConfig, projen.awscdk.CdkTasks, projen.awscdk.ConstructLibraryAws, projen.awscdk.EdgeLambdaAutoDiscover, projen.awscdk.IntegrationTest, projen.awscdk.IntegrationTestAutoDiscover, projen.awscdk.LambdaAutoDiscover, projen.awscdk.LambdaExtension, projen.awscdk.LambdaExtensionAutoDiscover, projen.awscdk.LambdaFunction, projen.build.BuildWorkflow, projen.cdk.AutoDiscoverBase, projen.cdk.ConstructLibrary, projen.cdk.IntegrationTestAutoDiscoverBase, projen.cdk.IntegrationTestBase, projen.cdk.JsiiDocgen, projen.cdk.JsiiProject, projen.cdk8s.AutoDiscover, projen.cdk8s.Cdk8sDeps, projen.cdk8s.Cdk8sDepsPy, projen.cdk8s.Cdk8sPythonApp, projen.cdk8s.Cdk8sTypeScriptApp, projen.cdk8s.ConstructLibraryCdk8s, projen.cdk8s.IntegrationTest, projen.cdk8s.IntegrationTestAutoDiscover, projen.cdktf.ConstructLibraryCdktf, projen.circleci.Circleci, projen.github.AutoApprove, projen.github.AutoMerge, projen.github.AutoQueue, projen.github.Dependabot, projen.github.GitHub, projen.github.GitHubProject, projen.github.GithubWorkflow, projen.github.MergeQueue, projen.github.Mergify, projen.github.PullRequestBackport, projen.github.PullRequestLint, projen.github.PullRequestTemplate, projen.github.Stale, projen.github.TaskWorkflow, projen.github.TaskWorkflowJob, projen.gitlab.CiConfiguration, projen.gitlab.GitlabConfiguration, projen.gitlab.NestedConfiguration, projen.java.JavaProject, projen.java.Junit, projen.java.MavenCompile, projen.java.MavenPackaging, projen.java.MavenSample, projen.java.Pom, projen.java.Projenrc, projen.javascript.Bundler, projen.javascript.Eslint, projen.javascript.Jest, projen.javascript.LicenseChecker, projen.javascript.NodePackage, projen.javascript.NodeProject, projen.javascript.NpmConfig, projen.javascript.Prettier, projen.javascript.Projenrc, projen.javascript.TypescriptConfig, projen.javascript.UpgradeDependencies, projen.javascript.Yarnrc, projen.python.Pip, projen.python.Poetry, projen.python.PoetryPyproject, projen.python.Projenrc, projen.python.Pytest, projen.python.PytestSample, projen.python.PythonProject, projen.python.PythonSample, projen.python.RequirementsFile, projen.python.SetupPy, projen.python.Setuptools, projen.python.Venv, projen.release.Publisher, projen.release.Release, projen.typescript.Projenrc, projen.typescript.ProjenrcTs, projen.typescript.TypeScriptAppProject, projen.typescript.TypeScriptLibraryProject, projen.typescript.TypeScriptProject, projen.vscode.DevContainer, projen.vscode.VsCode, projen.vscode.VsCodeLaunchConfig, projen.vscode.VsCodeRecommendedExtensions, projen.vscode.VsCodeSettings, projen.web.NextComponent, projen.web.NextJsProject, projen.web.NextJsTypeScriptProject, projen.web.ReactComponent, projen.web.ReactProject, projen.web.ReactTypeDef, projen.web.ReactTypeScriptProject, projen.Component, projen.Dependencies, projen.DockerCompose, projen.FileBase, projen.GitAttributesFile, projen.Gitpod, projen.IgnoreFile, projen.IniFile, projen.JsonFile, projen.License, projen.Logger, projen.Makefile, projen.ObjectFile, projen.Project, projen.ProjectBuild, projen.ProjectTree, projen.Projenrc, projen.ProjenrcFile, projen.ProjenrcJson, projen.Renovatebot, projen.SampleDir, projen.SampleFile, projen.SampleReadme, projen.SourceCode, projen.Tasks, projen.TextFile, projen.TomlFile, projen.Version, projen.XmlFile, projen.YamlFile, IConstruct, IDependable
Trait marker for classes that can be depended upon.
The presence of this interface indicates that an object has
an IDependable
implementation.
This interface can be used to take an (ordering) dependency on a set of constructs. An ordering dependency implies that the resources represented by those constructs are deployed before the resources depending ON them are deployed.
- Implemented By: IValidation
Implement this interface in order for the construct to be able to validate itself.
Name | Description |
---|---|
validate |
Validate the current construct. |
public validate(): string[]
Validate the current construct.
This method can be implemented by derived constructs in order to perform validation logic. It is called on all constructs before synthesis.
In what order to return constructs.
Name | Description |
---|---|
PREORDER |
Depth-first, pre-order. |
POSTORDER |
Depth-first, post-order (leaf nodes first). |
Depth-first, pre-order.
Depth-first, post-order (leaf nodes first).