forked from krishcool/test1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.tf
150 lines (128 loc) · 4.38 KB
/
webserver.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
# Set the Azure Provider source and version being used
terraform {
required_version = ">= 0.14"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.1.0"
}
}
}
# Configure the Microsoft Azure provider
provider "azurerm" {
features {}
}
# Create a Resource Group if it doesn’t exist
resource "azurerm_resource_group" "tfexample" {
name = "my-terraform-rg"
location = "West Europe"
}
# Create a Virtual Network
resource "azurerm_virtual_network" "tfexample" {
name = "my-terraform-vnet"
location = azurerm_resource_group.tfexample.location
resource_group_name = azurerm_resource_group.tfexample.name
address_space = ["10.0.0.0/16"]
tags = {
environment = "my-terraform-env"
}
}
# Create a Subnet in the Virtual Network
resource "azurerm_subnet" "tfexample" {
name = "my-terraform-subnet"
resource_group_name = azurerm_resource_group.tfexample.name
virtual_network_name = azurerm_virtual_network.tfexample.name
address_prefixes = ["10.0.2.0/24"]
}
# Create a Public IP
resource "azurerm_public_ip" "tfexample" {
name = "my-terraform-public-ip"
location = azurerm_resource_group.tfexample.location
resource_group_name = azurerm_resource_group.tfexample.name
allocation_method = "Static"
tags = {
environment = "my-terraform-env"
}
}
# Create a Network Security Group and rule
resource "azurerm_network_security_group" "tfexample" {
name = "my-terraform-nsg"
location = azurerm_resource_group.tfexample.location
resource_group_name = azurerm_resource_group.tfexample.name
security_rule {
name = "HTTP"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = var.server_port
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "my-terraform-env"
}
}
# Create a Network Interface
resource "azurerm_network_interface" "tfexample" {
name = "my-terraform-nic"
location = azurerm_resource_group.tfexample.location
resource_group_name = azurerm_resource_group.tfexample.name
ip_configuration {
name = "my-terraform-nic-ip"
subnet_id = azurerm_subnet.tfexample.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.tfexample.id
}
tags = {
environment = "my-terraform-env"
}
}
# Create a Network Interface Security Group association
resource "azurerm_network_interface_security_group_association" "tfexample" {
network_interface_id = azurerm_network_interface.tfexample.id
network_security_group_id = azurerm_network_security_group.tfexample.id
}
# Create a Virtual Machine
resource "azurerm_linux_virtual_machine" "tfexample" {
name = "my-terraform-vm"
location = azurerm_resource_group.tfexample.location
resource_group_name = azurerm_resource_group.tfexample.name
network_interface_ids = [azurerm_network_interface.tfexample.id]
size = "Standard_DS1_v2"
computer_name = "myvm"
admin_username = "azureuser"
admin_password = "Password1234!"
disable_password_authentication = false
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_disk {
name = "my-terraform-os-disk"
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}
tags = {
environment = "my-terraform-env"
}
}
# Configurate to run automated tasks in the VM start-up
resource "azurerm_virtual_machine_extension" "tfexample" {
name = "hostname"
virtual_machine_id = azurerm_linux_virtual_machine.tfexample.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.1"
settings = <<SETTINGS
{
"commandToExecute": "echo 'Hello, World' > index.html ; nohup busybox httpd -f -p ${var.server_port} &"
}
SETTINGS
tags = {
environment = "my-terraform-env"
}
}