微软云配置
指南:https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli
64位win11使用 PowerShell 安装 Azure CLI
方法一
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://aka.ms/installazurecliwindowsx64 -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; Remove-Item .\AzureCLI.msi
对于这一种方法安装完成后需重启PowerShell
方法二
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
安装完成后验证
az --version
登录
az login
1、创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
2、创建名为 providers.tf 的文件并插入下列代码:
terraform {
required_providers {
azapi = {
source = "azure/azapi"
version = "=0.1.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.2"
}
random = {
source = "hashicorp/random"
version = "=3.1.2"
}
}
}
provider "azapi" {
}
provider "azurerm" {
features {}
}
provider "random" {
}
3、创建名为 main.tf 的文件并插入下列代码:
resource "azurerm_resource_group" "qs101" {
name = "rg-qs101-eh-rules"
location = "westus2"
}
resource "azurerm_virtual_network" "qs101" {
name = "myvnet"
location = azurerm_resource_group.qs101.location
resource_group_name = azurerm_resource_group.qs101.name
address_space = ["172.17.0.0/16"]
dns_servers = ["10.0.0.4", "10.0.0.5"]
}
resource "azurerm_subnet" "qs101" {
name = "default"
resource_group_name = azurerm_resource_group.qs101.name
virtual_network_name = azurerm_virtual_network.qs101.name
address_prefixes = ["172.17.0.0/24"]
service_endpoints = ["Microsoft.EventHub"]
}
resource "random_pet" "qs101_namespace" {
length = 3
separator = ""
}
resource "azurerm_eventhub_namespace" "qs101" {
name = random_pet.qs101_namespace.id
location = azurerm_resource_group.qs101.location
resource_group_name = azurerm_resource_group.qs101.name
sku = "Standard"
capacity = 2
}
4、创建名为 variables.tf 的文件并插入下列代码:
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
5、创建名为 outputs.tf 的文件并插入下列代码:
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
进入资源所在文件夹
执行命令terraform init -upgrade
如下图
执行命令terraform plan -out main.tfplan
执行命令terraform apply main.tfplan
验证结果
(terraform output -raw resource_group_name)
其他
创建资源组
语法az group create --name <name> --location <location>
az group create --name myResourceGroupVM --location eastasia
Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。 必须在创建虚拟机前创建资源组。 在此示例中,在“chinaeast”区域中创建了名为“myResourceGroupVM”的资源组。
可用的location如下:
'eastasia,southeastasia,australiaeast,australiasoutheast,brazilsouth,canadacentral,canadaeast,switzerlandnorth,germanywestcentral,eastus2,eastus,centralus,northcentralus,francecentral,uksouth,ukwest,centralindia,southindia,jioindiawest,italynorth,japaneast,japanwest,koreacentral,koreasouth,northeurope,norwayeast,polandcentral,qatarcentral,swedencentral,uaenorth,westcentralus,westeurope,westus2,westus,southcentralus,westus3,southafricanorth,australiacentral,australiacentral2,israelcentral,westindia
验证资源组是否创建成功
az group list
创建虚拟机
az vm create --resource-group myResourceGroupVM --name myVM --image SuseSles15SP3 --public-ip-sku Standard --admin-username azureuser --generate-ssh-keys
创建 VM 可能需要几分钟。 创建 VM 后,Azure CLI 会输出有关 VM 的信息。 请记下publicIpAddress,可以使用此地址访问虚拟机。