Terraform HTTP Provider
repository·main·Indexed 42 days ago
https://github.com/hashicorp/terraform-provider-httpA 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.
What's inside Terraform HTTP Provider
- 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.
Use the http data source to make HTTP requests
mainThe
httpdata 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 bothhttpandhttpsschemes.Important Security Note: While
httpsis 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
retryblock 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" } }Use the HTTP provider to interact with HTTP servers
mainThe 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.Use the HTTP provider with CDKTF in Python
mainThe 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.How the HTTP Provider works
mainThe 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
GETrequests are supported. - Success Criteria: Only HTTP
200status codes are considered successful operations. - Protocol Support:
http(plaintext) andhttps(secure) are supported.- For HTTPS, the provider uses
HTTP/1.1orHTTP/2.0depending on server support. - Non-standard protocols like
SPDYorQUICare 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.2andTLS/1.3, whileTLS/1.0andTLS/1.1are deprecated.
- Method Support: Only idempotent
Available HTTP Provider data outputs
mainWhen 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.
Configure retries for the http Data Source
mainBy default, the
httpdata source performs no retries. You can configure aretryblock to handle client errors (like connection errors) or 5xx-range status codes (except 501).Retries are implemented using go-retryablehttp.
Validate HTTP response with Preconditions on other resources
mainIf you need to validate the result of an
httpdata source before another resource is created, use apreconditionon 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")Simulate GitHub Actions locally with act
mainYou can use
actto locally reproduce the CI behavior of thepull_requestGitHub Action workflows.To list the workflows available for the
pull_requestaction:act -l pull_requestTo execute the workflows associated with the
pull_requestaction:act pull_request# List workflows act -l pull_request # Execute workflows act pull_requestUse a local development build of the provider
mainTo use your local changes in a Terraform configuration, follow these steps:
- Install the local build to your
${GOBIN}(defaults to${GOPATH}/binor${HOME}/go/bin) usingmake install. Repeat this step whenever you modify the provider code. - Configure Terraform CLI development overrides to point to your local build.
make install- Install the local build to your
Validate HTTP response with Postconditions
mainYou can use Terraform
lifecyclepostconditionblocks to ensure thehttpdata 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" } } }Configure the HTTP Provider
mainThe HTTP provider requires no explicit configuration block. You can use the resources it provides directly in your Terraform configuration without setting up provider-level arguments.