{Azure Virtual Network}IPレンジ拡張


https://learn.microsoft.com/ja-jp/azure/virtual-network/manage-virtual-network


-- 1. VPC、サブネット、VMインスタンス作成

 

cat <<-'EOF' > main.tf


terraform{
    required_providers{
        azurerm={
            source  = "hashicorp/azurerm"
            version = "=3.6.0"
        }
    }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}


resource "azurerm_resource_group" "rg9999999" {
  name = "rg9999999"
  location = "Japan East"

}


resource "azurerm_virtual_network" "vnet01" {
  name                = "vnet01"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name
}

resource "azurerm_subnet" "subnet01" {
  name                 = "subnet01"
  resource_group_name  = azurerm_resource_group.rg9999999.name
  virtual_network_name = azurerm_virtual_network.vnet01.name
  address_prefixes     = ["10.0.0.0/24"]
}
resource "azurerm_subnet" "GatewaySubnet" {
  name                 = "GatewaySubnet"
  resource_group_name  = azurerm_resource_group.rg9999999.name
  virtual_network_name = azurerm_virtual_network.vnet01.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_public_ip" "pip01" {
  name                = "pip01"
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name
  allocation_method   = "Dynamic"
}

resource "azurerm_network_security_group" "nsg01" {
  name                = "nsg01"
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name

  security_rule {
    name                       = "SSH"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
  security_rule {
    name                       = "OCI"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "*"
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "10.1.0.0/24"
    destination_address_prefix = "*"
  }
}


resource "azurerm_network_interface" "nic01" {
  name                = "nic01"
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet01.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.pip01.id
  }
}

resource "azurerm_network_interface_security_group_association" "nsg01_nic01" {
  network_interface_id      = azurerm_network_interface.nic01.id
  network_security_group_id = azurerm_network_security_group.nsg01.id
}


resource "azurerm_linux_virtual_machine" "vm01"{
  name = "vm01"
  resource_group_name = azurerm_resource_group.rg9999999.name
  location = azurerm_resource_group.rg9999999.location
  size = "Standard_B1ls"
  admin_username = "azureuser"

  network_interface_ids = [
      azurerm_network_interface.nic01.id,
  ]

  admin_ssh_key {
    username   = "azureuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }


  os_disk{
      caching = "ReadWrite"
      storage_account_type = "Standard_LRS"
  }

  source_image_reference{
      publisher = "Canonical"
      offer = "0001-com-ubuntu-server-focal"
      sku = "20_04-lts"
      version = "latest"
  }

}


output "public_ip_address" {
  value = azurerm_linux_virtual_machine.vm01.public_ip_address
}

 

EOF

terraform init
terraform fmt
terraform -version

terraform plan

terraform apply -auto-approve


# terraform destroy -auto-approve

 

-- 2. アドレス範囲を追加

az network vnet list

az network vnet show \
--resource-group rg9999999 \
--name vnet01


az network vnet update \
--resource-group rg9999999 \
--name vnet01 \
--address-prefixes 10.0.0.0/15

 


-- 3. クリーンアップ

az group list

az group delete \
--name rg9999999 \
--yes


az group delete \
--name NetworkWatcherRG \
--yes