Skip to content

Commit

Permalink
FIX - edb-terraform cli - precompute tags so they are known during 't…
Browse files Browse the repository at this point in the history
…erraform plan'
  • Loading branch information
bryan-bar committed Dec 13, 2024
1 parent 334c07e commit 12a8d52
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 42 deletions.
17 changes: 9 additions & 8 deletions edbterraform/data/terraform/aws/modules/kubernetes/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
data "aws_availability_zones" "available" {}

data "aws_eks_cluster" "cluster" {
name = module.eks.cluster_name
}

data "aws_eks_cluster_auth" "cluster" {
name = module.eks.cluster_name
}

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.9.0"
Expand Down Expand Up @@ -56,3 +48,12 @@ module "eks" {

tags = var.tags
}

# Defer data read until the cluster is created
data "aws_eks_cluster" "cluster" {
name = can(module.eks) ? module.eks.cluster_name : module.eks.cluster_name
}

data "aws_eks_cluster_auth" "cluster" {
name = can(module.eks) ? module.eks.cluster_name : module.eks.cluster_name
}
4 changes: 0 additions & 4 deletions edbterraform/data/terraform/aws/modules/specification/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
resource "random_id" "apply" {
byte_length = 4
}

resource "time_static" "first_created" {
}

Expand Down
20 changes: 10 additions & 10 deletions edbterraform/data/terraform/aws/modules/specification/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
locals {
# Expects included tags for tracking:
# - terraform_hex
# - terraform_id
# - terraform_time
tags = merge(var.spec.tags, {
# add ids for tracking
terraform_hex = random_id.apply.hex
terraform_id = random_id.apply.id
terraform_time = time_static.first_created.id
created_by = local.created_by
cluster_name = local.cluster_name
})
Expand Down Expand Up @@ -78,7 +78,7 @@ locals {
tags = merge(local.tags, machine_spec.tags, {
# machine module specific tags
# Use 'Name' tag to have instance name set for AWS UI
Name = format("%s-%s-%s", (machine_spec.count > 1 ? "${name}-${index}" : name), local.cluster_name, random_id.apply.hex)
Name = format("%s-%s-%s", (machine_spec.count > 1 ? "${name}-${index}" : name), local.cluster_name, local.tags.terraform_hex)
})
# assign operating system from mapped names
# add private and public key paths so they can be passed in the machine outputs
Expand Down Expand Up @@ -114,7 +114,7 @@ output "region_databases" {
# spec project tags
tags = merge(local.tags, database_spec.tags, {
# database module specific tags
Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.hex)
Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_hex)
})
})
}...
Expand All @@ -129,7 +129,7 @@ output "region_auroras" {
# spec project tags
tags = merge(local.tags, aurora_spec.tags, {
# aurora module specific tags
Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.id)
Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_id)
})
})
}...
Expand All @@ -149,7 +149,7 @@ output "biganimal" {
# spec project tags
tags = merge(local.tags, biganimal_spec.tags, {
# Biganimal reserves the Name tag
# Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.id)
# Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_id)
})
data_groups = {
for data_group_name, data_group_spec in biganimal_spec.data_groups : data_group_name => merge(data_group_spec, {
Expand All @@ -166,7 +166,7 @@ output "biganimal" {
}

output "hex_id" {
value = random_id.apply.hex
value = local.tags.terraform_hex
}

output "pet_name" {
Expand All @@ -181,7 +181,7 @@ output "region_kubernetes" {
# spec project tags
tags = merge(local.tags, spec.tags, {
# kubernetes module specific tags
Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.id)
Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_id)
})
})
}...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
resource "random_id" "apply" {
byte_length = 4
}

resource "time_static" "first_created" {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
locals {
# Expects included tags for tracking:
# - terraform_hex
# - terraform_id
# - terraform_time
tags = merge(var.spec.tags, {
# add ids for tracking
terraform_hex = random_id.apply.hex
terraform_id = random_id.apply.id
terraform_time = time_static.first_created.id
created_by = local.created_by
cluster_name = local.cluster_name
})
Expand Down Expand Up @@ -139,7 +139,7 @@ output "biganimal" {
# spec project tags
tags = merge(local.tags, biganimal_spec.tags, {
# Biganimal reserves the Name tag
# Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.id)
# Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_id)
})
data_groups = {
for data_group_name, data_group_spec in biganimal_spec.data_groups : data_group_name => merge(data_group_spec, {
Expand Down Expand Up @@ -171,7 +171,7 @@ output "region_kubernetes" {
}

output "hex_id" {
value = random_id.apply.hex
value = local.tags.terraform_hex
}

output "pet_name" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ EOT
}
}

resource "random_id" "apply" {
byte_length = 4
}

resource "time_static" "first_created" {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
locals {
# Expects included tags for tracking:
# - terraform_hex
# - terraform_id
# - terraform_time
tags = merge(var.spec.tags, {
# add ids for tracking
terraform_hex = random_id.apply.hex
terraform_id = random_id.apply.id
terraform_time = time_static.first_created.id
created_by = local.created_by
cluster_name = local.cluster_name
})
Expand Down Expand Up @@ -142,7 +142,7 @@ output "biganimal" {
# spec project tags
tags = merge(local.tags, biganimal_spec.tags, {
# Biganimal reserves the Name tag
# Name = format("%s-%s-%s", name, local.cluster_name, random_id.apply.id)
# Name = format("%s-%s-%s", name, local.cluster_name, local.tags.terraform_id)
})
data_groups = {
for data_group_name, data_group_spec in biganimal_spec.data_groups : data_group_name => merge(data_group_spec, {
Expand Down Expand Up @@ -174,7 +174,7 @@ output "region_kubernetes" {
}

output "hex_id" {
value = random_id.apply.hex
value = local.tags.terraform_hex
}

output "pet_name" {
Expand Down
16 changes: 16 additions & 0 deletions edbterraform/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import sys
import shutil
import subprocess
import secrets
import base64
import datetime
from jinja2 import Environment, FileSystemLoader
import textwrap
from typing import List, Dict, Optional
Expand Down Expand Up @@ -569,6 +572,19 @@ def spec_compatability(infrastructure_variables, cloud_service_provider):
'cluster_name' in infrastructure_variables:
spec_variables['tags']['cluster_name'] = infrastructure_variables['cluster_name']

# Handle precomputed tags at generation time instead of during 'terraform apply'
# No longer handled within the terraform spec module and should be passed in as a project wide tag
# - terraform_hex = random_id.apply.hex | ex: "a24f8f4e"
# - terraform_id = random_id.apply.id | ex: "ok-PTg"
# - terraform_time = time_static.first_created.id | ex: "2024-11-26T01:36:28Z"
while True:
token = secrets.token_bytes(4)
spec_variables['tags']['terraform_hex'] = token.hex()
spec_variables['tags']['terraform_id'] = base64.b64encode(token).decode('utf-8').rstrip("=").replace('+','-').replace('/','-')
spec_variables['tags']['terraform_time'] = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
if spec_variables['tags']['terraform_id'][0].isalnum() and spec_variables['tags']['terraform_id'][-1].isalnum():
break

# if not provided,
# assign default output name for private/public ssh key filename
if 'ssh_key' not in spec_variables:
Expand Down

0 comments on commit 12a8d52

Please sign in to comment.