Use REST API with Terraform
Emmanuel Gautier / July 16, 2024
2 min read
Sometimes you need to interact with a REST API from Terraform. This can be done using the http
provider, but, let's be honest, it can be a bit cumbersome.
Hopefully, there are also some providers that make it easier to interact with REST APIs, such as the Mastercard REST API provider.
Why use those providers?
If there is an official provider for the solution you want to interact with, it's usually better to use it. Providers are easier to use and provide better integration with Terraform. They also provide better error handling and are more user-friendly.
But it exists some cases where a generic provider like http
can be useful:
- When there is no provider available for the REST API you want to interact with.
- When the provider is not maintained or doesn't provide the features you need.
When to use the http
provider
If you need to interact with a very simple REST API, you can use the http
provider. This provider allows you to make HTTP requests to any REST API.
Here's an example of how you can use the http
provider to interact with a REST API:
data "http" "example" {
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
# Optional request headers
request_headers = {
Accept = "application/json"
}
}
When to use a REST API provider
REST API provider developed by Mastercard is a good choice when you need to interact with a more complex REST API. This provider is designed to manage global behavior, such as action methods, authentication, resource id management, and error handling.
Here's an example of how you can use the Mastercard REST API provider to interact with a REST API:
provider "restapi" {
endpoint = "https://api.example.com/v1"
oauth_client_credentials {
oauth_token_endpoint = "https://api.example.com/oauth/token"
oauth_client_id = "your-client-id"
oauth_client_secret = "your-client-secret"
oauth_scopes = ["scope1", "scope2"]
}
}
resource "restapi_resource" "example" {
path = "/example"
}
Consulting
If you're seeking solutions to a problem or need expert advice, I'm here to help! Don't hesitate to book a call with me for a consulting session. Let's discuss your situation and find the best solution together.
Featured Posts
Introducing new blog about OAuth, OpenID Connect, and IAM Solutions
I'm excited to announce the launch of a new blog named CerberAuth, where I'll be exploring the world of OAuth, OpenID Connect, and IAM solutions for modern security.
How to deal with Docker Hub rate limit on AWS
Since 2020, DockerHub has been limited to only 200 container image pull requests per six hours. This article will help you to deal with this limitation on AWS.
How to enable Python type checking in VSCode
Python now has support for type hints. In this article, we will see how to enable better IntelliSense and type checking analysis in VSCode.