NashTech Blog

How to customize Terraform error message?

Table of Contents
close up photo of programming of codes

In this blog we will see how to customize error message in terraform which is quite easy. This IAC tool provide the functionality of creating your own custom message in case of any failure while using configuration apart from using its default error messages.

Use of on_failure in terraform

In Terraform, the on_failure block is used to define actions or behaviors to be taken when a resource or data source encounters an error during its creation or retrieval. This block allows you to specify how you should handle failures for a particular resource or data source. Hence giving you more control over the behavior of your infrastructure provisioning process.

The on_failure block is typically associated with resource or data source blocks and can contain the following actions:

  1. continue (default): This is the default behavior. When an error occurs during resource creation or data source retrieval, Terraform logs the error and continues processing other resources and data sources in your configuration.
  2. fail: If you specify on_failure = "fail", Terraform will stop processing the configuration and immediately return an error when an error occurs for the associated resource or data source. This is useful when you want to prevent further processing in case of a failure. It also helps you to have a more predictable failure behavior.
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

// your configurations

  on_failure = "fail"  # Stop processing if this resource fails to create
}

Using fail() with on_failure in terraform

In Terraform, the fail function is used to deliberately cause a configuration to fail during validation or planning. It is a way to introduce controlled errors into your configuration to prevent it from being applied in certain circumstances. The fail function is often used within conditional expressions to enforce specific conditions that must be met for the configuration to proceed.

Here’s the syntax for using the fail function in Terraform:

fail(message)
// message is a string that specifies the error message to display when the fail function is called.

Here is an example how to use it :

locals {
 instances_count = 3
}

resource "aws_instance" "example" {
  count = local.instances_count

  ami           = "ami-0c66b159cbgafe20"
  instance_type = "t2.micro"
  # Instance configuration...

  lifecycle {
    prevent_destroy = count.index == 0 ? true : false
  }

  provisioner "local-exec" {
    when = "destroy"

    command = "echo 'Instance IP: ${self.public_ip}' >> instance_ips.txt"
    on_failure = fail("Failed to execute local-exec provisioner for instance ${self.id}")
  }
}

So the error message will be displayed on in screen in case of any failure will be "Failed to execute local-exec provisioner for instance <id of instance>"

That is all for this blog . For more such blogs you can refer to nashtech blogs . Here is the Terraform official documentation link

Picture of Saumya

Saumya

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top