Terraform HTTP Provider

repository·main·Indexed 42 days ago

https://github.com/hashicorp/terraform-provider-http

A utility provider that allows users to make HTTP requests (GET, HEAD, and POST) as part of their Terraform infrastructure code. It provides a data source that exposes response headers, status codes, and bodies for use in other resources. Supports http and https schemes, custom request headers, and configurable retry logic via the retry block.

Tokens
5.4K
Snippets
16
Records
28
Agent score
53%

What's inside Terraform HTTP Provider

  1. Overview of the HTTP Provider

    main
    The HTTP provider is a utility provider designed to interact with generic HTTP servers as part of a Terraform configuration. It allows you to perform HTTP requests (such as GET, POST, etc.) to manage or retrieve information from web services during the Terraform lifecycle.
  2. Use the http data source to make HTTP requests

    main

    The http data source allows you to perform HTTP requests (GET, HEAD, or POST) to a specified URL and export the response information into your Terraform configuration. It supports both http and https schemes.

    Important Security Note: While https is supported, there is currently no mechanism to authenticate the remote server beyond general verification of the server certificate's chain of trust. Data from untrusted servers should be treated as untrustworthy.

    Retries: By default, no retries are performed. You can configure a retry block to handle client errors (like connection issues) or 5xx-range status codes (excluding 501).

    data "http" "example" {
      url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
    
      # Optional request headers
      request_headers = {
        Accept = "application/json"
      }
    }
  3. Use the HTTP provider to interact with HTTP servers

    main
    The HTTP provider allows you to interact with generic HTTP servers within a Terraform deployment. It provides a data source that issues an HTTP request and exposes the resulting response headers and body for use in other parts of your Terraform configuration.
  4. Use the HTTP provider with CDKTF in Python

    main
    The HTTP provider is a utility provider used to interact with generic HTTP servers within a Terraform configuration. When using the Cloud Development Kit for Terraform (CDKTF) with Python, you can leverage this provider to perform HTTP requests as part of your infrastructure orchestration. Note that the provider requires no explicit configuration to function.
  5. How the HTTP Provider works

    main

    The HTTP Provider is designed to interact with generic HTTP servers during a Terraform run. It functions primarily as a managed data source that issues an HTTP GET request to a defined URL every time Terraform is executed.

    Key operational constraints include:

    • Method Support: Only idempotent GET requests are supported.
    • Success Criteria: Only HTTP 200 status codes are considered successful operations.
    • Protocol Support:
      • http (plaintext) and https (secure) are supported.
      • For HTTPS, the provider uses HTTP/1.1 or HTTP/2.0 depending on server support.
      • Non-standard protocols like SPDY or QUIC are not supported.
    • TLS Versions: TLS support depends on the Go version used to build the provider. Current versions (built with Go 1.22) support TLS/1.2 and TLS/1.3, while TLS/1.0 and TLS/1.1 are deprecated.
  6. Available HTTP Provider data outputs

    main

    When using the HTTP provider to perform a GET request, the following information from the server response is exposed for use in your Terraform configuration:

    • Response Headers: The headers returned by the server in the response.
    • Response Body: The body of the response, exposed as a string where applicable.
    • Request Headers: You can also supply custom request headers to the outgoing GET request.
  7. Validate HTTP response with Preconditions on other resources

    main

    If you need to validate the result of an http data source before another resource is created, use a precondition on that target resource. This allows you to fail the plan/apply if the HTTP request did not return the expected status code.

    from cdktf import TerraformResourceLifecycle, Precondition, Fn, TerraformStack
    from imports.http. import DataHttp
    from imports.random.uuid import Uuid
    
    class MyConvertedCode(TerraformStack):
        def __init__(self, scope, name):
            super().__init__(scope, name)
            
            data_http_example = DataHttp(self, "example",
                request_headers=[{"Accept": "application/json"}],
                url="https://checkpoint-api.hashicorp.com/v1/check/terraform"
            )
            
            random_uuid_example = Uuid(self, "example_1",
                lifecycle=TerraformResourceLifecycle(
                    precondition=[
                        Precondition(
                            condition=Fn.contains([201, 204], data_http_example.status_code),
                            error_message="Status code invalid"
                        )
                    ]
                )
            )
            random_uuid_example.override_logical_id("example")
  8. Simulate GitHub Actions locally with act

    main

    You can use act to locally reproduce the CI behavior of the pull_request GitHub Action workflows.

    To list the workflows available for the pull_request action:

    act -l pull_request

    To execute the workflows associated with the pull_request action:

    act pull_request
    # List workflows
    act -l pull_request
    
    # Execute workflows
    act pull_request
  9. Use a local development build of the provider

    main

    To use your local changes in a Terraform configuration, follow these steps:

    1. Install the local build to your ${GOBIN} (defaults to ${GOPATH}/bin or ${HOME}/go/bin) using make install. Repeat this step whenever you modify the provider code.
    2. Configure Terraform CLI development overrides to point to your local build.
    make install
  10. Validate HTTP response with Postconditions

    main

    You can use Terraform lifecycle postcondition blocks to ensure the http data source returns an expected status code. If the condition is not met, Terraform will raise an error during the plan or apply phase.

    data "http" "example" {
      url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
    
      request_headers = {
        Accept = "application/json"
      }
    
      lifecycle {
        postcondition {
          condition     = contains([201, 204], self.status_code)
          error_message = "Status code invalid"
        }
      }
    }