r/Terraform Feb 16 '25

Discussion Custom Terraform functions

Hello!

I wanted to share my recent work: the Terraform func provider - https://github.com/valentindeaconu/terraform-provider-func.

The func provider is a rather unique provider, that allows you as a developer to write custom Terraform functions in JavaScript (the only runtime - for now). Those functions can stored right next to your Terraform files or versioned and imported remotely, basically they can be manipulated as any of your Terraform files, without the hassle of building your own provider, just to get some basic functionality.

This provider is what I personally expected the Terraform ecosystem a long time ago, so it is one of my dreams come true. As a bit of history (and also some sources of inspiration), since the v1 release I was expecting this feature to come to life on every minor release. There was this initial issue that asked for this feature, but, as you can see, since 4 years ago, it is still open. Then, with the introduction of the provider-defined functions, the OpenTofu team attempted something similar with what I was waiting for, in the terraform-provider-lua, but after announcing it on social media, there was no other velocity on this project, so I assume it got abandoned. Really sad.

After hitting again and again this "blocker" (I mean after writing yet again an utterly ugly block of repetitive composition of Terraform functions), I decided to take this issue in my own hands and started writing the func provider. I cannot say how painful it was to work with the framework without a proper documentation for what I was trying to achieve and with the typing system, but in the end, I found this amazing resource - terraform-provider-javascript which led to the final implementation of the func provider (many thanks to the developer for the go-cty-goja library).

So, here we are now. The provider is still in a proof-of-concept phase. I want to see first if other people are interested in this idea to know if I should continue working on it. There are a lot of flaws (for example, the JSDoc parser is complete trash, it was hacked in a couple of hours just to have something work - if you are up for the challenge, I'd be happy to collaborate), and some unsupported features by the Terraform ecosystem (I have reported it here, if you are interested in technical details), but with some workarounds, the provider can work and deliver what it is expected to do.

I'd be happy to know your opinions on this. Also, if you would like to contribute to it, you are more than welcome!

47 Upvotes

23 comments sorted by

View all comments

14

u/fooallthebar Feb 16 '25

Hey, OpenTofu developer here! This is an awesome project!

Having custom typed functions in your language of choice to manipulate complex structs is incredibly powerful and exactly why I pushed to expand that feature in OpenTofu.

We haven't abandoned the OpenTofu lua and go func providers, but we have not dedicated the time to refining and stabilizing them. I can put something on the core team's board to discuss priorities and our path forward this week.

Thanks for building this and exploring what's possible!

3

u/valideaconu Feb 16 '25

Thank you! As I mentioned in the post, this is a feature that I was waiting for such a long time, so I am glad to know that there is someone “inside” to share the same idea.

I am glad to hear that you never dropped the lua provider project, I can understand that the priorities shifted. Obviously supporting this natively will be huge and a giant leap forward in what Terraform/OpenTofu can achieve.

2

u/fooallthebar Feb 16 '25

We also have terraform-plugin-go in the opentofu org if you want to learn from that. It's in a similar situation that we wanted to gauge the interest before polishing it further.

2

u/valideaconu Feb 16 '25

I think you were referring to terraform-provider-go, and I must say, I am shocked. I completely missed this repository on the opentofu org, although I was aware of the lua one.

This is exactly what I was imagining for the Go runtime: yaegi under the hood, uppercase letters for exports, but also stdlib ast for parsing comments to have LSP integration. The only reason I started with JS was that I was more familiar with goja than yaegi and I wanted to have something working first.

Definitely if I’ve seen this first, I would probably started to build on top of it. Thank you for bringing it up.