Skip to content

Commit

Permalink
feat: generate backends
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmartius committed Nov 13, 2024
1 parent 417bc0c commit 57bb641
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 28 deletions.
11 changes: 11 additions & 0 deletions 01_example_outputs/aks/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/d0901d9c-0854-4af8-b0ca-813a63d1aaac/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
11 changes: 11 additions & 0 deletions 01_example_outputs/flux/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/6cd87f96-218f-4a72-8be1-88104b460c05/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
11 changes: 11 additions & 0 deletions 01_example_outputs/network/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/f20c9067-70ef-4489-8a70-9d1580ab67f7/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
11 changes: 11 additions & 0 deletions 02_example_data_sources/network/aks/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/5d97b4f1-4e0a-4831-8ea8-ad2d1b90f7b5/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
15 changes: 15 additions & 0 deletions 02_example_data_sources/network/aks/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

data "terraform_remote_state" "vpc" {
backend = "azurerm"
config = {
container_name = "tfstate"
key = "terraform/stacks/by-id/5999e860-86d6-49b6-aa18-c91df7586483/terraform.tfstate"
storage_account_name = "tfstatexscz2"
}
depends_on = [
null_resource.deployment_trigger,
]
}
resource "null_resource" "deployment_trigger" {
}
12 changes: 12 additions & 0 deletions 02_example_data_sources/network/aks/data.tf.tmgen
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
data "terraform_remote_state" "vpc" {
backend = "azurerm"
config = {
storage_account_name = global.terraform.backend.azurerm.storage_account_name
container_name = tm_try(global.terraform.backend.azurerm.container_name, "tfstate")
key = "terraform/stacks/by-id/${global.from_stack}/terraform.tfstate"
}

depends_on = [null_resource.deployment_trigger]
}

resource "null_resource" "deployment_trigger" {}
11 changes: 11 additions & 0 deletions 02_example_data_sources/network/aks/flux/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/2ac5c655-b876-44dd-8ab7-137d07e67097/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
15 changes: 15 additions & 0 deletions 02_example_data_sources/network/aks/flux/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

data "terraform_remote_state" "cluster" {
backend = "azurerm"
config = {
container_name = "tfstate"
key = "terraform/stacks/by-id/5d97b4f1-4e0a-4831-8ea8-ad2d1b90f7b5/terraform.tfstate"
storage_account_name = "tfstatexscz2"
}
depends_on = [
null_resource.deployment_trigger,
]
}
resource "null_resource" "deployment_trigger" {
}
12 changes: 12 additions & 0 deletions 02_example_data_sources/network/aks/flux/data.tf.tmgen
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
data "terraform_remote_state" "cluster" {
backend = "azurerm"
config = {
storage_account_name = global.terraform.backend.azurerm.storage_account_name
container_name = tm_try(global.terraform.backend.azurerm.container_name, "tfstate")
key = "terraform/stacks/by-id/${global.from_stack}/terraform.tfstate"
}

depends_on = [null_resource.deployment_trigger]
}

resource "null_resource" "deployment_trigger" {}
19 changes: 0 additions & 19 deletions 02_example_data_sources/network/aks/flux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@ provider "azurerm" {
features {}
}

data "terraform_remote_state" "cluster" {
backend = "local"

config = {
path = "../terraform.tfstate"
}

depends_on = [null_resource.deployment_trigger]
}

# data "azurerm_kubernetes_cluster" "cluster" {
# name = "dev2-aks-cluster-aks"
# resource_group_name = "${var.environment}-aks-env-out"

# depends_on = [null_resource.deployment_trigger]
# }

resource "null_resource" "deployment_trigger" {}

provider "helm" {
kubernetes {
host = "https://${data.terraform_remote_state.cluster.outputs.host}"
Expand Down
4 changes: 4 additions & 0 deletions 02_example_data_sources/network/aks/flux/stack.tm.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ stack {
description = "Flux Controller deployed via Helm in Kubernetes"
id = "2ac5c655-b876-44dd-8ab7-137d07e67097"
}

globals {
from_stack = "5d97b4f1-4e0a-4831-8ea8-ad2d1b90f7b5"
}
15 changes: 7 additions & 8 deletions 02_example_data_sources/network/aks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ resource "random_id" "name" {
# depends_on = [null_resource.deployment_trigger]
# }

data "terraform_remote_state" "vpc" {
backend = "local"
# data "terraform_remote_state" "vpc" {
# backend = "local"

config = {
path = "../terraform.tfstate"
}
# config = {
# path = "../terraform.tfstate"
# }

depends_on = [null_resource.deployment_trigger]
}
# depends_on = [null_resource.deployment_trigger]
# }

resource "null_resource" "deployment_trigger" {}

module "cluster" {
source = "Azure/aks/azurerm"
Expand Down
4 changes: 4 additions & 0 deletions 02_example_data_sources/network/aks/stack.tm.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ stack {
description = "Managed Kubernetes cluster on Azure"
id = "5d97b4f1-4e0a-4831-8ea8-ad2d1b90f7b5"
}

globals {
from_stack = "5999e860-86d6-49b6-aa18-c91df7586483"
}
11 changes: 11 additions & 0 deletions 02_example_data_sources/network/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

terraform {
backend "azurerm" {
container_name = "tfstate"
key = "terraform/stacks/by-id/5999e860-86d6-49b6-aa18-c91df7586483/terraform.tfstate"
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
use_oidc = true
}
}
21 changes: 21 additions & 0 deletions config.tm.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# globals "tofu" {
# version = "1.8.5"
# }

# globals "terraform" {
# version = "1.9.8"
# }

globals "terraform" "providers" "azurerm" {
enabled = true

source = "hashicorp/azurerm"
version = "~> 3.0"
features = {}
}

globals "terraform" "backend" "azurerm" {
resource_group_name = "tfstate"
storage_account_name = "tfstatexscz2"
container_name = "tfstate"
}
3 changes: 3 additions & 0 deletions imports.tm.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {
source = "./imports/*.tm.hcl"
}
18 changes: 18 additions & 0 deletions imports/backend.tm.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
generate_hcl "backend.tf" {
condition = tm_alltrue([
tm_can(tm_try(global.terraform.backend.azurerm, false)),
!tm_contains(terramate.stack.tags, "no-backend")
])

content {
terraform {
backend "azurerm" {
resource_group_name = global.terraform.backend.azurerm.resource_group_name
storage_account_name = global.terraform.backend.azurerm.storage_account_name
container_name = tm_try(global.terraform.backend.azurerm.container_name, "tfstate")
key = "terraform/stacks/by-id/${terramate.stack.id}/terraform.tfstate"
use_oidc = true
}
}
}
}
3 changes: 2 additions & 1 deletion terramate.tm.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ terramate {
}
}

# Enable Terramate Scripts
# Enable Features
experiments = [
"scripts",
"outputs-sharing",
"tmgen"
]
}
}

0 comments on commit 57bb641

Please sign in to comment.