-
Notifications
You must be signed in to change notification settings - Fork 1
/
rg.tf
151 lines (139 loc) · 4.41 KB
/
rg.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 Microsoft Azure Provider
provider "azurerm" {
features {}
}
# Create Resource Group
resource "azurerm_resource_group" "rg" {
name = "${var.resource-prefix}-rg"
location = var.location
tags = {
stage = var.tag-stage
}
}
# Create Virtual network
resource "azurerm_virtual_network" "vnet" {
name = "${var.resource-prefix}-vnet"
address_space = ["10.0.0.0/16"]
location = var.location
resource_group_name = azurerm_resource_group.rg.name
tags = {
stage = var.tag-stage
}
}
# Create Subnets
resource "azurerm_subnet" "devsnet" {
name = "${var.resource-prefix}-vnet-dev-snet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
# Create Public IP
resource "azurerm_public_ip" "cdbpip" {
name = "${var.resource-prefix}-cdbpip"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Standard"
allocation_method = "Static"
tags = {
stage = var.tag-stage
}
}
# Network Security Group
resource "azurerm_network_security_group" "devnsg" {
name = "${var.resource-prefix}-dev-nsg"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefixes = var.ssh-allowlist
destination_address_prefix = "*"
}
tags = {
stage = var.tag-stage
}
}
# Create Network interface with Subnet and Public IP
resource "azurerm_network_interface" "cdbnic" {
name = "${var.resource-prefix}-cdbnic"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
enable_accelerated_networking = var.cdbvm-nic-accelerated-networking
ip_configuration {
name = "cdbnic-ipconfig"
subnet_id = azurerm_subnet.devsnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.cdbpip.id
}
tags = {
stage = var.tag-stage
}
}
resource "azurerm_network_interface_security_group_association" "cdbnicnsg" {
network_interface_id = azurerm_network_interface.cdbnic.id
network_security_group_id = azurerm_network_security_group.devnsg.id
}
# Create storage account for boot diagnostics
resource "azurerm_storage_account" "cdbstorage" {
name = "${var.storage-prefix}cdbstor"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
stage = var.tag-stage
}
}
# Create a SSH key
resource "tls_private_key" "sshkey" {
algorithm = "RSA"
rsa_bits = 4096
}
# Create virtual machine
resource "azurerm_linux_virtual_machine" "cdbvm" {
name = "${var.resource-prefix}-cdbvm"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.cdbnic.id]
size = var.cdbvm-size
computer_name = "${var.cdbvm-comp-name}"
admin_username = var.cdbvm-username
disable_password_authentication = true
os_disk {
name = "${var.resource-prefix}-cdbvm-osdisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
disk_size_gb = "128"
}
source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-focal"
sku = "20_04-lts"
version = "latest"
}
admin_ssh_key {
username = var.cdbvm-username
public_key = tls_private_key.sshkey.public_key_openssh
}
boot_diagnostics {
storage_account_uri = azurerm_storage_account.cdbstorage.primary_blob_endpoint
}
tags = {
stage = var.tag-stage
}
}
output "sshpvk" {
value = tls_private_key.sshkey.private_key_pem
description = "SSH private key"
sensitive = true
}
output "cdbpip" {
value = azurerm_public_ip.cdbpip.ip_address
description = "Public IP Address"
sensitive = false
}