DataTerraformRemoteState expects arguments 'organization' and 'workspaces'

I’m trying to read remote Terraform state to grab an output for a TF module instantiated from another codebase. The source state uses the AWS S3 backend.

I tried to use a DataTerraformRemoteState as documented. In that document the example uses Terraform cloud workspace.

In our case there’s no Hashicorp Terraform Cloud workspace, therefore no organization and workspaces parameters apply. However the class still expects these two arguments.

...
      File "/Users/alp/code/platform-infrastructure/infrastructure/apps/main.py", line 94, in __init__
        eks_clusters = DataTerraformRemoteState(
      File "/Users/alp/.local/share/virtualenvs/apps-KSi_EQU-/lib/python3.9/site-packages/jsii/_runtime.py", line 111, in __call__
        inst = super().__call__(*args, **kwargs)
    TypeError: __init__() missing 2 required keyword-only arguments: 'organization' and 'workspaces'

What’s the way to read outputs from an S3Backend state?

Ah, that’s a bit confusing indeed. The DataTerraformRemoteState refers to the cloud or remote backends, hence the requirement for specifying workspace and organization. In order to refer to remote states from other backends, we have specific classes for those.

For the S3 backend: DataTerraformRemoteStateS3 should be used.

We do have an example of using the S3 backend remote state in Typescript that should be helpful too: terraform-cdk/main.ts at main · hashicorp/terraform-cdk · GitHub

We’ll try to clarify this in our documentation soon.

1 Like

Thank you mutahhir. I have two follow-up questions.

Given the below usage of this class:

            remote_state = DataTerraformRemoteStateS3(
                    self,
                    resource_instance_name + "-app-deployer",
                    bucket=state_bucket_name,
                    key=state_file_name
            )

If I use a bogus value for bucket, then I get no errors during synth; so I’m a bit confused, but I’m guessing that the remote state only gets read during deployment?

Within that state there’s a module output called eks_clusters which is a map of maps. I need to pass this as an input variable to another module, and couldn’t get anywhere with the below line…

Token.as_map(remote_state.get("eks_clusters"))

There’s no get_map(). How would one access this data?

Yes, indeed. CDKTF does not know at build time the validity of resources / data managed by Terraform.

The data you’re receiving is during runtime, so you will have to use the Fn.* API to extract the data. For example, if you were to look up the key "eks_clusters", it might look like: Fn.lookup(remote_state, "eks_clusters", ""), which will get translated into HCL as ${lookup(remote_state, "eks_clusters", "")} which can then be managed by Terraform properly.

1 Like