当前位置:   article > 正文

史上最简单的Terraform教程不浪费时间

terraform教程

废话

为什么起这个名字,因为他折磨了我很久,久到一周时间,看了github很多代码,最终,百度大法搞定。
我使用的是Azure,所以此教程针对Azure,其他云等用到会接着再此更新。

Terraform安装

https://docs.microsoft.com/en-us/azure/developer/terraform/quickstart-configure
直接看按照教程安装即可,说实话,不重要,不认识英文都能看得懂

Terraform使用

拿Azure里面的资源StorageAccount做例子
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account

Terraform执行命令

前三条创建资源
后两条销毁资源

terraform init
terraform plan -out main.tfplan
terraform apply main.tfplan

terraform plan -destroy -out main.destroy.tfplan
terraform apply main.destroy.tfplan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

场景一

一个资源组下面创建一个storage_account,直接官网拿过来用就可以

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_storage_account" "example" {
  name                     = "storageaccountname"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"

  tags = {
    environment = "staging"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

场景二

已有资源组下面创建storage_account
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group

data "azurerm_resource_group" "example" {
  name = "existing"
}

resource "azurerm_storage_account" "example" {
  name                     = "storageaccountname"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"

  tags = {
    environment = "staging"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

场景三

已有资源组下面创建多个storage_account

locals {
	stnames = ["st1","st2"]
}
data "azurerm_resource_group" "example" {
  name = "existing"
}

resource "azurerm_storage_account" "example" {
count = length(local.stnames )
  name                     = element(local.stnames, count.index)
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"

  tags = {
    environment = "staging"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Terraform模块化

这个时候出现了新的需求,每次创建资源总是那些内容,能不能进行模块化?当然可以,使用Terraform module。这个是真的坑很多

目录结构

在这里插入图片描述
resourcegroup->main.tf

resource "azurerm_resource_group" "example" {
  name     = var.rgname
  location = var.rglocation
}
  • 1
  • 2
  • 3
  • 4

resourcegroup->output.tf
此模块后面当module调用的时候可以访问属性

output "rgnames" {
  value       = azurerm_resource_group.example.name
}
output "rglocations" {
  value       = azurerm_resource_group.example.location
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

resourcegroup->variables.tf
此模块主要用来给变量赋值,在写module的时候必须包含这些变量

variable "rgname" {}
variable "rglocation" {}
  • 1
  • 2

staccount->main.tf

resource "azurerm_storage_account" "example" {
  name                     = var.stname
  resource_group_name      = var.rgname
  location                 = var.rglocation
  account_tier             = var.staccount_tier
  account_replication_type = var.staccount_replication_type
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

staccount->output.tf

在这里插入代码片
  • 1

staccount->variables.tf

variable "stname" {}
variable "rgname" {}
variable "rglocation" {}
variable "staccount_tier" {}
variable "staccount_replication_type" {}
  • 1
  • 2
  • 3
  • 4
  • 5

main.tf

locals {
    rgnames = ["rg1","rg2"]
    rglocations = ["West Europe","West Europe"]
	stnames = ["st1","st2"]
    account_tiers = ["Standard","Standard"]
    account_replication_type = ["GRS","GRS"]
}

module "rg" {
  source   = "./resourcegroup"
  count = length(local.stnames)
  rgname = element(local.rgnames, count.index)
  rglocation = element(local.rglocations, count.index)
}

module "st"{
  source   = "./resourcegroup"
  count = length(local.stnames)
  name                     = element(local.stnames, count.index)
  resource_group_name      = element(module.rg[*].rgnames, count.index) 
  /*不知道是azure的问题还是terraform版本的问题,创建多个资源的时候,使用已创建的模型的时候需要加[*]*/
  location                 = element(module.rg[*].rglocations, count.index)

  account_tier             = element(local.account_tiers, count.index)
  account_replication_type = element(local.account_replication_type, count.index)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

providers.tf

provider "azurerm" {
  features {}
}
  • 1
  • 2
  • 3

注意点

主要是module的调用方式
其他变量保持一致即可

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号