AWS Constructs Library

10.6.0 · active · verified Thu Apr 09

The `constructs` library, currently at version 10.6.0, provides a programming model for defining software-defined state. It is the fundamental building block for creating reusable, composable infrastructure components, most notably used as the core for the AWS Cloud Development Kit (CDK). The library is actively maintained with releases often synchronized with major CDK updates.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define and instantiate custom constructs using the `constructs` library. It shows how constructs form a hierarchical tree, how to access their `Node` for metadata and path information, and how to define a simple root scope for demonstration purposes. In a real application, `MyRootApp` would typically be `aws_cdk.App` or an `aws_cdk.Stack`.

from constructs import Construct, Node

class MyRootApp(Construct):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

class MyComponent(Construct):
    def __init__(self, scope: Construct, id: str, value: str):
        super().__init__(scope, id)
        self.node.add_metadata('custom_value', value)
        print(f"Created component: {self.node.path} with value: {value}")
        print(f"Node ID: {self.node.id}, Parent ID: {self.node.scope.node.id}")

# To demonstrate, we create a root construct
# In a real CDK application, this would typically be an `App` or `Stack`.
# For pure `constructs` library usage, we can create a base construct as root.
root = MyRootApp(None, "MyApplication") # Root construct has no parent scope

# Instantiate custom components within the root scope
component1 = MyComponent(root, "FirstComponent", "Hello")
component2 = MyComponent(root, "SecondComponent", "World")

# Accessing attributes of a child construct
print(f"\nMetadata for {component1.node.id}: {component1.node.metadata}")

view raw JSON →