on modules/security_group/main.tf line 64, in resource "azurerm_network_interface_security_group_association" "primary":
64: resource "azurerm_network_interface_security_group_association" "primary" {
我正在使用“ terraform validate”命令获得以上输出 以下是我用于terraform的配置。 这是我作为模块工作的树
├── main.tf
└── modules
├── network
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
├── resource
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
├── security_group
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
├── storage
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
└── vm
├── main.tf
├── variable.tf
└── variable.tfvars
main.cf:
#Select provider
provider "azurerm" {
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxx"
version = "~> 2.4"
features {}
}
module "resource" {
source = "./modules/resource"
resource_group_name = "devops_primary"
location = "southeastasia"
}
module "network" {
source = "./modules/network"
virtual_network = "primaryvnet"
subnet = "primarysubnet"
address_space = "192.168.0.0/16"
address_prefix = "192.168.1.0/24"
public_ip = "backendvmpip"
location = "southeastasia"
primary_nic = "backendvmnic"
primary_ip_conf = "backendvm"
resource_group_name = "devops_primary"
}
module "vm" {
source = "./modules/vm"
vm_name = "backendvm-primary"
vm_size = "standard_d2s_v3"
vm_storage_od_disk_name = "backend-vm-os-disk-primary"
computer_name = "backendserver"
username = "terraform"
ssh_key_path = "/home/terraform/.ssh/authorized_keys"
keys_data = "~/.ssh/id_rsa.pub"
}
module "security_group" {
source = "./modules/security_group"
sg_group_name = "primary_sg"
primary_nic_id = ["module.network.primary_nic_id"]
}
这是资源的main.cf文件:
#Select provider
provider "azurerm" {
subscription_id = "xxxxxxxxxxxxxxxxxxxxxx"
version = "~> 2.2"
features {}
}
#Create Primary Resource Group
resource "azurerm_resource_group" "primary" {
name = "var.resource_group_name"
location = "var.location"
tags = {
environment = "Test"
}
}
output "devops_primary" {
value = "${azurerm_resource_group.primary.name}"
}
output "location" {
value = "${azurerm_resource_group.primary.location}"
}
这是网络的main.cf文件:
#Create public IP address
resource "azurerm_public_ip" "primary" {
name = "var.public_ip"
location = "module.resource.azurerm_resource_group.primary.location"
resource_group_name = "module.resource.azurerm_resource_group.primary.name"
allocation_method = "Dynamic"
tags = {
environment = "Test"
}
}
output "public_ip_id"{
value = azurerm_public_ip.primary.id
}
#Create Network Interface
resource "azurerm_network_interface" "primary" {
name = "var.primary_nic"
location = "module.resource.azurerm_resource_group.primary.location"
resource_group_name = "module.resource.azurerm_resource_group.primary.name"
resource_group_name = var.resource_group_name
ip_configuration {
name = "var.primary_ip_conf"
#subnet_id = "${azurerm_subnet.primary.id}"
subnet_id = azurerm_subnet.primary.id
private_ip_address_allocation = "Dynamic"
#public_ip_address_id = "${azurerm_public_ip.primary.id}"
public_ip_address_id = azurerm_public_ip.primary.id
#public_ip_address_allocation = "Dymanic"
}
tags = {
environment = "Test"
}
# depends_on = [var.subnet_id_primary]
#depends_on = [module.resource.azurerm_resource_group.name]
}
output "primary_nic_id"{
description = "Primary VNET NIC Id "
value = ["azurerm_network_interface.primary.id"]
}
output "private_ip" {
description = "private ip addresses of the vm nics"
value = "${azurerm_network_interface.primary.private_ip_address}"
}
这是VM的main.cf文件:
#Create VM in Primary resource
resource "azurerm_virtual_machine" "primary" {
name = "var.vm_name"
location = "module.resource.azurerm_resource_group.primary.location"
resource_group_name = "module.resource.azurerm_resource_group.primary.name"
vm_size = "var.vm_size"
network_interface_ids = ["module.resource.azurerm_network_interface.primary.id"]
storage_os_disk {
name = "var.vm_storage_od_disk_name"
os_type = "Linux"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_profile {
computer_name = "var.computer_name"
admin_username = "var.username"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/terraform/.ssh/authorized_keys"
key_data = file("~/.ssh/id_rsa.pub")
}
}
tags = {
environment = "Test"
}
}
这是security_group的main.cf文件:
#Create Network Security Group
resource "azurerm_network_security_group" "primary" {
name = "var.sg_group_name"
#location = "module.resource.azurerm_resource_group.primary.location"
#resource_group_name = "module.resource.azurerm_resource_group.primary.name"
resource_group_name = "var.resource_group_name"
location = "var.location"
#Security Rules for Security Group
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "AppOut"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "8040"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "MySql"
priority = 1003
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3306"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "Redis"
priority = 1004
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "6379"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "Test"
}
}
variable "primary_nic_id" {}
# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "primary" {
#network_interface_id = "${module.network.azurerm_network_interface.primary.id}"
network_interface_id = "module.network.azurerm_network_interface.primary.id"
network_security_group_id = "${azurerm_network_security_group.primary.id}"
#depends_on = ["module.network.primary_nic_id"]
#primary_nic_id = ["var.primary_nic_id"]
}
#depends_on = [module.network.primary_nic_id]
# Generate a new ID only when a new resource group is defined
resource "random_id" "randomId" {
keepers = {
resource_group_name = "module.resource.azurerm_resource_group.primary.name"
}
byte_length = 8
}
请查看我当前正在使用的代码,我是terraform的新手,还只是一个学习者。
答案 0 :(得分:0)
首先,您可以删除所有非常数表达式的引号,并保留内部的内部表达式。要开始升级配置,请运行terraform 0.12upgrade
命令。
Terraform 0.11和更早版本要求所有非常数表达式必须为 通过插值语法提供,但现在不建议使用此模式。 要使此警告静音,请从开头删除“ $ { 此表达式末尾的}“序列,仅保留 内在表达。
模板插值语法仍用于从 模板包含多个插值时的表达式 序列或文字字符串和内插的混合。这个 弃用仅适用于完全由 单个插值序列。
调用模块意味着将该模块的内容包含在 为其输入变量指定特定值的配置。模组 在其他模块中使用
module
块进行调用:module "servers" { source = "./app-cluster" servers = 5 }
Accessing Module Output Values
模块中定义的资源被封装,因此调用 模块无法直接访问其属性。但是,孩子 模块可以声明输出值以有选择地导出某些值 由调用模块访问。
例如,如果
./app-cluster
模块导出了一个名为output
的值instance_ids
然后打电话 模块可以使用表达式引用该结果module.servers.instance_ids
:resource "aws_elb" "example" { # ... instances = module.servers.instance_ids }
有关引用命名值的更多信息,请参见Expressions。
例如,在这种情况下,您无法从模块中查询值作为代码
network_interface_id = "module.network.azurerm_network_interface.primary.id"
正确的表达式是module.<MODULE NAME>.<OUTPUT NAME>
。它是当前模块调用的子模块中指定的output
值的值。您应该像这样azurerm_network_interface.primary.id
那样从network
模块中查询network_interface_id = module.network.primary_nic_id
此外,由于模块块是在代码的根目录中声明的,因此您不能直接从子模块配置文件中引用它们。您可以使用输入变量将值从根模块传递到子模块。参见output values。
例如,在根目录的模块network
中,您像这样resource
调用模块devops_primary
的模块network
输出resource_group_name = module.resource.devops_primary
>
module "network" {
source = "./modules/network"
resource_group_name = module.resource.devops_primary
location = module.resource.location
virtual_network = "primaryvnet"
subnet = "primarysubnet"
address_space = ["192.168.0.0/16"]
...
}
在./modules.network目录中,您有
#Create Virtual Network in Primary Resource Group
resource "azurerm_virtual_network" "primary" {
name = var.virtual_network
resource_group_name = var.resource_group_name
address_space = var.address_space
location = var.location
}
variable "resource_group_name" {
}
variable "location" {
}
您可以按照上述规则重新编辑配置文件。有关更多示例,您可以搜索azurerm modules。