Upgrading to CDK for Terraform Version 0.18
0.18 brings performance improvements to CDKTF along with improvements to the convert
command.
Python performance improvements disable root-level provider imports
When using a Provider in python one could previously import the resource and data source namespaces on the root level. This is no longer possible and the namespaces must be imported through specifying the path in the import. The syntax was supported before as well, so you can change your code within 0.17.x
and then upgrade to 0.18.x
.
Before:
from constructs import Constructfrom cdktf import App, TerraformStackfrom imports.aws import provider, sns_topic, lambda_function, iam_role class MyStack(TerraformStack): def __init__(self, scope: Construct, ns: str): super().__init__(scope, ns) provider.AwsProvider(self, 'Aws', region='eu-central-1') sns_topic.SnsTopic(self, 'Topic', display_name='my-first-sns-topic') role = iam_role.IamRole(self, 'Role', name='lambda-role', assume_role_policy='{}') lambda_function.LambdaFunction(self, 'Lambda', function_name='my-first-lambda-function', role=role.arn, handler='index.handler', runtime='python3.6') app = App()MyStack(app, "before-change")app.synth()
After:
from constructs import Constructfrom cdktf import App, TerraformStack# This syntax was supported before as wellfrom imports.aws.provider import AwsProviderfrom imports.aws.sns_topic import SnsTopicfrom imports.aws.lambda_function import LambdaFunctionfrom imports.aws.iam_role import IamRole class MyStack(TerraformStack): def __init__(self, scope: Construct, ns: str): super().__init__(scope, ns) AwsProvider(self, 'Aws', region='eu-central-1') SnsTopic(self, 'Topic', display_name='my-first-sns-topic') role = IamRole(self, 'Role', name='lambda-role', assume_role_policy='{}') LambdaFunction(self, 'Lambda', function_name='my-first-lambda-function', role=role.arn, handler='index.handler', runtime='python3.6') app = App()MyStack(app, "after-change")app.synth()
Further support
If you encounter anything that is missing from this guide or not working as expected, please don't hesitate to file a bug so we can improve this guide. You can also post your question to HashiCorp Discuss using the terraform-cdk category.