Terraform: Difference between revisions

From Piszczynski
No edit summary
Line 22: Line 22:
Inspect state of configuration<syntaxhighlight lang="terraform">
Inspect state of configuration<syntaxhighlight lang="terraform">
terraform show
terraform show
</syntaxhighlight>
== Terraform Configuration files ==
The main config files are the main.tf and variables.tf files
=== main.tf ===
The main.tf contains the main configuration about the build you are creating. The first bit of info in the file is the terraform provider. This is a block of code describing the service that terraform will be building on (AWS, Azure, vSphere etc). It will also contain the info to login to the service.
for the vsphere provider:<syntaxhighlight lang="terraform">
provider "vsphere" {
  user          = "${var.vsphere_user}"
  password      = "${var.vsphere_password}"
  vsphere_server = "${var.vsphere_server}"
  allow_unverified_ssl = true
}
</syntaxhighlight>
=== variables.tf ===
This file will contain the variables for the main.tf file. This way it is easier to do different builds using the same main file.
example of the variable code block for the vsphere provider variables:<syntaxhighlight lang="terraform">
variable "vsphere_user" {
  description = "Username for vSphere"
  default = "<username>"
}
variable "vsphere_password" {
  description = "Password for vSphere"
  default = "<password>"
}
variable "vsphere_server" {
  description = "vSphere server address"
  default    = "<FQDN of vCenter server>"
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 21:52, 25 November 2023

Basic Commands

Initialise configuration: (use folder as a location to store terraform configuration files )

terraform init

Format configuration files

terraform fmt

Validate configuration

terraform validate

Show possible changes before applying:

terraform plan

Apply changes:

terraform apply

Inspect state of configuration

terraform show

Terraform Configuration files

The main config files are the main.tf and variables.tf files

main.tf

The main.tf contains the main configuration about the build you are creating. The first bit of info in the file is the terraform provider. This is a block of code describing the service that terraform will be building on (AWS, Azure, vSphere etc). It will also contain the info to login to the service.

for the vsphere provider:

provider "vsphere" {
   user           = "${var.vsphere_user}"
  password       = "${var.vsphere_password}"
  vsphere_server = "${var.vsphere_server}"
  allow_unverified_ssl = true
}

variables.tf

This file will contain the variables for the main.tf file. This way it is easier to do different builds using the same main file.

example of the variable code block for the vsphere provider variables:

variable "vsphere_user" {
  description = "Username for vSphere"
  default = "<username>"
}

variable "vsphere_password" {
  description = "Password for vSphere"
  default = "<password>"
}

variable "vsphere_server" {
  description = "vSphere server address"
  default     = "<FQDN of vCenter server>"
}