From e33c8e40ab29dd0d01e2dd06d040cb5b0e1e0307 Mon Sep 17 00:00:00 2001 From: FrankApiyo Date: Wed, 11 Dec 2024 13:20:33 +0300 Subject: [PATCH 1/2] Add volume type var --- compute.tf | 2 +- variables.tf | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/compute.tf b/compute.tf index c1ed99b..8dc30d0 100644 --- a/compute.tf +++ b/compute.tf @@ -14,7 +14,7 @@ resource "aws_launch_configuration" "main" { iam_instance_profile = aws_iam_instance_profile.ec2.name root_block_device { - volume_type = "gp2" + volume_type = var.storage_class volume_size = var.attached_volume_size delete_on_termination = true } diff --git a/variables.tf b/variables.tf index 622f636..09ead28 100644 --- a/variables.tf +++ b/variables.tf @@ -1,5 +1,9 @@ variable "deployment" {} variable "env" {} +variable "storage_class" { + description = "storage class" + default = "gp2" +} variable "project" {} variable "project_id" {} variable "deployment_type" { From a7fb079d8334fe801ea96f1ebf62a5ee3d13dd93 Mon Sep 17 00:00:00 2001 From: FrankApiyo Date: Wed, 11 Dec 2024 17:10:33 +0300 Subject: [PATCH 2/2] Use terraform locals to set tags for ASG https://developer.hashicorp.com/terraform/language/values/locals --- compute.tf | 105 +++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/compute.tf b/compute.tf index 8dc30d0..f11b27b 100644 --- a/compute.tf +++ b/compute.tf @@ -26,6 +26,44 @@ resource "aws_launch_configuration" "main" { } } +locals { common_tags = [ + { + key = "OwnerList" + value = var.owner + propagate_at_launch = true + }, + { + key = "EndDate" + value = var.end_date + propagate_at_launch = true + }, + { + key = "EnvironmentList" + value = var.env + propagate_at_launch = true + }, + { + key = "ProjectList" + value = var.project + propagate_at_launch = true + }, + { + key = "DeploymentType" + value = var.deployment_type + propagate_at_launch = true + }, + { + key = "App" + value = var.deployed_app + propagate_at_launch = true + }, + { + key = "SoftwareVersion" + value = var.software_version + propagate_at_launch = true + }, +] } + resource "aws_autoscaling_group" "main" { count = var.deployed ? 1 : 0 name = "asg-${var.project_id}-${var.env}-${var.deployed_app}-${var.deployment}" @@ -39,53 +77,26 @@ resource "aws_autoscaling_group" "main" { create_before_destroy = true } - tags = [ - { - key = "Name" - value = "${var.deployed_app}-${var.project}-${var.env}-${var.deployment}-${count.index}" - propagate_at_launch = true - }, - { - key = "Group" - value = "${var.deployed_app}-${var.project}-${var.env}-${var.deployment}" - propagate_at_launch = true - }, - { - key = "OwnerList" - value = var.owner - propagate_at_launch = true - }, - { - key = "EndDate" - value = var.end_date - propagate_at_launch = true - }, - { - key = "EnvironmentList" - value = var.env - propagate_at_launch = true - }, - { - key = "ProjectList" - value = var.project - propagate_at_launch = true - }, - { - key = "DeploymentType" - value = var.deployment_type - propagate_at_launch = true - }, - { - key = "App" - value = var.deployed_app - propagate_at_launch = true - }, - { - key = "SoftwareVersion" - value = var.software_version - propagate_at_launch = true - }, - ] + tag { + key = "Name" + value = "${var.deployed_app}-${var.project}-${var.env}-${var.deployment}-${count.index}" + propagate_at_launch = true + } + tag { + key = "Group" + value = "${var.deployed_app}-${var.project}-${var.env}-${var.deployment}" + propagate_at_launch = true + } + + dynamic "tag" { + for_each = local.common_tags + content { + key = tag.value.key + propagate_at_launch = tag.value.propagate_at_launch + value = tag.value.value + } + } + } resource "aws_cloudwatch_metric_alarm" "cpu_utilization" {