Tech and Digital Media

Sunday, January 1, 2023

[New post] Terraform – injecting local and remote Configuration Data

Site logo image Lucas Jellema posted: "This article describes how a Terraform plan can read configuration data at plan and application time from local or remote JSON documents to provide dynamic, easily and centrally maintainable settings that guide the provisioning process.Terraform variables" AMIS, Data Driven Blog - Oracle & Microsoft Azure

Terraform – injecting local and remote Configuration Data

Lucas Jellema

Jan 1

This article describes how a Terraform plan can read configuration data at plan and application time from local or remote JSON documents to provide dynamic, easily and centrally maintainable settings that guide the provisioning process.

image

Terraform variables are static: they are assigned using their default value (which cannot use function calls or references to other variables) or by overrides at startup time (from command line parameter or environment variable)

To use dynamic values - derived at run time using function calls and variable references - Terraform provides locals. These can be referred to using local.<name of local> in the same way as variables are used: var.<name of variable>. Locals are available only in the module in which they are defined.

Locals can easily be assigned values using functions in Terraform – functions for example for looking up values from a Map, for turning a JSON formatted string into a Map and for reading the content from a file into a string. With these functions at our disposal, it becomes easy to create a Terraform configuration that is driven in part by the actual contents of a JSON document outside the configuration. We will look first at the local file case, then at a situation with a file accessible through a URL.

The sources used in this articles are available on GitHub.

File variables.tf is the pivotal one:

image

The file variables.tf defines a variable environment, defined as "dev" and possibly overridden at run time. It also defines a few locals that are assigned files at run time:

The filename is constructed using the environment variable The resulting file is a json file that contains environment specific values. The content of that file is loaded into local json_string and converted to map environmentsettings. Finally, the values we care about - api_a_endpoint, api_b_endpoint and scaleFactor - are defined using the values looked up in the map.

File resources.tf defines four output-resources that print the respective values of the variable and the three locals.

image

Run this Terraform configuration using:

terraform init

terraform apply

The output is:

image

The Terraform configuration is processed and uses the values defined in the local JSON file dev-env-variables.json.

Next run

terraform apply -var environment=prod

and find this output

image

In the second case, the variable environment gets the value prod and this influences the local filename's value. The configuration settings are read from the other JSON document - prod-env-variables.json.

The file from which the map is created does  not have to be a local file, it can also be a document available at some accessible URL. For example: https://raw.githubusercontent.com/lucasjellema/oci-terraform-composites/main/lookup_%20variables_in_json/prod-env-variables.json.

WIth some small changes, we can make the Terraform configuration dynamically decide whether to retrieve the configuration data from the local file or from the remote document,

In variables.tf:

image

The reference to data.http.downloaded_document.response_body is linked to the new entry in the datasources.tf file:

image

which uses the Terraform http provider to download a document using an HTTP GET request.

With

terraform apply -var environment=dev -var configuration_origin=remote

we can apply the Terraform configuration, using the remote document with configuration data.

image

. After making two changes in the local configuration data document:

image

we can verify that this call

terraform apply -var environment=prod -var configuration_origin=local

reads the local document:

image

Note: I used ChatGPT while writing this article,. It gave me a number of suggestions for defining variables using functions (not possible in Terraform) as well as other code snippets that proved incorrect.

A small excerpt from our conversation:

image

I am baffled  by how ChatGPT responds, but underwhelmed about the quality of the (f)actual information and suggestions. I have seen many clearly incorrect code samples. I have asked where it found those, but these questions have gone unanswered.

Summary

We do not want to have to update our Terraform definiti0ns – treated as environment independent code, stored in source control – with each change in the environment or new and temporary insight into management settings. By externalizing the dynamic and environment specific values – we achieve some of that ambition. Managing the settings and configuration data becomes the next challenge of course. Use of Key Vault, Secret Store, Configuration Datastore and similar tools seem in order for many situations. A key or configuration data entry could be a JSON document that provides multiple key/value pairs. Or we can simply store a JSON document alongside the Terraform configuration or in a location that is accessible over HTTP from where the Terraform plan is applied.

This article showed a straightforward route to externalizing settings from the Terraform configuration by using both a local and remote JSON document.

Comment

Unsubscribe to no longer receive posts from AMIS, Data Driven Blog - Oracle & Microsoft Azure.
Change your email settings at manage subscriptions.

Trouble clicking? Copy and paste this URL into your browser:
https://technology.amis.nl/platform/terraform-injecting-local-and-remote-configuration-data/

Powered by Jetpack
Download on the App Store Get it on Google Play
at January 01, 2023
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

[New post] ‘Everyone Is Freaking Out’: Disney Explores Sale of ABC Network and Stations Amid Financial Challenges

...

  • [New post] ‘Everyone Is Freaking Out’: Disney Explores Sale of ABC Network and Stations Amid Financial Challenges
    ...
  • [New post] Asus is recruiting Android 12 beta testers for Zenfone 8
    Top Tech posted: " The Zenfone 8 announced in May with Android 11 already got a couple of Android 12 beta builds, but those...
  • [New post] Xiaomi’s Mi Smart Band 6 NFC is finally available in Europe officially
    Tech News For Today posted: "Xiaomi's Mi Smart Band 6 NFC is finally available in Europe officially At Xiaomi's bi...

Search This Blog

  • Home

About Me

Tech and Digital Media
View my complete profile

Report Abuse

Labels

  • 【ANDROID STUDIO】navigation
  • 【FLUTTER ANDROID STUDIO and IOS】backdrop filter widget
  • 【GAMEMAKER】Scroll Text
  • 【PYTHON】split train test
  • 【Visual Studio Visual Csharp】Message Box
  • 【Visual Studio Visual VB net】Taskbar properties
  • 【Vuejs】add dynamic tab labels labels exceed automatic scrolling

Blog Archive

  • September 2023 (502)
  • August 2023 (987)
  • July 2023 (954)
  • June 2023 (1023)
  • May 2023 (1227)
  • April 2023 (1057)
  • March 2023 (985)
  • February 2023 (900)
  • January 2023 (1040)
  • December 2022 (1072)
  • November 2022 (1145)
  • October 2022 (1151)
  • September 2022 (1071)
  • August 2022 (1097)
  • July 2022 (1111)
  • June 2022 (1117)
  • May 2022 (979)
  • April 2022 (1013)
  • March 2022 (982)
  • February 2022 (776)
  • January 2022 (681)
  • December 2021 (1197)
  • November 2021 (3156)
  • October 2021 (3212)
  • September 2021 (3140)
  • August 2021 (3271)
  • July 2021 (3205)
  • June 2021 (2984)
  • May 2021 (732)
Powered by Blogger.