From 61d592fa637fed59fa980a7fcea5880a7b18cab5 Mon Sep 17 00:00:00 2001 From: Mario Sergio Date: Thu, 7 Nov 2024 11:59:20 -0300 Subject: [PATCH] feat(instances): use terraform workspaces --- instances/main.tf | 40 ++++++++++++++++++++++++-------- instances/modules/common/main.tf | 1 + instances/variables.tf | 30 ------------------------ 3 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 instances/variables.tf diff --git a/instances/main.tf b/instances/main.tf index 18b6a79..0c294fd 100644 --- a/instances/main.tf +++ b/instances/main.tf @@ -24,23 +24,43 @@ provider "aws" { region = "us-east-1" } +locals { + # Tipo de imagem para o servidor legado (APIS e Clientes Bonde) + ami = "ami-0866a3c8686eaeeba" + + # Nome da chave SSH + key_name = "custom-host" + + # Caminho para a chave privada SSH + private_key_path = "~/.ssh/ailton-krenak.pem" + + # Ambiente (dev, staging, production) + env = terraform.workspace + + # Tipo de instância para o servidor legado (APIS e Clientes Bonde) + legacy_server_instance_type = terraform.workspace == "stage" ? "t3.small" : "t3.micro" + + # Tipo de instância para o servidor de sites (Bonde Público e CMS) + sites_server_instance_type = terraform.workspace == "stage" ? "t3.micro" : "t2.micro" +} + # Módulo para o servidor web module "legacy_server" { source = "./modules/common" - ami = var.ami - instance_type = var.legacy_server_instance_type - instance_name = "legacy-server-${var.env}" - key_name = var.key_name - private_key_path = var.private_key_path + ami = local.ami + instance_type = local.legacy_server_instance_type + instance_name = "legacy-server-${local.env}" + key_name = local.key_name + private_key_path = local.private_key_path monitoring_files_path = "./monitoring" } module "sites_server" { source = "./modules/common" - ami = var.ami - instance_type = var.sites_server_instance_type - instance_name = "sites-server-${var.env}" - key_name = var.key_name - private_key_path = var.private_key_path + ami = local.ami + instance_type = local.sites_server_instance_type + instance_name = "sites-server-${local.env}" + key_name = local.key_name + private_key_path = local.private_key_path monitoring_files_path = "./monitoring" } \ No newline at end of file diff --git a/instances/modules/common/main.tf b/instances/modules/common/main.tf index 3e5de6b..eaee011 100644 --- a/instances/modules/common/main.tf +++ b/instances/modules/common/main.tf @@ -6,6 +6,7 @@ resource "aws_instance" "server" { ami = var.ami instance_type = var.instance_type + # Nome da chave existente na Amazon, você deve ter o par da chave na sua máquina key_name = var.key_name tags = { diff --git a/instances/variables.tf b/instances/variables.tf deleted file mode 100644 index 95334db..0000000 --- a/instances/variables.tf +++ /dev/null @@ -1,30 +0,0 @@ -variable ami { - description = "Tipo de imagem para o servidor legado (APIS e Clientes Bonde)" - type = string - default = "ami-0866a3c8686eaeeba" # Ubuntu -} - -variable "legacy_server_instance_type" { - description = "Tipo de instância para o servidor legado (APIS e Clientes Bonde)" - type = string -} - -variable "sites_server_instance_type" { - description = "Tipo de instância para o servidor de sites (Bonde Público e CMS)" - type = string -} - -variable "env" { - description = "Ambiente (dev, staging, production)" - type = string -} - -variable "key_name" { - description = "Nome da chave SSH" - type = string -} - -variable "private_key_path" { - description = "Caminho para a chave privada SSH" - type = string -} \ No newline at end of file