http (Data Source)

The http data source makes an HTTP GET request to the given URL and exports information about the response.

The given URL may be either an http or https URL. This resource will issue a warning if the result is not UTF-8 encoded.

By default, there are no retries. Configuring the retry block will result in retries if an error is returned by the client (e.g., connection errors) or if a 5xx-range (except 501) status code is received. For further details see go-retryablehttp.

Example Usage

# The following example shows how to issue an HTTP GET request supplying
# an optional request header.
data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

# The following example shows how to issue an HTTP HEAD request.
data "http" "example_head" {
  url    = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
  method = "HEAD"
}

# The following example shows how to issue an HTTP POST request
# supplying an optional request body.
data "http" "example_post" {
  url    = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
  method = "POST"

  # Optional request body
  request_body = "request body"
}

Usage with Postcondition

Precondition and Postcondition checks are available with Terraform v1.2.0 and later.

data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }

  lifecycle {
    postcondition {
      condition     = contains([201, 204], self.status_code)
      error_message = "Status code invalid"
    }
  }
}

Usage with Precondition

Precondition and Postcondition checks are available with Terraform v1.2.0 and later.

data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

resource "random_uuid" "example" {
  lifecycle {
    precondition {
      condition     = contains([201, 204], data.http.example.status_code)
      error_message = "Status code invalid"
    }
  }
}

Usage with Provisioner

Failure Behaviour can be leveraged within a provisioner in order to raise an error and stop applying.

data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

resource "null_resource" "example" {
  # On success, this will attempt to execute the true command in the
  # shell environment running terraform.
  # On failure, this will attempt to execute the false command in the
  # shell environment running terraform.
  provisioner "local-exec" {
    command = contains([201, 204], data.http.example.status_code)
  }
}

Schema

Required

Optional

Read-Only

Nested Schema for retry

Optional: