Example documentation for cdktf python is not pulling the correct libraries

Greetings,

I am following these directions for python:

But every time I try cdk deploy, I get this error:

POC_Python_terraform % cdktf deploy

⠇  Synthesizing
[2022-10-05T08:32:18.310] [ERROR] default -   File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 20
    )TypeError: 'module' object is not callable
     ^
ERROR: cdktf encountered an error while synthesizing

Synth command: pipenv run python main.py
Error:         non-zero exit code 1

Command output on stderr:

      File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 20
        )TypeError: 'module' object is not callable
         ^
    SyntaxError: invalid syntax




⠼  Synthesizing

I decided to take out the TerraformOutput segment to just test it, and I now I get a different error:

POC_Python_terraform % cdktf deploy

⠹  Synthesizing
[2022-10-05T08:33:51.375] [ERROR] default - Traceback (most recent call last):
  File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 4, in <module>
    from cdktf_cdktf_provider_aws import AwsProvider, ec2
ImportError: cannot import name 'AwsProvider' from 'cdktf_cdktf_provider_aws' (/Users/matt/.local/share/virtualenvs/POC_Python_terraform-2Corphkz/lib/python3.9ERROR: cdktf encountered an error while synthesizing

Synth command: pipenv run python main.py
Error:         non-zero exit code 1

Command output on stderr:

    Traceback (most recent call last):
      File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 4, in <module>
        from cdktf_cdktf_provider_aws import AwsProvider, ec2
    ImportError: cannot import name 'AwsProvider' from 'cdktf_cdktf_provider_aws' (/Users/matt/.local/share/virtualenvs/POC_Python_terraform-2Corphkz/lib/python3.9/site-packages/cdktf_cdktf_provider_aws/__init__.py)

I dig around and tried adding the following to my cdktf.json file:

  "terraformProviders": ["aws@~> 4.0"],

and did a ‘cdktf get’, however, I still get the same error. Any help is appreciated.

Thanks,
Matt

Hi Matt,

I’m sorry for the confusion. We just released CDKTF version 0.13 which changes the way providers are generated by CDKTF.

We’re updating our examples that still talk about version 0.12, but meanwhile, you can look at our Python AWS example that works with version 0.13.

Hope this helps.

I followed those directions, I even copy and pasted the example script, and I still get the same Import error:

% cdktf synth   

⠴  Synthesizing
[2022-10-05T10:18:19.395] [ERROR] default - Traceback (most recent call last):
  File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 4, in <module>
    from imports.aws import SnsTopic, AwsProvider
ERROR: cdktf encountered an error while synthesizing

Synth command: pipenv run python main.py
Error:         non-zero exit code 1

Command output on stderr:

    Traceback (most recent call last):
      File "/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/main.py", line 4, in <module>
        from imports.aws import SnsTopic, AwsProvider
    ImportError: cannot import name 'SnsTopic' from 'imports.aws' (/Users/matt/Repos/Cloud%20Operations/POC_Python_terraform/imports/aws/__init__.py)




⠧  Synthesizing

0.13 changed how terraform provider packages are built so that now there are multiple subpackages.
For your example, try replacing from imports.aws import SnsTopic, AwsProvider with

from imports.aws.provider import AwsProvider
from imports.aws.sns_topic import SnsTopic

if you are using pre-built provider of AWS, then you should use below format.

from cdktf_cdktf_provider_aws.provider import AwsProvider

the best way to understand the code is to import a module in ipython and go over the libraries and classes.

I can create a youtube video/documentation for everyone to reference.