My code to create a Terraform configuration.
Code:
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "West US"
}
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_security_group" "example" {
name = "example-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_network_security_rule" "example" {
name = "example-nsg-rule"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}
My error here is
Error: Error creating/updating Network Security Rule
"example-nsg-rule" (Resource Group "example-resource-group"):
network.SecurityRulesClient#CreateOrUpdate: Failure sending request:
StatusCode=400 — Original Error: Code="InvalidRequestFormat"
Message="Cannot parse the request." Details=[{"Message":"The request
body is invalid. Please provide a valid request body."}]
I suspect there might be an issue with the configuration or a missing parameter.
Could someone please review the code and provide insights into the error?
2
Answers
OK, so normally in a single terraform file the order of declaration is not important, but before you create a security rule make sure NSG and resource group exist or is declared before.
I also saw an alert in their documentation regarding this:
Terraform currently provides both a standalone Network Security Rule resource, and allows for Network Security Rules to be defined in-line within the Network Security Group resource. At this time you cannot use a Network Security Group with in-line Network Security Rules in conjunction with any Network Security Rule resources. Doing so will cause a conflict of rule settings and will overwrite rules.
Based on the error message provided, there might be an issue with the request body for the network security rule. Upon reviewing the
destination_Address_prefixes
parameter in theazurerm_security_rule resource
block this parameter specifies the destination IP address range for the network security rule.To overcome this we need to specify the destination_address_prefixes parameter with a valid IP address range.
My terraform configuration:
Output