-
Notifications
You must be signed in to change notification settings - Fork 8
/
azure-main.tf
151 lines (132 loc) · 4.96 KB
/
azure-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
149
150
151
#Configure the Azure Provider
provider "azurerm" {
environment = "public"
subscription_id = var.azure_subscription_id
client_id = var.azure_client_id
client_secret = var.azure_client_secret
tenant_id = var.azure_tenant_id
}
#Create Resource Group
resource "azurerm_resource_group" "azure-rg" {
name = "${var.app_name}-${var.app_environment}-rg"
location = var.rg_location
}
#Create a virtual network
resource "azurerm_virtual_network" "azure-vnet" {
name = "${var.app_name}-${var.app_environment}-vnet"
resource_group_name = azurerm_resource_group.azure-rg.name
location = var.rg_location
address_space = [var.azure_vnet_cidr]
tags = {
environment = var.app_environment
}
}
#Create a subnet
resource "azurerm_subnet" "azure-subnet" {
name = "${var.app_name}-${var.app_environment}-subnet"
resource_group_name = azurerm_resource_group.azure-rg.name
virtual_network_name = azurerm_virtual_network.azure-vnet.name
address_prefix = var.azure_subnet_cidr
}
#Create Security Group to access Web Server
resource "azurerm_network_security_group" "azure-web-nsg" {
name = "${var.app_name}-${var.app_environment}-web-nsg"
location = azurerm_resource_group.azure-rg.location
resource_group_name = azurerm_resource_group.azure-rg.name
security_rule {
name = "AllowHTTP"
description = "Allow HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
security_rule {
name = "AllowSSH"
description = "Allow SSH"
priority = 101
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
tags = {
environment = var.app_environment
}
}
#Associate the Web NSG with the subnet
resource "azurerm_subnet_network_security_group_association" "azure-web-nsg-association" {
subnet_id = azurerm_subnet.azure-subnet.id
network_security_group_id = azurerm_network_security_group.azure-web-nsg.id
}
#Get a Static Public IP
resource "azurerm_public_ip" "azure-web-ip" {
name = "${var.app_name}-${var.app_environment}-web-ip"
location = azurerm_resource_group.azure-rg.location
resource_group_name = azurerm_resource_group.azure-rg.name
allocation_method = "Static"
tags = {
environment = var.app_environment
}
}
#Create Network Card for Web Server VM
resource "azurerm_network_interface" "azure-web-nic" {
name = "${var.app_name}-${var.app_environment}-web-nic"
location = azurerm_resource_group.azure-rg.location
resource_group_name = azurerm_resource_group.azure-rg.name
network_security_group_id = azurerm_network_security_group.azure-web-nsg.id
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.azure-subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.azure-web-ip.id
}
tags = {
environment = var.app_environment
}
}
# Create web server vm
resource "azurerm_virtual_machine" "azure-web-vm" {
name = "${var.app_name}-${var.app_environment}-web-vm"
location = azurerm_resource_group.azure-rg.location
resource_group_name = azurerm_resource_group.azure-rg.name
network_interface_ids = [azurerm_network_interface.azure-web-nic.id]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = var.ubuntu-linux-publisher
offer = var.ubuntu-linux-offer
sku = var.ubuntu-linux-18-sku
version = "latest"
}
storage_os_disk {
name = "${var.app_name}-${var.app_environment}-web-vm-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = var.linux_vm_hostname
admin_username = var.linux_admin_user
admin_password = var.linux_admin_password
custom_data = file("azure-user-data.sh")
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
environment = var.app_environment
}
}
#Output
output "external-ip-azure-web-server" {
value = azurerm_public_ip.azure-web-ip.ip_address
}