From ac3fe5eeac176c63e55d0dd7c1597911236ce490 Mon Sep 17 00:00:00 2001 From: Herman Wong Date: Tue, 28 May 2024 09:22:56 -0700 Subject: [PATCH 1/4] Add precondition. --- ec2.tf | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ec2.tf b/ec2.tf index 8d83188..f372c7a 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,10 +1,14 @@ +data "aws_ec2_instance_type" "this" { + instance_type = var.ec2_instance_type +} + resource "aws_instance" "this" { ### BASICS ### - ami = var.ami - instance_type = var.ec2_instance_type - count = var.instance_count - key_name = var.ec2_key_pair - monitoring = true + ami = var.ami + instance_type = var.ec2_instance_type + count = var.instance_count + key_name = var.ec2_key_pair + monitoring = true user_data = var.user_data user_data_base64 = var.user_data_base64 user_data_replace_on_change = var.user_data_replace_on_change @@ -51,5 +55,13 @@ resource "aws_instance" "this" { lifecycle { ignore_changes = [root_block_device, ebs_block_device, user_data, ami] + precondition { + condition = (data.aws_ec2_instance_type.this.ebs_optimized_support == "unsupported" && var.ebs_optimized == false) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "supported" && var.ebs_optimized == true) + error_message = <<-EOT + The instance type (${var.ec2_instance_type}) has EBS Optimized value of (${data.aws_ec2_instance_type.this.ebs_optimized_support}), + but variable ebs_optimized is set to (${var.ebs_optimized}) (default is 'true'). + Please ensure the ebs_optimized variable matches the EBS Optimized support of the instance type. + EOT + } } } From 82376f7f16bb0700de07d387cec4b6ed7c70312a Mon Sep 17 00:00:00 2001 From: Herman Wong Date: Tue, 28 May 2024 10:16:05 -0700 Subject: [PATCH 2/4] Fix possible value. --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index f372c7a..2cc59a1 100644 --- a/ec2.tf +++ b/ec2.tf @@ -56,7 +56,7 @@ resource "aws_instance" "this" { lifecycle { ignore_changes = [root_block_device, ebs_block_device, user_data, ami] precondition { - condition = (data.aws_ec2_instance_type.this.ebs_optimized_support == "unsupported" && var.ebs_optimized == false) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "supported" && var.ebs_optimized == true) + condition = (data.aws_ec2_instance_type.this.ebs_optimized_support == "unsupported" && var.ebs_optimized == false) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "supported" && var.ebs_optimized == true) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "default" && var.ebs_optimized == true) error_message = <<-EOT The instance type (${var.ec2_instance_type}) has EBS Optimized value of (${data.aws_ec2_instance_type.this.ebs_optimized_support}), but variable ebs_optimized is set to (${var.ebs_optimized}) (default is 'true'). From d6f3481d3faf332e045405a4984e14eda189ac14 Mon Sep 17 00:00:00 2001 From: Herman Wong Date: Tue, 28 May 2024 10:22:32 -0700 Subject: [PATCH 3/4] Fix verbiage. --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 2cc59a1..6f1db75 100644 --- a/ec2.tf +++ b/ec2.tf @@ -58,7 +58,7 @@ resource "aws_instance" "this" { precondition { condition = (data.aws_ec2_instance_type.this.ebs_optimized_support == "unsupported" && var.ebs_optimized == false) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "supported" && var.ebs_optimized == true) || (data.aws_ec2_instance_type.this.ebs_optimized_support == "default" && var.ebs_optimized == true) error_message = <<-EOT - The instance type (${var.ec2_instance_type}) has EBS Optimized value of (${data.aws_ec2_instance_type.this.ebs_optimized_support}), + The instance type (${var.ec2_instance_type}) has an EBS Optimized value of (${data.aws_ec2_instance_type.this.ebs_optimized_support}), but variable ebs_optimized is set to (${var.ebs_optimized}) (default is 'true'). Please ensure the ebs_optimized variable matches the EBS Optimized support of the instance type. EOT From df2ff618c149073e9d04e88f153014c998ff1138 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 28 May 2024 17:31:41 +0000 Subject: [PATCH 4/4] terraform-docs: automated action --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 82bcf68..a81dd2e 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,7 @@ module "ad2" { | [aws_network_interface_attachment.eni_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface_attachment) | resource | | [aws_network_interface_sg_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface_sg_attachment) | resource | | [aws_volume_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/volume_attachment) | resource | +| [aws_ec2_instance_type.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type) | data source | | [aws_iam_policy.AmazonSSMManagedInstanceCore](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy) | data source | ## Inputs