SUSE Support

Here When You Need Us

How to make a simple terraform API request

This document (000020792) is provided subject to the disclaimer at the end of this document.

Environment

Latest version of Rancher 2.6.X
Rancher Terraform Provider 1.24.1+
Latest Version of Terraform 1.3.1+
User with a token created via the API and proper permissions to the local Rancher cluster.

Local directory for terraform plan files (name.tf) and a local terraform.tfstate file.


Situation

Sometimes it is necessary to create a basic skeleton for beginning a task, like using the Rancher2 Terraform Provider to speak with the Rancher API.
This represents a starting point to choose a simple read-only task like "query the cluster information for the local Rancher cluster".

Resolution

Terraform commands are very easy, below are the main options one may typically use.
  • terraform init -- download needed files like the rancher2 terraform provider
  • terraform plan -- compare the environment to the state file, plan is like a diff of any changes to be made
  • terraform apply -- apply the planned changes
  • terraform refresh -- update the state to match remote systems
  • terraform output -- show output values from the main.tf plan
  • terraform destroy -- clean up anything created by terraform
  • terraform fmt -- spacing is important in HCL, terraform's language, use this command to format all spacing in the current working directory

To get started, create a directory to hold all of the files.  Terraform will examine the local file or files, and then populate a local terraform.tfstate data file which represents the most recent refresh of the information from the Rancher API.  The files below can be separate or all together in a main.tf file.  Separating plan files into individual pieces can make managing a larger project easier.  Terraform will take actions required using variables supplied by the user or admin, or computed during the "apply" operation.  As a typical rule of thumb for any provider, "data" sources are read operations while "resource" operations are write/create/change.

Upon running "terraform apply" with the main.tf file below, terraform will contact the Rancher API, authenticate, request the cluster_info for the local Rancher cluster with ID "local" and store it into the terraform statefile, as well as output to the screen.  The comments explain a potential name for each file, the only requirement that it ends in "tf".

### tfvars.tf or environment.tf

#  these outline the url speaking to, and the authorization token 

variable "api_url" {
  description = "rancher api url"
  default     = "https://urlto.rancher-fqdn.com/v3"
}

variable "token_key" {
  description = "api key to use for tf"
  default     = "token-nameid:jwt-long-hash-string"
}


### providers.tf

# use the variables from the earlier section to define the provider

provider "rancher2" {
  api_url   = var.api_url
  token_key = var.token_key
  insecure  = true
}


### versions.tf

# tell terraform what versions of providers and terraform itself, to expect

terraform {
  required_providers {
    rancher2 = {
      source  = "rancher/rancher2"
      version = ">= 1.24.1"
    }
  }
  required_version = ">= 1.3.1"
}


### main.tf 

## hard-coded example, read cluster info for local 
## export with 'terraform output cluster_info'

data "rancher2_cluster" "local" {
  name = "local"
}

output "cluster_info" {
  value     = data.rancher2_cluster.local
}

 

Status

Top Issue

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:000020792
  • Creation Date: 29-Sep-2022
  • Modified Date:30-Sep-2022
    • SUSE Rancher

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

tick icon

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

tick icon

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.

tick icon

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.