SaltStack Pillar as Terraform external data source - Python

SaltStack is more than a configuration management tool, SaltStack Pillar could have a lot of data which may be needed somewhere else, e.g. Terraform!

So to avoid duplications, and after reading Terraform documentation "External Data Source", I wrote a Python script that works as external data source for Terraform. It simply reads from SaltStack Pillar, and returns the values in Terraform via "external" data source.

You can query any Pillar key from SaltStack and get the value in Terraform. And here we have 2 possibilities, this Pillar key has a string value, or it's a dictionary of strings (Terraform supports strings only as external data source values). But at the end all keys will be returned and accessible directly.

How to use

Let's have an example. So after clone the script, let's create Terraform conf:
cat << EOF > external_source.tf
data "external" "pillar" {
  program = ["python", "${path.module}/pillar_reader.py"]
  query = {
    # The value of this key is a string.
    baz = "foo:baz"
    # The value of this key is a dict of strings.
    foo_bar = "foo:bar"
So if we have a Pillar as like this:
  baz: "value"
    key01: "value01"
    key02: "value02"
Based on previous example, we can access the keys as following:

And that's it! It's a simple and easy bridge between SaltStack and Terraform!
More details are in the repo: terraform-external-data-source-saltstack-pillar.


Powered by Blogger.

Hello, my name is Ahmed AbouZaid and this is my "lite" technical blog!

I'm a passionate DevOps, Linux system engineer, Cloud/Kubernetes specialist, Free/Open source geek, author, interested in environment, calligraphy, and I believe that “Details Matter”!

DevOps transformation, automation, data, and metrics are my preferred areas. I have a built-in monitoring chip, and too lazy to do anything manually :D

Popular Posts