Terraform
BigConfig main idea is that it is better to write Clojure to generate JSON instead of HCL.
(ns core (:require [big-config.utils :refer [deep-merge sort-nested-map]] [big-tofu.core :refer [add-suffix construct]] [big-tofu.create :as create] [cheshire.core :as json]))
(let [{:keys [aws-account-id region] :as data} {:aws-account-id "111111111111" :region "eu-west-1" :module "example"} queues (->> (for [n (range 2)] (create/sqs (add-suffix :alpha/big-sqs (str "-" n)))) flatten (map construct)) kms (->> (create/kms :alpha/big-kms) (map construct)) bucket (format "tf-state-%s-%s" aws-account-id region) provider (create/provider (assoc data :bucket bucket)) m (->> [provider] (into kms) (into queues) (apply deep-merge) sort-nested-map)] (json/generate-string m {:pretty true}))
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" { alpha_big_kms_data_policy = { statement = { actions = ["kms:*"] effect = Allow principals = { identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] type = AWS } resources = ["*"] } }}
provider "aws" { region = eu-west-1}
resource "aws_kms_key" "alpha_big_kms" {}
resource "aws_kms_key_policy" "alpha_big_kms_resource_policy" { key_id = resource.aws_kms_key.alpha_big_kms.id policy = data.aws_iam_policy_document.alpha_big_kms_data_policy.json}
resource "aws_sqs_queue" "alpha_big_sqs_0" { name = alpha_big_sqs_0}
resource "aws_sqs_queue" "alpha_big_sqs_1" { name = alpha_big_sqs_1}
terraform { backend "s3" { bucket = tf-state-111111111111-eu-west-1 encrypt = true key = example.tfstate region = eu-west-1 }
required_providers "aws" { source = hashicorp / aws version = "~> 5.0" } required_version = ">= 1.8.0"}
{ "data" : { "aws_caller_identity" : { "current" : { } }, "aws_iam_policy_document" : { "alpha_big_kms_data_policy" : [ { "statement" : [ { "actions" : [ "kms:*" ], "effect" : "Allow", "principals" : [ { "identifiers" : [ "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root" ], "type" : "AWS" } ], "resources" : [ "*" ] } ] } ] } }, "provider" : { "aws" : { "region" : "eu-west-1" } }, "resource" : { "aws_kms_key" : { "alpha_big_kms" : { } }, "aws_kms_key_policy" : { "alpha_big_kms_resource_policy" : { "key_id" : "${resource.aws_kms_key.alpha_big_kms.id}", "policy" : "${data.aws_iam_policy_document.alpha_big_kms_data_policy.json}" } }, "aws_sqs_queue" : { "alpha_big_sqs_0" : { "name" : "alpha_big_sqs_0" }, "alpha_big_sqs_1" : { "name" : "alpha_big_sqs_1" } } }, "terraform" : { "backend" : { "s3" : { "bucket" : "tf-state-111111111111-eu-west-1", "encrypt" : true, "key" : "example.tfstate", "region" : "eu-west-1" } }, "required_providers" : { "aws" : { "source" : "hashicorp/aws", "version" : "~> 5.0" } }, "required_version" : ">= 1.8.0" }}
Requirements
Section titled “Requirements”BigConfig requires Clojure and Babashka. You should also configure dirvenv for your shell.
brew install clojure/tools/clojurebrew install borkdude/brew/babashkabrew install direnv
devbox global add clojuredevbox global add babashkadevbox global add direnv
Getting started
Section titled “Getting started”- Create the Terraform project
Terminal window # Add big-config as tool to Clojureclojure -Ttools install-latest :lib io.github.amiorin/big-config :as big-config# Invoke the terraform templateclojure -Tbig-config terraform - Configure git
Terminal window git initgit addgit commitgit push - Shows the Babashka tasks
Terminal window bb tasks - Show the BigConfig DSL manual.
Terminal window bb show-help
Conclusion
Section titled “Conclusion”Terraform supports both HCL and JSON files. Instead of learning a new language (HCL) you can use Clojure to generate the JSON files.
Terraform library Clojure functions to help you to generate Terraform JSON files.