【Azure 环境】在Windows系统中 使用Terraform创建中国区Azure资源步骤(入门级)

简介: 【Azure 环境】在Windows系统中 使用Terraform创建中国区Azure资源步骤(入门级)

Terraform(全称:Hashicorp Terraform )是一种开源工具,用于预配和管理云基础结构。 它将基础结构编入描述云资源拓扑的配置文件中。 这些资源包括虚拟机、存储帐户和网络接口等。

本文介绍在Windows系统中,如何安装Terraform,并且根据Azure的示例模板创建,在中国区创建一个虚拟机(VM)的入门级教程。

 

安装Terraform

Terraform的Windows版本为一个.exe文件,下载后把放置在自定义的文件夹中,最后配置号系统的PATH即可。

  • 下载Terraform(https://www.terraform.io/downloads.html),根据情况选择Windows 32-bit版 或 64-bit版
  • 解压文件,复制到目标文件夹中,如:C:\LBWorkSpace\tool
  • 配置系统PATH路径
  • 打开CMD,使用Terraform --version测试

准备创建VM的Terraform模板

准备Terraform模板,参考Azure的官方文档:使用 Terraform 在 Azure 中创建带有基础结构的 Linux VM。可以一步一步的编写模板,也可全部COPY至本地,并命名为:terraform_azure.tf(名字可以随便改动,文件后缀名tf不可变)。

View Code

 

完成 Terraform 脚本https://docs.microsoft.com/zh-cn/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure#complete-terraform-script

 

登录到中国区Azure

本文使用Visual Studio Code工具来展示命令及Terraform脚本,也可以直接使用PowerShell窗口。

一:打开VS Code,使用 az cloud set --name AzureChinaCloud 设置登录环境为China Azure。

二:使用 az login 登录

三:如有多个订阅号,可以使用 az account set --subscription "your subscription id" 指定资源所创建的订阅  

执行Terraform init, plan, apply命令

第一步:初始化 terraform init

命令

terraform init

输出

PS C:\LBWorkSpace\MyCode\24-Terraform> terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/tls from the dependency lock file
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Using previously-installed hashicorp/tls v3.1.0
- Using previously-installed hashicorp/azurerm v2.56.0
- Using previously-installed hashicorp/random v3.1.0

 Warning: Version constraints inside provider configuration blocks are deprecated
 
   on terraform_azure.tf line 5, in provider "azurerm":
    5:     version = "~>2.0"
 
 Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will 
 be removed in a future version of Terraform. To silence this warning, move the provider version constraint into the required_providers block.

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.

第二步:检查并验证模板 terraform plan

命令

terraform plan

输出

PS C:\LBWorkSpace\MyCode\24-Terraform> terraform plan
tls_private_key.example_ssh: Refreshing state... [id=4b9fa2d1e40856b8ed19e1978c7713feb660ce9b]
azurerm_resource_group.myterraformgroup: Refreshing state... [id=/subscriptions/a9dc7515-7692-4316-9ad4-762f383eec10/resourceGroups/myResourceGroup]

 Warning: Version constraints inside provider configuration blocks are deprecated
 
   on terraform_azure.tf line 5, in provider "azurerm":
    5:     version = "~>2.0"
 
 Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will be removed in a future version of Terraform. To silence this
 warning, move the provider version constraint into the required_providers block.


 Error: "eastus" was not found in the list of supported Azure Locations: "chinaeast,chinanorth,chinanorth2,chinaeast2"
 
   on terraform_azure.tf line 10, in resource "azurerm_resource_group" "myterraformgroup":
   10: resource "azurerm_resource_group" "myterraformgroup" {
 


 Error: Output refers to sensitive values
 
   on terraform_azure.tf line 126:
  126: output "tls_private_key" { value = tls_private_key.example_ssh.private_key_pem }
 
 Expressions used in outputs can only refer to sensitive values if the sensitive attribute is true.

PS C:\LBWorkSpace\MyCode\24-Terraform>

注意:在检查模板时候有两个错误,是因为copy的terraform模板是创建在global azure的,而当前是在中国区azure,所以需要修改location从eastus到chinaeast或其他。 第二个错误output输出可以暂时注释126行模板即可。修改完成后,再次使用terraform plan命令检查模板,输出结果中会包含模板中将要创建,修改的资源列表。

第三步:构建模板中的资源 terraform apply

命令

terraform apply

输出(apply命令需要在执行前手动输入yes,然后开始真正执行创建资源。当创建完成后,可以看见Apply complete! Resources: 9 added, 0 changed, 0 destroyed.提示消息)

View Code

 

恭喜!踏入Terraform创建Azure资源大门。

 

参考资料

“Azure 上的 Terraform”文档: https://docs.microsoft.com/zh-cn/azure/developer/terraform/

使用 Terraform 在 Azure 中创建带有基础结构的 Linux VM: https://docs.microsoft.com/zh-cn/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure#complete-terraform-script

Install Azure CLI on Windows:https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-powershell

 

相关文章
|
2月前
|
安全 数据安全/隐私保护 Windows
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
134 15
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
|
2月前
|
人工智能 API 调度
微软开源Windows桌面智能体操作系统!UFO²:一句话调度多应用,自动协同工作
UFO²是微软推出的Windows桌面多智能体操作系统,通过中央HostAgent协调多个AppAgent实现跨应用任务自动化,结合GUI交互和原生API调用提升执行效率,支持虚拟桌面隔离运行。
224 3
微软开源Windows桌面智能体操作系统!UFO²:一句话调度多应用,自动协同工作
|
2月前
|
关系型数据库 虚拟化 UED
Omnissa Horizon Windows OS Optimization Tool 2503 - Windows 系统映像优化工具
Omnissa Horizon Windows OS Optimization Tool 2503 - Windows 系统映像优化工具
87 7
Omnissa Horizon Windows OS Optimization Tool 2503 - Windows 系统映像优化工具
|
2月前
|
XML 存储 搜索推荐
Omnissa Dynamic Environment Manager 2503 - 个性化动态 Windows 桌面环境管理
Omnissa Dynamic Environment Manager 2503 - 个性化动态 Windows 桌面环境管理
64 7
Omnissa Dynamic Environment Manager 2503 - 个性化动态 Windows 桌面环境管理
|
30天前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
332 0
|
2月前
|
Ubuntu 数据库 虚拟化
Windows 环境下 Odoo 安装保姆级教程
本教程详细介绍了在 Windows 系统上通过虚拟机部署 Odoo 的完整流程。首先确认硬件需求,确保 CPU、内存和磁盘空间满足最低配置;接着安装 VMware Workstation Pro 并创建 Ubuntu 虚拟机,配置桥接网络以实现主机与虚拟机的通信;随后借助微聚云快速安装预配置好的 Odoo 环境,简化复杂环境搭建;最后通过浏览器访问虚拟机 IP,完成 Odoo 数据库初始化及基础设置。整个过程清晰易懂,适合新手快速上手 Odoo 部署。
297 4
|
21天前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
58 2
|
3月前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
187 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
|
3月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
215 4
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
|
2月前
|
Linux 虚拟化 iOS开发
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
105 29
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板

推荐镜像

更多