【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

 

相关文章
|
5月前
|
运维 安全 Linux
【清爽加速】Windows 11 Pro 24H2-Emmy精简系统
“清爽加速”Windows 11 Pro 24H2 针对老旧或低配设备,通过精简系统、优化服务与简化装机流程,降低资源占用,提升运行流畅度,兼顾安全性与稳定性,让老设备也能轻松应对日常办公与轻度娱乐需求。
325 1
【清爽加速】Windows 11 Pro 24H2-Emmy精简系统
|
5月前
|
安全 搜索推荐 开发者
【适度精简】Windows 7 旗舰版-emmy精简系统
Windows 7旗舰版因硬件占用高、冗余组件多、兼容性差及缺乏安全更新等问题,逐渐难以满足用户需求。适度精简版通过去除无用组件、优化性能与安全性,提升老旧设备运行效率,增强兼容性与稳定性,同时保留用户熟悉的操作界面,降低学习成本,满足个性化需求,延续Windows 7的实用价值。
313 2
|
5月前
|
安全 数据安全/隐私保护 Windows
ZyperWin++使用教程!让Windows更丝滑!c盘飘红一键搞定!ZyperWin++解决系统优化、Office安装和系统激活
ZyperWin++是一款仅5MB的开源免费Windows优化工具,支持快速优化、自定义设置与垃圾清理,兼具系统加速、隐私保护、Office安装等功能,轻便无广告,小白也能轻松上手,是提升电脑性能的全能管家。
1747 0
|
6月前
|
Ubuntu Linux Windows
windows11系统安装ubuntu系统详细步骤
安装后,您可以直接从商店启动应用程序来源
1296 0
|
运维 安全 IDE
加速阿里云部署:Terraform在甄云科技的深度应用
甄云科技是一家领先的数字化采购平台服务商,通过Terraform实现全球云基础设施的高效管理与快速部署。公司成立于2017年,已服务全球30多个行业的中大型企业,客户遍布20多个国家和地区。利用IaC(基础架构即代码)理念和Terraform工具,甄云科技显著提升了开发与运维效率,减少了人为错误,加快了迭代速度,并支持业务快速扩展,为全球化战略提供了稳固的云基础架构支持。未来,公司将持续优化技术框架,回馈社区,助力更多企业的数字化转型。
|
弹性计算 API 持续交付
基于 ROS 的 Terraform 托管服务轻松部署文本转语音系统 ChatTTS
基于 IaC 的理念,通过定义一个模板,使用 ROS 提供的 Terraform 托管服务进行自动化部署,可以非常高效快捷地部署任意云资源和应用(比如 ChatTTS 服务)。相比于手动部署或者通过 API、SDK 的部署方式,有着高效、稳定等诸多优势,也是服务上云的最佳实践。
495 58
基于 ROS 的 Terraform 托管服务轻松部署文本转语音系统 ChatTTS
|
弹性计算 持续交付 API
基于 ROS 的Terraform托管服务轻松部署ChatGLM-6B
文章介绍了如何利用ROS和Terraform模板轻松自动化部署基于GLM架构、优化中文对话的ChatGLM-6B模型至阿里云,提高了部署效率与便捷性,适用于多种应用场景,且模型部署过程详细,彰显了基础设施即代码(IaC)的优势。
基于 ROS 的Terraform托管服务轻松部署ChatGLM-6B
|
弹性计算 人工智能 持续交付
基于 ROS 的Terraform托管服务轻松部署Qwen-7B-Chat
文章介绍了如何利用ROS和Terraform模板轻松自动化部署阿里云的Qwen-7B-Chat大语言模型服务,提高了部署效率与便捷性,是实现云资源和服务快速上线的最佳实践。
基于 ROS 的Terraform托管服务轻松部署Qwen-7B-Chat
|
弹性计算 持续交付 数据中心
一键云部署:ROS的Terraform托管服务助你轻松上线2048经典游戏
阿里云的资源编排服务ROS提供了Terraform托管能力,用户可以直接在ROS控制台上部署Terraform脚本,本文将详细介绍如何使用ROS的Terraform托管服务一键部署经典的2048小游戏到云端,让全世界的玩家都能在线体验。
EMQ
|
JSON 负载均衡 物联网
使用 Terraform 在 GCP 上一键部署 EMQX MQTT Broker
本文将指导您如何设置 GCP 项目、创建服务账户、编写 Terraform 配置文件,实现在 GCP 上轻松部署 EMQX MQTT Broker。
EMQ
361 0
使用 Terraform 在 GCP 上一键部署 EMQX MQTT Broker

推荐镜像

更多