Minimalistic example repository for Terraform Language source code generation with Kotlin.
Generated code may also be executed through Kotlin subshells.
The source code used to write this example code is contained in repository:
Terraform infrastructure-as-code with Kotlin
The library uses Jitpack for package distribution. See configuration in:
- The dependency block of build.gradle.kts
- README of above linked repository
See different Kotlin files in src/main/kotlin:
- Init.kt creates the out-folder and its contents, it downloads the Terraform binary and executes the init-command of Terraform. This downloads the specified providers and creates a state file.
- Plan.kt executes the Terraform plan-command. Good to check changes before really applying them.
- Apply.kt executes the Terraform apply-command. Changed are applied in the cloud.
- Destroy.kt executes the Terraform destroy-command. Changed from before are being reverted.
Executor.kt Terraform commands are completed synchronous. It is possible to call them directly after each other. Example:
fun main() {
// Download Terraform binary to out-directory
Executor.downloadTerraformBinary(
"https://releases.hashicorp.com/terraform/1.3.3/terraform_1.3.3_windows_386.zip",
true
)
// Generate Terraform-files in out-directory (see method in Init.kt)
generateTfFiles()
// Setup workspace in out-directory
Executor.init()
// See changes
Executor.plan()
// Apply changes
Executor.apply()
// Revert changes
Executor.destroy()
}