DEV Community

karvounis
karvounis

Posted on

Introducing a Terraform provider for Forem

I am glad to announce that I have just released a Terraform provider for Forem! This provider is my small contibution to this amazing open-source project. You can find its source code on Github and the provider's page on Terraform registry.

TL;DR

Manage your Forem resources using code! Forem as Code (FaC) following the Infrastructure as Code (IaC) concept.

Need

Forem is a great software project and the platform that powers dev.to. When I published my first article in dev.to, I found it very easy to navigate through the browser interface of Forem, create, write, and edit the article.

However, while I was writing the second article, I realised that there are a lot of similarities between them and that certain things can be automated. I also realised that the more articles you publish, the harder their maintenance is going to get. Using just the browser does not scale well and there is certainly room for automation.

I decided to use Terraform to automate the management of Forem resources. Quoted from their website:

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

If there is an API, you can create a Terraform provider for it!

API

Forem has an API that you can use in order to create and update articles, listings and a few other resources. Instead of using the browser, you can send HTTP requests to the api endpoint of the Forem installation and read, create or update the resources you need.

Example

You can either visit the linkg https://dev.to/karvounis/basic-traefik-configuration-tutorial-593m from your browser or send the following curl request:

curl https://dev.to/api/articles/karvounis/basic-traefik-configuration-tutorial-593m
Enter fullscreen mode Exit fullscreen mode

How to use it

The provider requires two arguments:

  • api_key (String) API key to be able to communicate with the FOREM API. Can be specified with the FOREM_API_KEY environment variable.
  • host (String) Host of the FOREM API. You can specify the dev.to or any other Forem installation. Can be specified with the FOREM_HOST environment variable. Default: https://dev.to/api.

In order to generate an API key, go to Settings -> Account -> DEV Community API Keys, give it a proper description and press the Generate API Key button.

Forem Articles through Terraform

You can create a new Forem article using the examples shown here. In this example, both example_file and example_full articles use the same tags that are defined in the locals block.

Imagine having way more articles maintained by Terraform and you need to remove a specific tag from all of them. With this provider, you can just remove that tag from the list, re-run the plan and Terraform will be smart enough to understand the difference and remove this tag from all the articles. By doing that, we saved ourselves a lot of clicks!

Currently supported

Currently, the lastest Terraform provider version is 1.0.1 and supports the following Forem resources and actions:

Resources Actions
Articles Create, Update, Read, Import
Listings Create, Update, Read, Import
Data Sources Actions
Articles Get By ID, Get By username and slug
Listings Get By ID
Users Get By ID, Get By username

Conclusion

I really hope that this provider is going to help all people using Forem. In fact, this very article has been generated using this provider! Don’t take my word for it, check the Terraform article resource and the actual Markdown file! πŸ˜‰

Any contributions to the provider are welcome and I would appreciate any feedback in the comments section! Cheers!

Useful links

Top comments (2)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Very cool!! πŸ™Œ

Seriously, this is a really helpful addition to Forem. I'm siked to see that you've already shared it on forem.dev so Forem Creators know about it too.

I'm also going to add this to a post I wrote up a while back where I'm urging folks to drop their favorite DEV plugins/accompanying apps:

Thanks so much for creating this!! πŸ˜€

Collapse
 
karvounis profile image
karvounis

Yes please Ξ±dd it! I am glad that this provider can help this project. You can also use it to manage your articles ;)