12/08/2018

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"
  }
}
EOF
So if we have a Pillar as like this:
foo:
  baz: "value"
  bar:
    key01: "value01"
    key02: "value02"
Based on previous example, we can access the keys as following:
${data.external.pillar.result.baz}
${data.external.pillar.result.key01}
${data.external.pillar.result.key02}
${data.external.pillar.result.key03}

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.

Enjoy!

Powered by Blogger.

Hello, my name is Ahmed AbouZaid, I'm a passionate Tech Lead DevOps Engineer. 👋

I specialize in Cloud-Native and Kubernetes. I'm also a Free/Open source geek and book author. My favorite topics are DevOps transformation, DevSecOps, automation, data, and metrics.

More about me ➡️

Contact Me

Name

Email *

Message *

Start Your DevOps Engineer Journey!

Start Your DevOps Engineer Journey!
Start your DevOps career for free the Agile way with the Dynamic DevOps Roadmap ⭐

Latest Post

10 Helm Chart Development Best Practices - Kubernetes

About 10 years ago, I used SaltStack extensively and wrote my top 10 best practices for SaltStack formulas . Today, I do the ...

Popular Posts

Blog Archive