史上最简单的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

场景一

一个资源组下面创建一个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模块化

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

目录结构

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

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的调用方式
其他变量保持一致即可

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以参考Terraform官方网站上的教程来学习Terraform的基础知识。在官方网站上,你可以找到有关Terraform的详细文档、教程和示例代码。此外,还有一些第三方的教程和资源,比如HashiCorp的学习平台和其他云服务提供商的文档。在学习Terraform之前,你可以先通过执行"terraform init"命令来初始化你的Terraform项目,然后使用"terraform plan"命令来查看你的计划,最后使用"terraform apply"命令来应用你的计划。这些命令是使用Terraform进行基本操作的关键步骤。另外,Terraform采用了插件模式的运行机制,它使用RPC与Terraform插件进行通信,并与具体的Provider(如AWS、Kubernetes、Azure等)对接,以封装各种资源操作的接口供Terraform Core使用。要开始使用Terraform,你只需要下载Terraform的二进制可执行文件,并将其添加到系统环境变量PATH中即可。这些资源将为你提供初学者的指导和教程,帮助你快速入门Terraform。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Terraform基础入门 (Infrastructure as Code)](https://blog.csdn.net/zhongxianyao/article/details/129105211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值