Skip to content

Commit 501c48a

Browse files
committed
fix: Allow computed values for name, make timeouts dynamic/optional
1 parent be4e9e3 commit 501c48a

File tree

7 files changed

+63
-16
lines changed

7 files changed

+63
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ No modules.
275275
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | The VPC Subnet ID to launch in | `string` | `null` | no |
276276
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
277277
| <a name="input_tenancy"></a> [tenancy](#input\_tenancy) | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host | `string` | `null` | no |
278-
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | Define maximum timeout for creating, updating, and deleting EC2 instance resources | `map(string)` | `{}` | no |
278+
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | Define maximum timeout for creating, updating, and deleting EC2 instance resources | <pre>object({<br/> create = optional(string)<br/> update = optional(string)<br/> delete = optional(string)<br/> })</pre> | `null` | no |
279279
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user\_data\_base64 instead | `string` | `null` | no |
280280
| <a name="input_user_data_base64"></a> [user\_data\_base64](#input\_user\_data\_base64) | Can be used instead of user\_data to pass base64-encoded binary data directly. Use this instead of user\_data whenever the value is not a valid UTF-8 string. For example, gzip-encoded user data must be base64-encoded and passed via this argument to avoid corruption | `string` | `null` | no |
281281
| <a name="input_user_data_replace_on_change"></a> [user\_data\_replace\_on\_change](#input\_user\_data\_replace\_on\_change) | When used in combination with user\_data or user\_data\_base64 will trigger a destroy and recreate when set to true. Defaults to false if not set | `bool` | `null` | no |

examples/complete/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ Note that this example may create resources which can cost money. Run `terraform
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.7 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 6.0 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.0 |
2425

2526
## Providers
2627

2728
| Name | Version |
2829
|------|---------|
2930
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 6.0 |
31+
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.0 |
3032

3133
## Modules
3234

3335
| Name | Source | Version |
3436
|------|--------|---------|
3537
| <a name="module_ec2_complete"></a> [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a |
38+
| <a name="module_ec2_computed_name"></a> [ec2\_computed\_name](#module\_ec2\_computed\_name) | ../../ | n/a |
3639
| <a name="module_ec2_disabled"></a> [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a |
3740
| <a name="module_ec2_ignore_ami_changes"></a> [ec2\_ignore\_ami\_changes](#module\_ec2\_ignore\_ami\_changes) | ../../ | n/a |
3841
| <a name="module_ec2_metadata_options"></a> [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a |
@@ -55,6 +58,7 @@ Note that this example may create resources which can cost money. Run `terraform
5558
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
5659
| [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |
5760
| [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource |
61+
| [random_string.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
5862
| [aws_ami.amazon_linux](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
5963
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
6064

examples/complete/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ module "ec2_t3_unlimited" {
146146
tags = local.tags
147147
}
148148

149+
# https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/issues/456
150+
module "ec2_computed_name" {
151+
source = "../../"
152+
153+
name = join("-", [local.name, random_string.suffix.result])
154+
155+
ignore_ami_changes = true
156+
ami = data.aws_ami.amazon_linux.id
157+
158+
instance_type = "t3.micro"
159+
availability_zone = element(module.vpc.azs, 0)
160+
subnet_id = element(module.vpc.private_subnets, 0)
161+
162+
tags = local.tags
163+
}
164+
149165
module "ec2_disabled" {
150166
source = "../../"
151167

@@ -383,3 +399,10 @@ resource "aws_network_interface" "this" {
383399
subnet_id = element(module.vpc.private_subnets, 0)
384400
security_groups = [module.security_group.security_group_id]
385401
}
402+
403+
resource "random_string" "suffix" {
404+
length = 2
405+
numeric = false
406+
upper = false
407+
special = false
408+
}

examples/complete/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 6.0"
88
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 3.0"
12+
}
913
}
1014
}

main.tf

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ resource "aws_instance" "this" {
219219
volume_tags = var.enable_volume_tags ? merge(var.tags, var.volume_tags, { "Name" = var.name }) : null
220220
vpc_security_group_ids = var.network_interface == null ? local.vpc_security_group_ids : null
221221

222-
timeouts {
223-
create = try(var.timeouts.create, null)
224-
update = try(var.timeouts.update, null)
225-
delete = try(var.timeouts.delete, null)
222+
dynamic "timeouts" {
223+
for_each = var.timeouts != null ? [var.timeouts] : []
224+
225+
content {
226+
create = timeouts.value.create
227+
update = timeouts.value.update
228+
delete = timeouts.value.delete
229+
}
226230
}
227231
}
228232

@@ -409,10 +413,14 @@ resource "aws_instance" "ignore_ami" {
409413
volume_tags = var.enable_volume_tags ? merge(var.tags, var.volume_tags, { "Name" = var.name }) : null
410414
vpc_security_group_ids = var.network_interface == null ? local.vpc_security_group_ids : null
411415

412-
timeouts {
413-
create = try(var.timeouts.create, null)
414-
update = try(var.timeouts.update, null)
415-
delete = try(var.timeouts.delete, null)
416+
dynamic "timeouts" {
417+
for_each = var.timeouts != null ? [var.timeouts] : []
418+
419+
content {
420+
create = timeouts.value.create
421+
update = timeouts.value.update
422+
delete = timeouts.value.delete
423+
}
416424
}
417425

418426
lifecycle {
@@ -596,9 +604,13 @@ resource "aws_spot_instance_request" "this" {
596604
volume_tags = var.enable_volume_tags ? merge(var.tags, var.volume_tags, { "Name" = var.name }) : null
597605
vpc_security_group_ids = var.network_interface == null ? local.vpc_security_group_ids : null
598606

599-
timeouts {
600-
create = try(var.timeouts.create, null)
601-
delete = try(var.timeouts.delete, null)
607+
dynamic "timeouts" {
608+
for_each = var.timeouts != null ? [var.timeouts] : []
609+
610+
content {
611+
create = timeouts.value.create
612+
delete = timeouts.value.delete
613+
}
602614
}
603615

604616
lifecycle {
@@ -609,7 +621,7 @@ resource "aws_spot_instance_request" "this" {
609621
}
610622

611623
resource "aws_ec2_tag" "spot_instance" {
612-
for_each = { for k, v in local.instance_tags : k => v if local.create && var.create_spot_instance }
624+
for_each = local.create && var.create_spot_instance ? local.instance_tags : {}
613625

614626
resource_id = aws_spot_instance_request.this[0].spot_instance_id
615627
key = each.key

variables.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,12 @@ variable "vpc_security_group_ids" {
360360

361361
variable "timeouts" {
362362
description = "Define maximum timeout for creating, updating, and deleting EC2 instance resources"
363-
type = map(string)
364-
default = {}
363+
type = object({
364+
create = optional(string)
365+
update = optional(string)
366+
delete = optional(string)
367+
})
368+
default = null
365369
}
366370

367371
################################################################################

wrappers/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module "wrapper" {
9292
subnet_id = try(each.value.subnet_id, var.defaults.subnet_id, null)
9393
tags = try(each.value.tags, var.defaults.tags, {})
9494
tenancy = try(each.value.tenancy, var.defaults.tenancy, null)
95-
timeouts = try(each.value.timeouts, var.defaults.timeouts, {})
95+
timeouts = try(each.value.timeouts, var.defaults.timeouts, null)
9696
user_data = try(each.value.user_data, var.defaults.user_data, null)
9797
user_data_base64 = try(each.value.user_data_base64, var.defaults.user_data_base64, null)
9898
user_data_replace_on_change = try(each.value.user_data_replace_on_change, var.defaults.user_data_replace_on_change, null)

0 commit comments

Comments
 (0)