CDK (python) throws an error while I provision a Network Load Balancer, the code used and and error as follows,

0
from aws_cdk import (
    aws_elasticloadbalancingv2 as elbv2,
    aws_ec2 as ec2,
    core
)

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        vpc = ec2.Vpc(self, 'MyVpc')

        nlb = elbv2.NetworkLoadBalancer(self, 'MyNlb',
            vpc=vpc,
            internet_facing=True
        )

        # Add a listener to the load balancer
        nlb.add_listener('MyListener',
            port=80,
            open=True
        )

ERROR: ImportError: cannot import name 'core' from 'aws_cdk' (/home/gowtham/cdk_workshop/.venv/lib/python3.10/site-packages/aws_cdk/init.py)

  • Kindly suggest

Gowtham
gefragt vor 3 Monaten233 Aufrufe
1 Antwort
0

Hello.

I think it's probably because I'm using version 2 of CDK.
https://aws.amazon.com/jp/blogs/developer/experimental-construct-libraries-are-now-available-in-aws-cdk-v2/

I think the error occurred because the core module disappeared in CDK version 2.
https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html

AWS CDK v2 consolidates the stable parts of the AWS Construct Library, including the core library, into a single package, aws-cdk-lib. Developers no longer need to install additional packages for the individual AWS services they use. This single-package approach also means that you don't have to synchronize the versions of the various CDK library packages.

So I think you need to do something like this:

from aws_cdk import (
    aws_elasticloadbalancingv2 as elbv2,
    aws_ec2 as ec2
)
from constructs import Construct
profile picture
EXPERTE
beantwortet vor 3 Monaten
profile pictureAWS
EXPERTE
überprüft vor 3 Monaten
  • could you help me with the complete code as I have provided above? I am a bit confused here.

  • I think it will look something like this.

    from aws_cdk import (
        aws_elasticloadbalancingv2 as elbv2,
        aws_ec2 as ec2
    )
    from constructs import Construct
    
    class MyStack(core.Stack):
    
        def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
            super().__init__(scope, id, **kwargs)
    
            vpc = ec2.Vpc(self, 'MyVpc')
    
            nlb = elbv2.NetworkLoadBalancer(self, 'MyNlb',
                vpc=vpc,
                internet_facing=True
            )
    
            # Add a listener to the load balancer
            nlb.add_listener('MyListener',
                port=80,
                open=True
            )
    
  • Traceback (most recent call last): File "/home/gowtham/cdk_workshop/app.py", line 5, in <module> from cdk_workshop.cdk_workshop_stack import CdkWorkshopStack File "/home/gowtham/cdk_workshop/cdk_workshop/cdk_workshop_stack.py", line 7, in <module> class MyStack(core.Stack): NameError: name 'core' is not defined Thanks for looking into it.

  • Will it work if I change it like below?

    class MyStack(Stack):
    
  • I will try now

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen