-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
114 lines (98 loc) · 3.24 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
# 1. Declaring Azure Provider
provider "azurerm" {
features {}
}
# 2. Creating Resource Group for the machine.
resource "azurerm_resource_group" "rg-demoapp" {
name = "rg-demoapp"
location = "West US 3"
}
# 3. Deploy Virtual Network
resource "azurerm_virtual_network" "vn-demoapp" {
name = "vn-demoapp"
address_space = ["10.30.0.0/24"]
location = azurerm_resource_group.rg-demoapp.location
resource_group_name = azurerm_resource_group.rg-demoapp.name
}
# 4. Create Subnet
resource "azurerm_subnet" "subnet-demoapp" {
name = "subnet-demoapp"
resource_group_name = azurerm_resource_group.rg-demoapp.name
virtual_network_name = azurerm_virtual_network.vn-demoapp.name
address_prefixes = ["10.30.0.0/28"]
}
# 5. Deploy Public IP DemoAPP
resource "azurerm_public_ip" "vm-demoapp-pip" {
name = "vm-demoapp-pip"
location = azurerm_resource_group.rg-demoapp.location
resource_group_name = azurerm_resource_group.rg-demoapp.name
allocation_method = "Dynamic"
domain_name_label = "demo-app-yhernandez"
}
# 6. Deploy Network Interface
resource "azurerm_network_interface" "vm-demoapp-nic-01" {
name = "vm-demoapp-nic-01"
location = azurerm_resource_group.rg-demoapp.location
resource_group_name = azurerm_resource_group.rg-demoapp.name
depends_on = [
azurerm_public_ip.vm-demoapp-pip
]
ip_configuration {
name = "ipconfig1"
subnet_id = azurerm_subnet.subnet-demoapp.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.vm-demoapp-pip.id
}
}
# 7. Wait the allocation of Public IP
resource "time_sleep" "wait_90_seconds" {
depends_on = [azurerm_network_interface.vm-demoapp-nic-01]
create_duration = "90s"
}
# 8. Deploy Virtual Machine vm-demoapp
resource "azurerm_linux_virtual_machine" "vm-demoapp" {
name = "vm-demoapp"
location = azurerm_resource_group.rg-demoapp.location
resource_group_name = azurerm_resource_group.rg-demoapp.name
size = "Standard_D2as_v4"
admin_username = "yhernandez"
disable_password_authentication = true
network_interface_ids = [
azurerm_network_interface.vm-demoapp-nic-01.id,
]
depends_on = [time_sleep.wait_90_seconds]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
admin_ssh_key {
username = "yhernandez"
public_key = file("~/.ssh/id_rsa.pub")
}
source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-focal"
sku = "20_04-lts"
version = "20.04.202209200"
}
}
# 9. Wait before provisioning
resource "time_sleep" "wait_45_seconds" {
depends_on = [azurerm_linux_virtual_machine.vm-demoapp]
create_duration = "45s"
}
# 10. Provisioning vm-demo
resource "null_resource" "vm-demo-provisioning" {
depends_on = [time_sleep.wait_45_seconds]
provisioner "local-exec" {
command = "/usr/bin/ansible-playbook -u yhernandez -i 'demo-app-yhernandez.westus3.cloudapp.azure.com,' provisioning.yml"
}
}