-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
176 lines (143 loc) · 5.29 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
provider "azurerm" {
version = "=2.20.0"
features {}
}
locals {
custom_data = <<CUSTOM_DATA
#!/bin/bash
wget https://golang.org/dl/go1.15.2.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
echo 'PATH=$PATH:/usr/local/go/bin' >> /home/example/.profile
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo systemctl restart docker
sudo docker pull frrouting/frr:v7.4.0
sudo docker pull networkop/cloudroutesync
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo curl -L https://raw.githubusercontent.com/networkop/cloudroutesync/main/demo-docker/bgpd.conf -o /home/example/bgpd.conf
sudo curl -L https://raw.githubusercontent.com/networkop/cloudroutesync/main/demo-docker/daemons -o /home/example/daemons
sudo curl -L https://raw.githubusercontent.com/networkop/cloudroutesync/main/demo-docker/docker-compose.yml -o /home/example/docker-compose.yml
CUSTOM_DATA
}
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "West Europe"
}
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "external_router" {
name = "${var.prefix}-public-router-ip"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
allocation_method = "Static"
}
resource "azurerm_network_interface" "router" {
name = "${var.prefix}-router-nic"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
enable_ip_forwarding = true
ip_configuration {
name = "router-nic"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.external_router.id
}
}
resource "azurerm_linux_virtual_machine" "router" {
name = "${var.prefix}-router-vm"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.router.id]
size = "Standard_B1ms"
admin_username = var.username
admin_ssh_key {
username = var.username
public_key = file(var.pub_ssh_key_path)
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
offer = "UbuntuServer"
publisher = "Canonical"
sku = "16.04-LTS"
version = "latest"
}
identity {
type = "SystemAssigned"
}
custom_data = base64encode(local.custom_data)
}
resource "azurerm_role_assignment" "network" {
scope = azurerm_resource_group.main.id
role_definition_name = "Network Contributor"
principal_id = azurerm_linux_virtual_machine.router.identity[0].principal_id
}
######################
# Non-router VM #
######################
resource "azurerm_public_ip" "external" {
name = "${var.prefix}-public-ip"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
allocation_method = "Static"
}
resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
enable_ip_forwarding = true
ip_configuration {
name = "nic"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.external.id
}
}
resource "azurerm_linux_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.main.id]
size = "Standard_B1ms"
admin_username = var.username
admin_ssh_key {
username = var.username
public_key = file(var.pub_ssh_key_path)
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
offer = "UbuntuServer"
publisher = "Canonical"
sku = "16.04-LTS"
version = "latest"
}
custom_data = base64encode(local.custom_data)
}