AWS Solutions Constructs Core
The `aws-solutions-constructs-core` library provides base classes, helper functions, and common utilities for building and extending AWS Solutions Constructs patterns. It simplifies the creation of well-architected AWS infrastructure using the AWS Cloud Development Kit (CDK). Currently at version 2.101.0, it maintains a rapid release cadence, often updated weekly or bi-weekly to align with `aws-cdk-lib` releases.
Common errors
-
AttributeError: 'CfnBucket' object has no attribute 'bucket_name'
cause `aws-cdk-lib` version incompatibility. The Solutions Constructs library expects specific CDK API versions.fixCheck the `aws-solutions-constructs-core` package's `pyproject.toml` or PyPI page for its `Requires-Dist` or `aws-cdk-lib` dependency range. Install a compatible `aws-cdk-lib` version, e.g., `pip install aws-cdk-lib==2.101.0`. -
ModuleNotFoundError: No module named 'aws_solutions_constructs.aws_solutions_constructs_core'
cause The `aws-solutions-constructs-core` package is either not installed or the import path is incorrect.fixEnsure the package is installed: `pip install aws-solutions-constructs-core`. Verify the import statement matches the full module path: `from aws_solutions_constructs.aws_solutions_constructs_core import ...`. -
TypeError: Expected an instance of constructs.Construct, got <aws_cdk.aws_lambda.Function object at ...>
cause Often arises from a version mismatch between `aws-cdk-lib` and the underlying `constructs` package, or passing an object of the wrong type where a `constructs.Construct` is expected by a Solutions Construct.fixEnsure `aws-cdk-lib` and `aws-solutions-constructs-core` are compatible versions, as `aws-cdk-lib` typically handles the `constructs` dependency. When creating new constructs, always pass `self` (the current construct scope) as the first argument.
Warnings
- breaking AWS Solutions Constructs are tightly coupled to specific versions of `aws-cdk-lib`. Mismatched versions can lead to runtime errors, API inconsistencies, or unexpected behavior.
- gotcha The `aws-solutions-constructs-core` library provides base classes and helper functions. It is not designed to be instantiated directly for full patterns. Instead, it's used to extend or assist with other `aws-solutions-constructs.*` pattern libraries.
- gotcha Solutions Constructs abstracts away many underlying L1/L2 details for simplicity. If you need fine-grained control or access to low-level CloudFormation properties, you might need to use `.node.default_child` or similar, which can bypass the construct's intended abstraction and lead to future compatibility issues.
Install
-
pip install aws-solutions-constructs-core
Imports
- SolutionConstruct
from aws_solutions_constructs.aws_solutions_constructs_core import SolutionConstruct
- add_tags
from aws_solutions_constructs.aws_solutions_constructs_core import add_tags
- build_lambda_function
from aws_solutions_constructs.aws_solutions_constructs_core import build_lambda_function
Quickstart
import os
from aws_cdk import (
App,
Stack,
aws_lambda as _lambda,
)
from constructs import Construct
from aws_solutions_constructs.aws_solutions_constructs_core import add_tags
class MyCoreUsageStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create a basic Lambda function
my_function = _lambda.Function(
self, "MyFunction",
runtime=_lambda.Runtime.PYTHON_3_9,
handler="index.handler",
code=_lambda.Code.from_inline("def handler(event, context): return 'Hello from Lambda!'")
)
# Use a utility from aws_solutions_constructs_core to add tags to the Lambda
# add_tags is often used implicitly by pattern constructs, but can be used directly.
add_tags(my_function, {
"Project": "MyCDKApp",
"Environment": "Development"
})
app = App()
MyCoreUsageStack(app, "MyCoreUsageStack", env={
'account': os.environ.get('CDK_DEFAULT_ACCOUNT', ''),
'region': os.environ.get('CDK_DEFAULT_REGION', 'us-east-1')
})
app.synth()