1

I have taken reference of github code.Please find below URL

https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/vm-from-managed-image

I modified the scripts and executed terraform init. I received below error.

Error reading config for azurerm_network_interface[main]: parse error at 1:18: expected ")" but found "."[0m

My Script :

    # Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxxxxxx"
    client_id       = "xxxxxxxx"
    client_secret   = "xxxxxxxx"
    tenant_id       = "xxxxxxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZLXSPTDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/4f5c9f2a-3584-4bbd-a26e-bbf69ffbfbe6/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXSPTDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-TEST"
  location = "eastus"
}

# Create a Virtual Network within the Resource Group
resource "azurerm_virtual_network" "main" {
  name                = "RG-Vnet"
  address_space       = ["10.100.0.0/16"]
  resource_group_name = "${azurerm_resource_group.main.name}"
  location            = "${azurerm_resource_group.main.location}"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Web-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  security_rule {
    name                       = "allow_SSH"
    description                = "Allow SSH access"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "myNIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "primary"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.1.8/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXSPTDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXSPTDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "admin"
    admin_password = "admin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}
8
  • 1
    This looks like a simple typo to me. You need quotes around the IP range on "${cidrhost(10.100.1.8/24, 4)}". So it should be "${cidrhost("10.100.1.8/24", 4)}" instead. Commented Feb 20, 2019 at 13:08
  • Thanks. But i am receiving two new errors. 1. azurerm_subnet.internal: 1 error(s) occurred: * azurerm_subnet.internal: Error Creating/Updating Subnet "RG" (Virtual Network "RG-Vnet" / Resource Group "RG"): network.SubnetsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="NetcfgInvalidSubnet" Message="Subnet 'RG-out' is not valid in virtual network 'RG-Vnet'." Details=[] Commented Feb 21, 2019 at 8:03
  • 2. azurerm_virtual_network.main: 1 error(s) occurred: * azurerm_virtual_network.main: Error Creating/Updating Virtual Network "RG-Vnet" (Resource Group "RG-TEST"): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet RG-Terraform-snet-out is in use by /subscriptions/4f5c9f2a-3584-4bbd-a26e-bbf69ffbfbe6/resourceGroups/RG-OPT-QA-TEST/providers/Microsoft.Network/networkInterfaces/myNIC/ipConfigurations/myNicConfiguration and cannot be deleted." Details=[] Commented Feb 21, 2019 at 8:04
  • While my answer helps you solve the problem, why not accept it? Commented Feb 27, 2019 at 6:12
  • When i tried to create vm in new firewall i faced an error. azurerm_subnet.internal: Error Creating/Updating Subnet "VNET-PFSENSE-TEST/SNET-IN" (Virtual Network "VNET-PFSENSE-TEST" / Resource Group "RG-PF-TEST"): network.SubnetsClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="Failed" Message="The async operation failed." AdditionalInfo=[{"Message":"No HTTP resource was found that matches the request URI Commented Feb 27, 2019 at 7:25

2 Answers 2

7

Below script is working fine

# Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxx"
    client_id       = "xxxx"
    client_secret   = "xxxx"
    tenant_id       = "xxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/xxxxxx/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-OPT-QA-TEST"
  location = "eastus"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "RG-OPT-QA-Vnet"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Dev-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  security_rule {
    name                       = "allow_SSH"
    description                = "Allow SSH access"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "NIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "nicconfig"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.2.16/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "devopsadmin"
    admin_password = "Cssladmin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Well, with the errors that in your comment, I think you should set the subnet like this:

resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.1.0/24"
}

And the error with the virtual network, I do not see the virtual network with the name "RG-Vnet" in the code as the error said. So you should take a check if everything is all right in your code as you want.

To create an Azure VM from the image in Azure Marketplace, you can follow the tutorial Create a complete Linux virtual machine infrastructure in Azure with Terraform. You do not need to create an image resource in your Terraform code. Just set it like this in the resource azurerm_virtual_machine:

storage_os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Premium_LRS"
    } 

Also, when you refer to other resources in the same code, you should do it like this:

virtual_network_name = "${azurerm_virtual_network.main.name}"

not just with the string name as "RG-Vnet", it's not the correct way.

17 Comments

Thank you for the response.Now one error is resolved. Still one more error i am receiving.
@MohanKanth Any more questions? Or if it's helpful you can accept it as the answer.
1 error(s) occurred: * azurerm_virtual_network.main: 1 error(s) occurred: * azurerm_virtual_network.main: Error Creating/Updating Virtual Network "RG-Vnet" (Resource Group "RG"): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet RG-Terraform-snet-out is in use by /subscriptions/xxxxxxxx/resourceGroups/RG-OPT-QA-TEST/providers/Microsoft.Network/networkInterfaces/myNIC/ipConfigurations/myNicConfiguration and cannot be deleted." Details=[]
@MohanKanth As I said in the answer, I cannot find the virtual network named RG-Vnet in your code. Do you show all the code?
@MohanKanth Also, there is no problem with your virtual network that I can see your code if you change the setting of your subnet as I suggest.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.