首页 > 其他分享 >关于使用Terraform为Azure创建一个资源组的简单案例

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

时间:2023-08-16 21:15:23浏览次数:58  
标签:resource name 案例 random terraform Terraform rg Azure group

使用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

     

 

标签:resource,name,案例,random,terraform,Terraform,rg,Azure,group
From: https://www.cnblogs.com/5201351/p/17636176.html

相关文章

  • 【Azure Service Fabric】关于Service Fabric的相关问题
    问题一:ServiceFabric是否支持PrivateLink?在AzurePrivateEndpoint文档中,罗列出了Azure上支持PrivateLink的服务。ServiceFabric不在其中。AzurePrivateLinkavailability:https://learn.microsoft.com/en-us/azure/private-link/availability 问题二:是否可以Disable......
  • 【Azure Service Fabric】关于Service Fabric的相关问题
    问题一:ServiceFabric是否支持PrivateLink?在AzurePrivateEndpoint文档中,罗列出了Azure上支持PrivateLink的服务。ServiceFabric不在其中。AzurePrivateLinkavailability:https://learn.microsoft.com/en-us/azure/private-link/availability 问题二:是否可以Dis......
  • WebSocket_入门案例
           ......
  • [Microsoft Azure] 配置Azure App Service仅虚拟内网访问
    本文将介绍如何配置AzureAppService以仅允许虚拟内网访问,从而提高安全性和隐私性。我们将讨论配置过程的不同步骤以及注意事项,以便您可以轻松实现更安全的应用部署。 随着云计算的普及,越来越多的企业和开发者开始将其应用部署到云平台上。在这种情况下,如何确保应用安全性和......
  • 河北沧州佰盛天璞售楼部金属结构门楼工程案例 中式售楼部铝艺门楼 小区景观廊架
    河北沧州佰盛天璞售楼部金属结构门楼工程案例中式售楼部铝艺门楼小区景观廊架......
  • 深度解读智能化编码的技术架构与实践案例
    向更智能、更兼容演进。陈高星|演讲者大家好,我是阿里云视频云的陈高星,今天和大家分享的主题是“多”维演进:智能化编码架构的研究与实践。本次分享分为四部分:首先是视频编码与增强方向的业界趋势,其次是对在该背景下衍生的阿里云视频云智能编码架构进行介绍,以及其中关于“多”维演......
  • ChatGPT 问答00017 在Disruptor中,WorkHandler使用案例
    在Disruptor中,WorkHandler通常用于实现多线程消费事件的场景。下面是一个简单的示例,展示了如何使用WorkHandler:假设我们有一个RingBuffer,用于存储事件,并且有多个工作线程需要并发地处理这些事件。我们定义一个名为Event的类作为事件对象,并创建一个EventWorkHandler来处理这些事......
  • ChatGPT 问答00018 在Disruptor中,EventHandler使用案例
    在Disruptor中,EventHandler通常用于并行处理事件的场景。下面是一个简单的示例,展示了如何使用EventHandler:假设我们有一个RingBuffer,用于存储事件,并且有多个事件处理器需要并发地处理这些事件。我们定义一个名为Event的类作为事件对象,并创建多个EventHandler来处理这些事件。首......
  • 【Azure K8S | AKS】在中国区AKS上遇见ImagePullBackOff时的替代方案
    问题描述在AKS集群中部署calico时候,遇见ImagePullBackOff问题。在创建POD calico-typha-horizontal-autoscale时候遇见拉取镜像失败问题。错误消息为:Failedtopullimage"k8s.gcr.io/cluster-proportional-autoscaler-amd64:1.1.2-r2":rpcerror:code=Unknowndesc=E......
  • 网站建设与网页设计案例教程
    网站建设与网页设计是现代社会中非常重要的技能,对于任何一个企业或个人来说都至关重要。本文将介绍一个网站建设与网页设计的案例教程,帮助读者了解如何进行网站建设和网页设计。首先,网站建设是一个包括多个步骤的过程。首先,需要确定网站的目标和受众,明确网站的定位和功能。然后,进行......