-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
148 lines (125 loc) · 4.04 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
}
# Locate existing Packer Image
data "azurerm_image" "search" {
name = "raddit-base-ISO"
resource_group_name = var.hashirg
}
output "image_id" {
value = "/subscriptions/32cf0621-e31e-4501-b524-31a57248104a/resourceGroups/HashiDemo/providers/Microsoft.Compute/images/raddit-base-ISO"
}
# Create virtual network
resource "azurerm_virtual_network" "hashinet" {
name = "vpVnet"
address_space = ["10.0.0.0/16"]
location = var.hashiregion
resource_group_name = var.hashirg
tags = {
environment = "Terraform Demo"
}
}
# Create subnet
resource "azurerm_subnet" "hashisubnet" {
name = "vpSubnet"
resource_group_name = var.hashirg
virtual_network_name = azurerm_virtual_network.hashinet.name
address_prefixes = ["10.0.1.0/24"]
}
# Create Public IPs
resource "azurerm_public_ip" "hashipubip" {
name = "vpPublicIP"
location = var.hashiregion
resource_group_name = var.hashirg
allocation_method = "Dynamic"
}
# Create Network Security Group and Rule
resource "azurerm_network_security_group" "hashinsg" {
name = "vpNetworkSecurityGroup"
location = var.hashiregion
resource_group_name = var.hashirg
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "raddit"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "9292"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create Network Interface
resource "azurerm_network_interface" "hashinic" {
name = "vpNIC"
location = var.hashiregion
resource_group_name = var.hashirg
ip_configuration {
name = "vpNicConfiguration"
subnet_id = azurerm_subnet.hashisubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.hashipubip.id
}
depends_on = [
azurerm_subnet.hashisubnet
]
}
resource "azurerm_network_interface_security_group_association" "hashinicsgass" {
network_interface_id = azurerm_network_interface.hashinic.id
network_security_group_id = azurerm_network_security_group.hashinsg.id
}
# Create virtual machine
resource "azurerm_virtual_machine" "radditvm" {
name = "raddit-instance"
location = var.hashiregion
resource_group_name = var.hashirg
network_interface_ids = [azurerm_network_interface.hashinic.id]
vm_size = "Standard_DS1_v2"
delete_os_disk_on_termination = "true"
delete_data_disks_on_termination = "true"
storage_image_reference {
id = data.azurerm_image.search.id
}
storage_os_disk {
name = "raddit-instance"
caching = "ReadWrite"
managed_disk_type = "Standard_LRS"
create_option = "FromImage"
}
os_profile {
computer_name = "raddit-instance"
admin_username = var.user_name
admin_password = var.user_password
}
os_profile_linux_config {
disable_password_authentication = false
}
}
resource "time_sleep" "wait_2_min" {
depends_on = [azurerm_virtual_machine.radditvm]
create_duration = "120s"
}
output "public_ip" {
depends_on = [time_sleep.wait_2_min]
value = azurerm_public_ip.hashipubip.ip_address
}