From 025d3b4beca9835a5e997e41677a4c6441a733f4 Mon Sep 17 00:00:00 2001 From: Luigi Di Fraia <93160889+luigidifraiawork@users.noreply.github.com> Date: Wed, 28 Jun 2023 20:59:03 +0100 Subject: [PATCH] feat: Add input variable 'instance_tags' (#343) --- README.md | 1 + examples/complete/main.tf | 9 +++------ main.tf | 6 +++--- variables.tf | 6 ++++++ wrappers/main.tf | 1 + 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 62540a88..c66eef7a 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,7 @@ No modules. | [iam\_role\_use\_name\_prefix](#input\_iam\_role\_use\_name\_prefix) | Determines whether the IAM role name (`iam_role_name` or `name`) is used as a prefix | `bool` | `true` | no | | [ignore\_ami\_changes](#input\_ignore\_ami\_changes) | Whether changes to the AMI ID changes should be ignored by Terraform. Note - changing this value will result in the replacement of the instance | `bool` | `false` | no | | [instance\_initiated\_shutdown\_behavior](#input\_instance\_initiated\_shutdown\_behavior) | Shutdown behavior for the instance. Amazon defaults this to stop for EBS-backed instances and terminate for instance-store instances. Cannot be set on instance-store instance | `string` | `null` | no | +| [instance\_tags](#input\_instance\_tags) | Additional tags for the instance | `map(string)` | `{}` | no | | [instance\_type](#input\_instance\_type) | The type of instance to start | `string` | `"t3.micro"` | no | | [ipv6\_address\_count](#input\_ipv6\_address\_count) | A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet | `number` | `null` | no | | [ipv6\_addresses](#input\_ipv6\_addresses) | Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface | `list(string)` | `null` | no | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 0d0120ab..25baae5e 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -414,12 +414,9 @@ module "ec2_cpu_options" { } ] - tags = merge( - local.tags, - { - Name = "${local.name}-cpu-options" - } - ) + instance_tags = { Persistence = "09:00-18:00" } + + tags = local.tags } ################################################################################ diff --git a/main.tf b/main.tf index 90669c21..b7548c34 100644 --- a/main.tf +++ b/main.tf @@ -176,7 +176,7 @@ resource "aws_instance" "this" { delete = try(var.timeouts.delete, null) } - tags = merge({ "Name" = var.name }, var.tags) + tags = merge({ "Name" = var.name }, var.instance_tags, var.tags) volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null } @@ -344,7 +344,7 @@ resource "aws_instance" "ignore_ami" { delete = try(var.timeouts.delete, null) } - tags = merge({ "Name" = var.name }, var.tags) + tags = merge({ "Name" = var.name }, var.instance_tags, var.tags) volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null lifecycle { @@ -518,7 +518,7 @@ resource "aws_spot_instance_request" "this" { delete = try(var.timeouts.delete, null) } - tags = merge({ "Name" = var.name }, var.tags) + tags = merge({ "Name" = var.name }, var.instance_tags, var.tags) volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null } diff --git a/variables.tf b/variables.tf index 2951625e..a05e3043 100644 --- a/variables.tf +++ b/variables.tf @@ -124,6 +124,12 @@ variable "instance_type" { default = "t3.micro" } +variable "instance_tags" { + description = "Additional tags for the instance" + type = map(string) + default = {} +} + variable "ipv6_address_count" { description = "A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet" type = number diff --git a/wrappers/main.tf b/wrappers/main.tf index e8d0f591..c6639eed 100644 --- a/wrappers/main.tf +++ b/wrappers/main.tf @@ -24,6 +24,7 @@ module "wrapper" { iam_instance_profile = try(each.value.iam_instance_profile, var.defaults.iam_instance_profile, null) instance_initiated_shutdown_behavior = try(each.value.instance_initiated_shutdown_behavior, var.defaults.instance_initiated_shutdown_behavior, null) instance_type = try(each.value.instance_type, var.defaults.instance_type, "t3.micro") + instance_tags = try(each.value.instance_tags, var.defaults.instance_tags, {}) ipv6_address_count = try(each.value.ipv6_address_count, var.defaults.ipv6_address_count, null) ipv6_addresses = try(each.value.ipv6_addresses, var.defaults.ipv6_addresses, null) key_name = try(each.value.key_name, var.defaults.key_name, null)