Question
Answer and Explanation
Yes, there will be an ImportError
when attempting to import the name aws_cdk
from aws_cdk4
.
The primary reason for this is that the correct package name for AWS Cloud Development Kit (CDK) v2 (which is what is commonly used and referenced in current guides) is aws-cdk-lib
, not aws_cdk4
, and there is not an attribute or name as aws_cdk
within that.
When using the AWS CDK v2, you need to import modules or specific constructs from aws_cdk_lib
directly.
Here's a more detailed explanation:
1. Incorrect Import Statement:
An ImportError
occurs because there is no module or package named aws_cdk4
, therefore from aws_cdk4 import aws_cdk
won't work.
Also, even if the hypothetical aws_cdk4
existed, the correct pattern to access constructs and tools in aws-cdk would not use 'aws_cdk
'. You should import them directly from the module instead of through an aliased import
2. Correct Import Syntax (for AWS CDK v2):
To properly import CDK constructs, you must use:
from aws_cdk_lib import core, aws_lambda, aws_s3
Or:
import aws_cdk_lib as cdk
These would then let you refer to the imported name with either 'cdk' as in cdk.Stack()
or through referencing core.Stack()
. You would not ever import `aws_cdk` to begin with.
Here is what would cause an ImportError
from aws_cdk4 import aws_cdk
3. Explanation
The primary package for CDK constructs is 'aws-cdk-lib', if you wish to shorten and alias you may do as such through import aws_cdk_lib as cdk
for instance.
4. Version Matters
For any imports the version matters heavily, double check what version of CDK you are targetting by seeing aws_cdk_lib --version
in the cli, or viewing its requirements from within your projects package management file
Therefore, attempting to import aws_cdk
from a non existent aws_cdk4
package will certainly cause an ImportError
. Be sure to correctly install and import constructs through aws_cdk_lib
in aws cdk v2 (the current standard)