关于使用Terraform为Azure创建一个资源组的简单案例

发布时间 2023-08-16 21:00:54作者: 520_1351

使用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、最后一起看看执行过程

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 ]$