对于天蓝色的地形,我不清楚输出命令吗? [英] For azure terraform I am unclear about the output command?

查看:65
本文介绍了对于天蓝色的地形,我不清楚输出命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不了解何时在天蓝色的地形中使用输出"命令?输出到哪里去了?为什么我们需要此输出?我有多个将tf文件转换为azure的aw文件.输出什么类型的信息?

Do not understand when the Output command is used in azure terraform? Where is the output going? Why do we need this output for? We have multiple tf files in aws which I am converting to azure. What type of information is outputted?

没有此输出信息会发生什么?

Without this output information what would happen?

请提供示例.

谢谢

推荐答案

很多问题,我不确定我能否涵盖所有内容,但我可以举个例子.

A lot of questions, I'm not sure I can cover all this but I can deo give an example.

一种好的做法是先构建terraform模块,然后使用这些模块的输出将参数传递给其他使用相同类型输入的模块.

A good practice would be to build terraform modules, then use the output from these modules to pass along arguments to other modules consuming the same type of input.

以下是一个基本模块",它创建资源组,Vnet,几个子网(例如外部和内部),NSG并将规则绑定到子网

The following is a "base module" that creates a resource group, a Vnet, couple of subnets (external and internal for example), NSG and binds the rules to the subnets

resource "azurerm_resource_group" "main_rg" {
  name     = var.resource_group_name
  location = var.location

  tags = {
    group    = var.resource_group_name
    Customer = var.tag_reference
    stack    = "resource group"
  }

}

resource "azurerm_virtual_network" "main_vnet" {
  name                = "${azurerm_resource_group.main_rg.name}-primary-vnet"
  address_space       = ["${var.vnet_cidr}"]
  location            = azurerm_resource_group.main_rg.location
  resource_group_name = azurerm_resource_group.main_rg.name

  tags = {
    group    = var.resource_group_name
    Customer = var.tag_reference
    stack    = "virtual network"
  }

}

resource "azurerm_subnet" "backend_subnet" {
  name                 = "${azurerm_resource_group.main_rg.name}-backend-subnet"
  resource_group_name  = azurerm_resource_group.main_rg.name
  virtual_network_name = azurerm_virtual_network.main_vnet.name
  address_prefix       = var.backend_subnet_cidr
  service_endpoints    = ["Microsoft.Sql", "Microsoft.Storage"]
}

resource "azurerm_subnet" "frontend_subnet" {
  name                 = "${azurerm_resource_group.main_rg.name}-frontend-subnet"
  resource_group_name  = azurerm_resource_group.main_rg.name
  virtual_network_name = azurerm_virtual_network.main_vnet.name
  address_prefix       = var.frontend_subnet_cidr
  service_endpoints    = ["Microsoft.Sql"]
}

resource "azurerm_network_security_group" "default_nsg" {
  name                = "${azurerm_resource_group.main_rg.name}-nsg"
  location            = azurerm_resource_group.main_rg.location
  resource_group_name = azurerm_resource_group.main_rg.name

  security_rule {
    name                        = "appgwV1Exception"
    priority                    = 100
    direction                   = "Inbound"
    access                      = "Allow"
    protocol                    = "*"
    source_port_range           = "*"
    destination_port_range      = "65503-65534"
    source_address_prefix       = "Internet"
    destination_address_prefix  = "*"
    description = "This is needed to allow helth check of backend server to pass according to official documentation"
  }

  security_rule {
    name                        = "appgwV2Exception"
    priority                    = 200
    direction                   = "Inbound"
    access                      = "Allow"
    protocol                    = "*"
    source_port_range           = "*"
    destination_port_range      = "65200-65535"
    source_address_prefix       = "Internet"
    destination_address_prefix  = "*"
    description = "This is needed to allow helth check of backend server to pass according to official documentation"
  }

  security_rule {
    name                        = "Office"
    priority                    = 500
    direction                   = "Inbound"
    access                      = "Allow"
    protocol                    = "*"
    source_port_range           = "*"
    destination_port_range      = "443"
    source_address_prefix       = "1.2.3.4/32"
    destination_address_prefix  = "*"
    description                 = "Allow 443 access from the office"
  }


  tags = {
    group    = var.resource_group_name
    Customer = var.tag_reference
    stack    = "NSG"
  }
}


resource "azurerm_subnet_network_security_group_association" "nsg_to_backend" {
  network_security_group_id = azurerm_network_security_group.default_nsg.id
  subnet_id                 = azurerm_subnet.backend_subnet.id
}

resource "azurerm_subnet_network_security_group_association" "nsg_to_frontend" {
  network_security_group_id = azurerm_network_security_group.default_nsg.id
  subnet_id                 = azurerm_subnet.frontend_subnet.id
}


output "resource_group_name" {
  value = azurerm_resource_group.main_rg.name
}

output "vnet_name" {
  value = azurerm_virtual_network.main_vnet.name
}

output "vnet_id" {
  value = azurerm_virtual_network.main_vnet.id
}

output "backend_subnet_id" {
  value = azurerm_subnet.backend_subnet.id
}

output "frontend_subnet_id" {
  value = azurerm_subnet.frontend_subnet.id
}

output "nsg_id" {
  value = azurerm_network_security_group.default_nsg.id
}

再次使用terraform最佳做法是使用输出文件,但现在我将为您省去这部分,请参阅文件底部的输出,现在我还有另一个用于创建VM的模块,因此我的main.tf您将会看到或整个文件看起来像这样:

Again the terraform best practice is to use an output file but I'll spare you that for now, see the outputs at the bottom of the file, now I have another module for creating a VM, so my main.tf if you will or the complete file might look a bit something like this:

provider "azurerm" {
  version = "~> 1.21"
}

terraform {
  backend "azurerm" {}
}


module "base_infra" {
  source               = "../../base_infra"
  location             = var.location
  resource_group_name  = var.resource_group_name
  vnet_cidr            = var.vnet_cidr
  backend_subnet_cidr  = var.backend_subnet_cidr
  frontend_subnet_cidr = var.frontend_subnet_cidr
  tag_reference        = var.tag_reference
}

module "webapp_vm" {
  source               = "../../webapp"
  resource_group_name  = module.base_infra.resource_group_name
  location             = var.location
  vnet_cidr            = module.base_infra.main_vnet_id
  subnet_id            = module.base_infra.backend_subnet_id
  tag_reference        = var.tag_reference
  datadisk_size_gb     = "200"
  instance_count       = "1"
  instance_name_prefix = "${module.base_infra.resource_group_name}-webapp"
  vm_size              = var.vm_size
  vm_username          = var.vm_username
  vm_password          = module.webapp_vm_password.password_result
}

我确实回答了一些错误,希望我能给你足够的信息

I did cut some corners in my answer, hope I gave you enough

这篇关于对于天蓝色的地形,我不清楚输出命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆