You are creating a Linux VM using a custom image from your Compute Gallery, and you provided the SKU, publisher, and offer using your own naming convention, as shown in the screenshot. I also provided similar names accordingly.
Terraform main.tf file:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.0.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "myresource19981" {
name = "1998myresource-resources"
location = "East Us"
}
resource "azurerm_virtual_network" "virtualnetwork" {
name = "virtualnetwork-vnet1"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.myresource19981.location
resource_group_name = azurerm_resource_group.myresource19981.name
}
resource "azurerm_subnet" "example" {
name = "example-subnet1"
resource_group_name = azurerm_resource_group.myresource19981.name
virtual_network_name = azurerm_virtual_network.virtualnetwork.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_interface" "example" {
name = "example-nic1"
location = azurerm_resource_group.myresource19981.location
resource_group_name = azurerm_resource_group.myresource19981.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "example" {
name = "example-vm1"
resource_group_name = azurerm_resource_group.myresource19981.name
location = azurerm_resource_group.myresource19981.location
size = "Standard_D2s_v3"
network_interface_ids = [
azurerm_network_interface.example.id,
]
admin_username = "adminstrator"
# Recommended: Use SSH key authentication
admin_ssh_key {
username = "adminstrator"
public_key = file("C:\\Users\\username\\.ssh\\id_rsa.pub") # Path to your SSH public key
}
source_image_id = "/subscriptions/subscriptions-id/resourceGroups/vm-win/providers/Microsoft.Compute/galleries/terraform/images/rocky/versions/9.4.0"
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
name = "example-osdisk"
}
}
I successfully created the main.tf
Terraform file using the custom image. Whenever you execute the Terraform file, please make sure to validate it and check if the format is correct. Also, remember to run terraform plan
before applying.
Please find the screenshot of the VM created using a custom image through Terraform.
If you found information is helpful, please click "Upvote" on the post to let us know.
If you have any further queries feel free to ask us we are happy to assist you.
Thank You.