Storing resource attributes as variables in python


I am using cdktf to leverage both Terraform and direct API calls and need to store one of the attributes from a resource that I am creating/updating in a variable for some of those API calls. I’ve tried playing with TerraformOutput but haven’t succeeded. Any suggestions?

Hi @julian!

you could use Terraform to create a file which can be read by your subsequent scripts that do the API calls. Here’s an example for such a workflow.

Out of interest: What kinds of API calls do you invoke after provisioning your infrastructure? What is the underlying workflow there?


1 Like

Thanks for the response @ansgarm

I’ll take a look at this. I take it that there is no way to do this for passing attributes to in-line function calls? Please excuse my lack of experience on this. :smile:

The provider I am using is missing some resources needed for my workflow. So basically:
API call to create thing → Terraform to provision most of thing → More API calls to finish what Terraform couldn’t

Hi @julian!

I take it that there is no way to do this for passing attributes to in-line function calls?

Inline as in the CDK for Terraform stack / codebase?

Ah, I see. Is that some kind of internal API or a public API / Service / SaaS product?

Yeah, within the stack/codebase. Ideally I’d be able to do something like:


It’s a SaaS product API. I am solving for the missing resources by using those direct API calls.

While it isn’t directly supported in cdktf (need to use escape hatches), you could use provisioners on a null resource. It’s not a generally recommended approach, but it may be the best option for your situation.

1 Like

Thanks for this as well!

Not sure if it’s a good design practice, but could I collect the values after calling my stack and then proceed to pass them to other functions from there?


app = App()
MyStack(app, "name-here")


var = someWayToCollectResourceOutput.attribute

There isn’t currently an API for running the deploy (synth just creates a Terraform file to be ran by the deploy command).
You could rig something up that calls the cli commands and checks some outputted values, but it would be a bit rough.