Conditionally creating resource by querying data blocks and storing them in ruby?

Hello Tung and the BoltOps Community,

I’m facing a situation where I think I’ve hit a limitation from pure Terraform HCL (and of course, as always the case, looking if Terraspace could help overcome this limitation).

I’m trying to deploy AWS AppStream stacks/fleets and configure its autoscaling policy. From, it mentions a few roles that is needed for the AppStream autoscaling.

The thing with the roles is… I think they don’t exist until we try to utilize AppStream autoscaling (where it would create it automatically)… So what happens is, when I deploy my code the first time, it would fail (because the roles don’t exist). But when I try to explicitly create the role, it would error saying that the roles already exists… And what I’d like to do is cater for both scenarios without having to recode/reconfigure input. I.e. what I want is to evaluate whether the resource exists, and only create it if it’s not there…

However, according to, it was never the intention for this conditional creation of resources to happen (because it blurs whether or not the object is managed by Terraform).

In any case, I was wondering if I’d able to do something like

data "aws_iam_role" "amazonappstreamserviceaccess" {
  name = "AmazonAppStreamServiceAccess"

to query the role, and somehow put the result into a ruby variable, and use erb templating to evaluate the variable, and conditionally create/ignore the creation of the roles? Or perhaps, is there a better way to do this?

Interested to know if someone’s encountered a similar challenge and managed to work around around it

Many thanks,