Skip to content

Commit a460f58

Browse files
committed
Add a virtual machine
1 parent f9a6563 commit a460f58

8 files changed

+154
-10
lines changed

.terraform.lock.hcl

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

computes.tf

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
resource "azurerm_linux_virtual_machine" "vm" {
2+
name = "tutorial-vm"
3+
location = azurerm_resource_group.rg.location
4+
resource_group_name = azurerm_resource_group.rg.name
5+
size = "Standard_D2ads_v5"
6+
admin_username = var.vm_admin_username
7+
network_interface_ids = [
8+
azurerm_network_interface.nic.id
9+
]
10+
11+
admin_ssh_key {
12+
username = var.vm_admin_username
13+
public_key = tls_private_key.vm_ssh_pk.public_key_openssh
14+
}
15+
16+
os_disk {
17+
caching = "ReadWrite"
18+
storage_account_type = "Standard_LRS"
19+
}
20+
21+
source_image_reference {
22+
publisher = "Canonical"
23+
offer = "UbuntuServer"
24+
sku = "18.04-LTS"
25+
version = "latest"
26+
}
27+
28+
computer_name = "tutorial-vm"
29+
disable_password_authentication = true
30+
}
31+
32+
# RSA key of size 4096 bits
33+
resource "tls_private_key" "vm_ssh_pk" {
34+
algorithm = "RSA"
35+
rsa_bits = 4096
36+
}

networks.tf

+30-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,40 @@ resource "azurerm_virtual_network" "vnet" {
44
location = azurerm_resource_group.rg.location
55
resource_group_name = azurerm_resource_group.rg.name
66
address_space = ["10.0.0.0/16"]
7+
tags = var.tags
8+
}
79

8-
subnet {
9-
name = "subnet1"
10-
address_prefix = "10.0.1.0/24"
11-
}
12-
13-
tags = var.tags
10+
resource "azurerm_subnet" "subnet" {
11+
name = "tutorial-subnet"
12+
resource_group_name = azurerm_resource_group.rg.name
13+
virtual_network_name = azurerm_virtual_network.vnet.name
14+
address_prefixes = ["10.0.1.0/24"]
1415
}
1516

16-
resource "azurerm_public_ip" "vm-public-ip" {
17+
resource "azurerm_public_ip" "vm_public_ip" {
1718
name = "vm-public-ip"
18-
resource_group_name = azurerm_resource_group.rg.name
1919
location = azurerm_resource_group.rg.location
20-
allocation_method = "Static"
20+
resource_group_name = azurerm_resource_group.rg.name
21+
allocation_method = "Dynamic"
2122
tags = var.tags
2223
}
24+
25+
resource "azurerm_network_interface" "nic" {
26+
name = "vm-nic"
27+
location = azurerm_resource_group.rg.location
28+
resource_group_name = azurerm_resource_group.rg.name
29+
30+
ip_configuration {
31+
name = "internal"
32+
subnet_id = azurerm_subnet.subnet.id
33+
private_ip_address_allocation = "Dynamic"
34+
public_ip_address_id = azurerm_public_ip.vm_public_ip.id
35+
}
36+
37+
tags = var.tags
38+
}
39+
40+
resource "azurerm_network_interface_security_group_association" "nic_nsg" {
41+
network_interface_id = azurerm_network_interface.nic.id
42+
network_security_group_id = azurerm_network_security_group.nsg.id
43+
}

outputs.tf

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
output "resource_group_id" {
22
value = azurerm_resource_group.rg.id
33
}
4+
5+
output "tls_private_key" {
6+
value = tls_private_key.vm_ssh_pk.private_key_pem
7+
sensitive = true
8+
}
9+
10+
output "vm_public_ip" {
11+
value = azurerm_public_ip.vm_public_ip.ip_address
12+
}

resource-groups.tf

+20
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,23 @@ resource "azurerm_resource_group" "rg" {
33
location = var.location
44
tags = var.tags
55
}
6+
7+
resource "azurerm_network_security_group" "nsg" {
8+
name = "tutorial-network-security-group"
9+
location = azurerm_resource_group.rg.location
10+
resource_group_name = azurerm_resource_group.rg.name
11+
12+
security_rule {
13+
name = "allow-ssh"
14+
priority = 100
15+
direction = "Inbound"
16+
access = "Allow"
17+
protocol = "Tcp"
18+
source_port_range = "*"
19+
destination_port_range = "22"
20+
source_address_prefix = "*"
21+
destination_address_prefix = "*"
22+
}
23+
24+
tags = var.tags
25+
}

storage.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "random_id" "rg_storage_account" {
2+
keepers = {
3+
resource_group = azurerm_resource_group.rg.name
4+
}
5+
byte_length = 8
6+
}
7+
resource "azurerm_storage_account" "storage_account" {
8+
name = "storage${random_id.rg_storage_account.hex}"
9+
location = azurerm_resource_group.rg.location
10+
resource_group_name = azurerm_resource_group.rg.name
11+
account_tier = "Standard"
12+
account_replication_type = "LRS"
13+
tags = var.tags
14+
}

terraform.tfvars

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
resource_group_name = "my-rg"
2-
location = "westus"
2+
location = "centralus"
33
tags = {
44
environment = "dev"
55
team = "DevOps"
66
}
7+
vm_admin_username = "moazshorbagy"

variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ variable "tags" {
1515
team = "DevOps"
1616
}
1717
}
18+
19+
variable "vm_admin_username" {
20+
type = string
21+
default = "adminuser"
22+
}

0 commit comments

Comments
 (0)