User Tools

Site Tools


devops:terraform

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
devops:terraform [2022/09/22 16:37] skipidardevops:terraform [2024/04/03 19:53] (current) skipidar
Line 1: Line 1:
 ===== Terraform ===== ===== Terraform =====
 +
 +===Debugging===
 +
 +
 +<sxh shell>
 +# https://developer.hashicorp.com/terraform/internals/debugging
 +# investigage errors
 +export TF_LOG="TRACE"
 +export TF_LOG_PATH="./terraform.log"
 +</sxh>
  
 ===Glossary=== ===Glossary===
Line 16: Line 26:
 https://github.com/skipidar/terraform-skeleton https://github.com/skipidar/terraform-skeleton
  
-Apply terraform bash script+== Apply terraform bash script == 
 <sxh bash> <sxh bash>
  
Line 31: Line 41:
  
 #terraform apply #terraform apply
 +</sxh>
  
 +== Upgrade terraform provider ==
  
-</sxh>+https://developer.hashicorp.com/terraform/tutorials/configuration-language/provider-versioning
  
 +<sxh bash>
  
 +terraform init -upgrade
 +
 +Initializing the backend...
 +
 +Initializing provider plugins...
 +- Finding hashicorp/aws versions matching "~> 5.0"...
 +- Installing hashicorp/aws v5.21.0...
 +- Installed hashicorp/aws v5.21.0 (signed by HashiCorp)
 +
 +Terraform has made some changes to the provider dependency selections recorded
 +in the .terraform.lock.hcl file. Review those changes and commit them to your
 +version control system if they represent changes you intended to make.
 +
 +Terraform has been successfully initialized!
 +</sxh>
  
 === Main === === Main ===
Line 104: Line 132:
  
 === Data === === Data ===
 +
 +When you define ''data'' blocks Terraform communicates with the **AWS provider** to query AWS and fetch the list of requested resources, mentioned in data-block.
 +
 +<sxh json>
 +
 +</sxh>
 +
 +You can apply filters.
 +
 +<sxh json>
 +
 +# Find the latest available AMI that is tagged with Component = web
 +data "aws_ami" "web" {
 +  filter {
 +    name   = "state"
 +    values = ["available"]
 +  }
 +
 +  filter {
 +    name   = "tag:Component"
 +    values = ["web"]
 +  }
 +
 +  most_recent = true
 +}
 +</sxh>
 +
  
 Create **templates.tf** Create **templates.tf**
Line 142: Line 197:
 } }
 </sxh> </sxh>
 +
 +
 +=== Dynamic block ===
 +
 +See https://spacelift.io/blog/terraform-dynamic-blocks
 +
 +To replace the repetitive code as here in a module:
 +<sxh json>
 +resource "azurerm_virtual_network" "dynamic_block" {
 +  name                = "vnet-dynamicblock-example-centralus"
 +  resource_group_name = azurerm_resource_group.dynamic_block.name
 +  location            = azurerm_resource_group.dynamic_block.location
 +  address_space       = ["10.10.0.0/16"]
 +
 +  subnet {
 +    name           = "snet1"
 +    address_prefix = "10.10.1.0/24"
 +  }
 +
 +  subnet {
 +    name           = "snet2"
 +    address_prefix = "10.10.2.0/24"
 +  }
 +
 +  subnet {
 +    name           = "snet3"
 +    address_prefix = "10.10.3.0/24"
 +  }
 +
 +  subnet {
 +    name           = "snet4"
 +    address_prefix = "10.10.4.0/24"
 +  }
 +}
 +</sxh>
 +
 +
 +Use the "dynamic" block
 +
 +<sxh json>
 +resource "azurerm_virtual_network" "dynamic_block" {
 +  name                = "vnet-dynamicblock-example-centralus"
 +  resource_group_name = azurerm_resource_group.dynamic_block.name
 +  location            = azurerm_resource_group.dynamic_block.location
 +  address_space       = ["10.10.0.0/16"]
 +
 +  dynamic "subnet" {
 +    for_each = var.subnets
 +    iterator = item   #optional
 +    content {
 +      name           = item.value.name
 +      address_prefix = item.value.address_prefix
 +    }
 +  }
 +}
 +
 +</sxh>
 +
 +
 +Declare a variable in your module
 +<sxh json>
 +variable "subnets" {
 +  description = "list of values to assign to subnets"
 +  type = list(object({
 +    name           = string
 +    address_prefix = string
 +  }))
 +}
 +</sxh>
 +
 +
 +**USAGE of your module**
 +
 +Assigning values to the variable "subnets", which are taken by the module above.
 +
 +<sxh json>
 +subnets = [
 +  { name = "snet1", address_prefix = "10.10.1.0/24" },
 +  { name = "snet2", address_prefix = "10.10.2.0/24" },
 +  { name = "snet3", address_prefix = "10.10.3.0/24" },
 +  { name = "snet4", address_prefix = "10.10.4.0/24" }
 +]
 +</sxh>
 +
devops/terraform.1663864633.txt.gz · Last modified: by skipidar