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)

Gowtham
질문됨 3달 전255회 조회
1개 답변
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
전문가
답변함 3달 전
profile pictureAWS
전문가
검토됨 3달 전
  • 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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠