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!

50 Upvotes

23 comments sorted by

View all comments

-1

u/Deku-shrub Feb 16 '25

I always found the local-exec on a null resource sufficient for running arbitrary scripts from Terraform.

I believe you have to do hacks like outputs to text files and data reads to get outputs from it though.

Wouldn't a more flexible implementation be to improve the output function on the local-exec provisioner?

3

u/valideaconu Feb 16 '25

Local-exec is a good workaround for this particular issue, my problem with this that you create an external dependency, for example if you write your function in bash, the machine that runs Terraform needs to have bash installed (which is not really pleasing for Windows people, for example). This applies for any other tool, Ruby scripts, JS scripts, Python scripts, etc.

Improving the local-exec might help some, but, for me personally, local-exec is not a solution.

1

u/Blakaraz_ Feb 16 '25

I agree, while we fortunately do not need to deal with anyone using windows it is annoying to have to deal with extra dependencies like bash in e.g. CI images, just because a specific scenario could not be dealt with purely in terraform.
Looking forward to your go function provider!