Complete the code to add a precondition that checks if the variable 'instance_count' is greater than zero.
variable "instance_count" { type = number [1] = [ { condition = var.instance_count > 0 error_message = "Instance count must be greater than zero." } ] }
The validation block is used in Terraform variables to define preconditions that must be met before applying the configuration.
Complete the resource block to add a precondition that ensures the 'ami' attribute is not empty.
resource "aws_instance" "example" { ami = var.ami instance_type = "t2.micro" [1] { condition = length(self.ami) > 0 error_message = "AMI must not be empty." } }
The precondition block inside a resource checks conditions before creating or updating the resource.
Fix the error in the postcondition block that checks if the instance state is 'running'.
resource "aws_instance" "example" { ami = var.ami instance_type = "t2.micro" lifecycle { [1] { condition = self.instance_state.name == "running" error_message = "Instance must be running after creation." } } }
The postcondition block inside the lifecycle block checks conditions after resource creation or update.
Fill both blanks to add a precondition and a postcondition to a resource that checks 'instance_type' before creation and 'state' after creation.
resource "aws_instance" "example" { ami = var.ami instance_type = var.instance_type [1] { condition = self.instance_type == "t2.micro" error_message = "Instance type must be t2.micro." } lifecycle { [2] { condition = self.instance_state.name == "running" error_message = "Instance must be running after creation." } } }
The precondition block checks conditions before resource creation, and the postcondition block inside lifecycle checks after creation.
Fill all three blanks to add a variable with validation, a resource with precondition, and a lifecycle postcondition.
variable "region" { type = string [1] = [ { condition = var.region != "" error_message = "Region cannot be empty." } ] } resource "aws_instance" "example" { ami = var.ami instance_type = "t2.micro" [2] { condition = self.instance_type == "t2.micro" error_message = "Instance type must be t2.micro." } lifecycle { [3] { condition = self.instance_state.name == "running" error_message = "Instance must be running after creation." } } }
Variables use validation blocks for input checks. Resources use precondition blocks before creation and postcondition blocks inside lifecycle after creation.