为什么起这个名字,因为他折磨了我很久,久到一周时间,看了github很多代码,最终,百度大法搞定。
我使用的是Azure,所以此教程针对Azure,其他云等用到会接着再此更新。
https://docs.microsoft.com/en-us/azure/developer/terraform/quickstart-configure
直接看按照教程安装即可,说实话,不重要,不认识英文都能看得懂
拿Azure里面的资源StorageAccount做例子
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account
前三条创建资源
后两条销毁资源
terraform init
terraform plan -out main.tfplan
terraform apply main.tfplan
terraform plan -destroy -out main.destroy.tfplan
terraform apply main.destroy.tfplan
一个资源组下面创建一个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"
}
}
已有资源组下面创建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"
}
}
已有资源组下面创建多个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"
}
}
这个时候出现了新的需求,每次创建资源总是那些内容,能不能进行模块化?当然可以,使用Terraform module。这个是真的坑很多
resource "azurerm_resource_group" "example" {
name = var.rgname
location = var.rglocation
}
resourcegroup->output.tf
此模块后面当module调用的时候可以访问属性
output "rgnames" {
value = azurerm_resource_group.example.name
}
output "rglocations" {
value = azurerm_resource_group.example.location
}
resourcegroup->variables.tf
此模块主要用来给变量赋值,在写module的时候必须包含这些变量
variable "rgname" {}
variable "rglocation" {}
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
}
staccount->output.tf
在这里插入代码片
staccount->variables.tf
variable "stname" {}
variable "rgname" {}
variable "rglocation" {}
variable "staccount_tier" {}
variable "staccount_replication_type" {}
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)
}
providers.tf
provider "azurerm" {
features {}
}
主要是module的调用方式
其他变量保持一致即可