Using google and google-beaa providers with python

Hello
I am a newbie here (sorry).

I have two providers set in the cdktf.json file :

 "terraformProviders": ["google@~> 3.84.0", "google-beta@~> 3.84.0"], 

For both my google-beta resources I need to specify the provider within the setting :

#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack
from imports.google import FirestoreDocument
from imports.google_beta import GoogleDataCatalogTaxonomy, GoogleDataCatalogPolicyTag

class MyStack(TerraformStack):
  def __init__(self, scope: Construct, ns: str):
    super().__init__(scope, ns)
    
    FirestoreDocument(self,'cdktf_test_classification', project='dev', collection='taxonomies',document_id='cdktf_test_classification', fields='')
    GoogleDataCatalogTaxonomy(self, 'cdktf_test_taxonomy',  provider='google-beta', project='dev', region='europe-west2', display_name='cdktf_test_classification', description='my description', activated_policy_types=['FINE_GRAINED_ACCESS_CONTROL'])
    GoogleDataCatalogPolicyTag(self, 'cdktf_test_unclassified_policy_tag', provider='google-beta', taxonomy='1234', display_name='cdktf_test_Unclassified', description='my description')


app = App()
MyStack(app, "hello-terraform")

app.synth()

When I run this however I get an error :

[2021-09-16T15:25:16.267] [ERROR] default - jsii.errors.JavaScriptError: 
  Error: Expected object reference, got "google-beta"
      at Object.deserialize .....

If I remove the provider and proceed through to run a terrafrom plan I end up with a provider error as the plan tries to use the google provider not google-beta ?

β”‚ Error: Invalid resource type
β”‚ 
β”‚   on cdk.tf.json line 37, in resource:
β”‚   37:     "google_data_catalog_taxonomy": {
β”‚ 
β”‚ The provider hashicorp/google does not support resource type "google_data_catalog_taxonomy".
β•΅
β•·
β”‚ Error: Invalid resource type
β”‚ 
β”‚   on cdk.tf.json line 67, in resource:
β”‚   67:     "google_data_catalog_policy_tag": {
β”‚ 
β”‚ The provider hashicorp/google does not support resource type "google_data_catalog_policy_tag".

How do I point to the google-beta resource here ?

If I go into the cdk.tf.json file that is produced and add the missing provider tag manually

            "provider": "google-beta",

The terraform plan completes as expected, I need to figure out how to include the provider tag in the main.py google-beta resources.

Hey, I think I see what’s going on:

The provider key you are passing takes an instantiated provider and not a string:

#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack
from imports.google import FirestoreDocument
from imports.google_beta import GoogleProvider GoogleDataCatalogTaxonomy, GoogleDataCatalogPolicyTag

class MyStack(TerraformStack):
  def __init__(self, scope: Construct, ns: str):
    super().__init__(scope, ns)
    googleBeta = GoogleProvider(self, "google-beta")
    
    FirestoreDocument(self,'cdktf_test_classification', project='dev', collection='taxonomies',document_id='cdktf_test_classification', fields='')
    GoogleDataCatalogTaxonomy(self, 'cdktf_test_taxonomy',  provider=googleBeta, project='dev', region='europe-west2', display_name='cdktf_test_classification', description='my description', activated_policy_types=['FINE_GRAINED_ACCESS_CONTROL'])
    GoogleDataCatalogPolicyTag(self, 'cdktf_test_unclassified_policy_tag', provider=googleBeta, taxonomy='1234', display_name='cdktf_test_Unclassified', description='my description')


app = App()
MyStack(app, "hello-terraform")

app.synth()

I’m a newbie in Python, you should import the GoogleProvider from the normal google provider as well, not sure how to rename the imports

1 Like

Thanks @DanielMSchmidt that has progressed me here. :grinning: