-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
211 lines (188 loc) · 7.36 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
locals {
tags = {
environment = "${var.environment}"
}
}
provider "azurerm" {
features {}
}
# Using data as a reference because resource group already exists
# Data is used to read existing data from external systems.
# Unlike resources, data sources do not create, update, or delete infrastructure objects. They only provide information for use in the configuration.
data "azurerm_resource_group" "main" {
name = "Azuredevops"
}
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/22"]
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
tags = local.tags
}
resource "azurerm_subnet" "main" {
name = "${var.prefix}-subnet"
resource_group_name = data.azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}
# Network Security Group
resource "azurerm_network_security_group" "main" {
name = "${var.prefix}-nsg"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
# Allow traffic from other VMs within the same subnet
security_rule {
name = "AllowFromSubnet"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "VirtualNetwork"
destination_address_prefix = "VirtualNetwork"
}
# Deny direct access from the internet
security_rule {
name = "DenyInternetAccess"
priority = 200
direction = "Inbound"
access = "Deny"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "Internet"
}
tags = local.tags
}
# Network Interface
resource "azurerm_network_interface" "main" {
count = var.number_of_vms
name = "${var.prefix}-nic-${count.index}"
resource_group_name = data.azurerm_resource_group.main.name
location = data.azurerm_resource_group.main.location
tags = local.tags
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.main.id
private_ip_address_allocation = "Dynamic"
}
}
# Connect the security group to the network interface
# https://learn.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-terraform?tabs=azure-cli
resource "azurerm_network_interface_security_group_association" "main" {
count = var.number_of_vms
network_interface_id = azurerm_network_interface.main[count.index].id
network_security_group_id = azurerm_network_security_group.main.id
}
# Public IP
resource "azurerm_public_ip" "main" {
name = "${var.prefix}-pip"
resource_group_name = data.azurerm_resource_group.main.name
location = data.azurerm_resource_group.main.location
allocation_method = "Static"
sku = "Standard"
tags = local.tags
}
# Load Balancer
resource "azurerm_lb" "main" {
name = "${var.prefix}-lb"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
sku = "Standard"
tags = local.tags
frontend_ip_configuration {
name = "${var.prefix}-PublicIPAddress"
public_ip_address_id = azurerm_public_ip.main.id
}
}
resource "azurerm_lb_backend_address_pool" "main" {
name = "${var.prefix}-lb-backend"
loadbalancer_id = azurerm_lb.main.id
}
resource "azurerm_lb_probe" "main" {
loadbalancer_id = azurerm_lb.main.id
name = "${var.prefix}-test-probe"
port = 80
}
resource "azurerm_lb_rule" "main" {
loadbalancer_id = azurerm_lb.main.id
name = "${var.prefix}-lb-rule"
protocol = "Tcp"
frontend_port = 80
backend_port = 80
disable_outbound_snat = true
frontend_ip_configuration_name = "${var.prefix}-PublicIPAddress"
probe_id = azurerm_lb_probe.main.id
backend_address_pool_ids = [azurerm_lb_backend_address_pool.main.id]
}
# Associate Network Interface to the Backend Pool of the Load Balancer
resource "azurerm_network_interface_backend_address_pool_association" "main" {
count = var.number_of_vms
network_interface_id = azurerm_network_interface.main[count.index].id
ip_configuration_name = azurerm_network_interface.main[count.index].ip_configuration[0].name
backend_address_pool_id = azurerm_lb_backend_address_pool.main.id
}
# Availability Set for Virtual Machines.
resource "azurerm_availability_set" "main" {
name = "${var.prefix}-aset"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
tags = local.tags
}
# Create managed disks
resource "azurerm_managed_disk" "main" {
count = var.number_of_vms
name = "${var.prefix}-managed_disk-${count.index}"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
tags = local.tags
}
data "azurerm_image" "main" {
name = var.image
resource_group_name = data.azurerm_resource_group.main.name
}
resource "azurerm_linux_virtual_machine" "main" {
count = var.number_of_vms
name = "${var.prefix}-vm-${count.index}"
resource_group_name = data.azurerm_resource_group.main.name
location = data.azurerm_resource_group.main.location
size = "Standard_D2s_v3"
admin_username = var.admin_username
admin_password = var.admin_password
disable_password_authentication = false
network_interface_ids = [azurerm_network_interface.main[count.index].id]
# The ID of the Image which this Virtual Machine should be created from
source_image_id = data.azurerm_image.main.id
# Specifies the ID of the Availability Set in which the Virtual Machine should exist.
availability_set_id = azurerm_availability_set.main.id
tags = local.tags
os_disk {
name = "${var.prefix}-osdisk-${count.index}"
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}
}
resource "azurerm_virtual_machine_data_disk_attachment" "main" {
count = var.number_of_vms
managed_disk_id = azurerm_managed_disk.main[count.index].id
virtual_machine_id = azurerm_linux_virtual_machine.main[count.index].id
lun = "10"
caching = "ReadWrite"
}