solutions/chapter-1/terraform-deployment/main.tf (91 lines of code) (raw):

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.111.0" } } } # Configure the Microsoft Azure Provider provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "example-resources" location = "West Europe" } resource "azurerm_virtual_network" "example_vnet" { 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_subnet" { name = "example-subnet" resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example_vnet.name address_prefixes = ["10.0.1.0/24"] } resource "azurerm_network_interface" "example_nic" { name = "example-nic" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name ip_configuration { name = "internal" subnet_id = azurerm_subnet.example_subnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.example_public_ip.id } } resource "azurerm_public_ip" "example_public_ip" { name = "example-publicip" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name allocation_method = "Dynamic" } resource "azurerm_linux_virtual_machine" "example_vm" { name = "example-vm" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location size = "Standard_B1s" admin_username = "YWRtaW51c2VyC" network_interface_ids = [ azurerm_network_interface.example_nic.id, ] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } source_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } admin_ssh_key { username = "YWRtaW51c2VyC" public_key = file("~/.ssh/id_rsa.pub") } disable_password_authentication = true } resource "azurerm_network_security_group" "example_nsg" { name = "example-nsg" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name } resource "azurerm_network_security_rule" "allow_ssh" { name = "SSH" priority = 1000 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_nsg.name } resource "azurerm_network_interface_security_group_association" "example" { network_interface_id = azurerm_network_interface.example_nic.id network_security_group_id = azurerm_network_security_group.example_nsg.id }