使用Terraform创建Azure Cloud平台的资源需要,得还有如下主要的环境及条件
a、安装有Terraform
b、解决身份认证及相关的权限
++++++++++++++++++++++++++++++++++++++++
本文的目标,创建一个 rg-开头 的随机名称的 资源组
先得准备有4个文件,【providers.tf】、【main.tf】、【variables.tf】、【outputs.tf】,其中前两个是应该必须要有的
1、providers.tf 文件内容
terraform { required_version = ">=0.12" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
2、main.tf 文件的内容
resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id }
3、variables.tf 变量文件的内容
variable "resource_group_location" { default = "eastus" description = "Location of the resource group." } variable "resource_group_name_prefix" { default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." }
4、outputs.tf 内容输出文件的内容
当然这部分就算写入到 main.rtf 中也是可以的,只是单独写到一个这样一个文件,也比较规范
output "resource_group_name" { value = azurerm_resource_group.rg.name }
5、最后一起看看Terraform的执行过程
li [ ~/qq-5201351 ]$ cd ../001-ResourceGroup/ li [ ~/001-ResourceGroup ]$ ls main.tf outputs.tf providers.tf variables.tf li [ ~/001-ResourceGroup ]$ li [ ~/001-ResourceGroup ]$ terraform init -upgrade Initializing the backend... Initializing provider plugins... - Finding hashicorp/azurerm versions matching "~> 2.0"... - Finding hashicorp/random versions matching "~> 3.0"... - Installing hashicorp/azurerm v2.99.0... - Installed hashicorp/azurerm v2.99.0 (signed by HashiCorp) - Installing hashicorp/random v3.5.1... - Installed hashicorp/random v3.5.1 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. li [ ~/001-ResourceGroup ]$ terraform plan -out main.tfplan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_resource_group.rg will be created + resource "azurerm_resource_group" "rg" { + id = (known after apply) + location = "eastus" + name = (known after apply) } # random_pet.rg_name will be created + resource "random_pet" "rg_name" { + id = (known after apply) + length = 2 + prefix = "rg" + separator = "-" } Plan: 2 to add, 0 to change, 0 to destroy. Changes to Outputs: + resource_group_name = (known after apply) ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Saved the plan to: main.tfplan To perform exactly these actions, run the following command to apply: terraform apply "main.tfplan" li [ ~/001-ResourceGroup ]$ terraform apply main.tfplan random_pet.rg_name: Creating... random_pet.rg_name: Creation complete after 0s [id=rg-exotic-hound] azurerm_resource_group.rg: Creating... azurerm_resource_group.rg: Creation complete after 4s [id=/subscriptions/2027a3ea-db43-5d93-a2b3-caff8e3c1157/resourceGroups/rg-exotic-hound] Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: resource_group_name = "rg-exotic-hound" li [ ~/001-ResourceGroup ]$
主要是3条命令,这里分另解释一下:
terraform init -upgrade # 将 Terraform 部署进行初始化。 此命令将下载管理 Azure 资源所需的 Azure 提供程序
terraform plan -out main.tfplan # 命令将创建一个执行计划,但不会执行它。 它会确定创建配置文件中指定的配置需要执行哪些操作
terraform apply main.tfplan # 运行 terraform apply,将指定的执行计划应用到云基础结构
上面terraform apply 命令假定你之前运行 terraform plan -out main.tfplan过
如果为 -out 参数指定了不同的文件名,请在对 terraform apply 的调用中使用该相同文件名
如果未使用 -out 参数,请调用不带任何参数的 terraform apply
其他知识点:
1、对于variables.tf 中定义的变量的引用,var.变量名称 ,如上var.resource_group_name_prefix 的值就是定义的字符串 ”rg“
2、关于random_pet 这个类型资源的rg_name名称 ,定义了 prefix值为rg,其他默认的有 长度默认为2个单词-(不算prefix),默认使用-进行连接
3、random_pet 这个类型资源的rg_name名称,如属性 length 和 separator,我们也可以自己单独定义的
4、关于random_pet 这个类型资源的更多的帮助及更高级的使用方法,还可以参考其如下的官方网址
https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet
尊重别人的劳动成果 转载请务必注明出处: https://www.cnblogs.com/5201351/p/17636176.html
作者:一名卑微的IT民工
出处:https://www.cnblogs.com/5201351
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
由于博主的水平不高,文章没有高度、深度和广度,只是凑字数,不足和错误之处在所难免,希望大家能够批评指出。
博主是利用读书、参考、引用、复制和粘贴等多种方式打造成自己的文章,请原谅博主成为一个卑微的IT民工!