-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
158 lines (134 loc) · 4.45 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
terraform {
required_providers {
banyan = {
source = "banyansecurity/banyan"
version = "0.6.3"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0.2"
}
time = {
source = "hashicorp/time"
version = "0.7.2"
}
}
}
provider "azurerm" {
features {}
}
provider "banyan" {
api_token = var.banyan_api_key
host = var.banyan_host
}
locals {
tags = merge(var.tags, {
Provider = "Banyan"
Name = "${var.connector_name}"
})
}
resource "banyan_api_key" "connector_key" {
name = var.connector_name
description = var.connector_name
scope = "satellite"
}
resource "banyan_connector" "connector_spec" {
name = var.connector_name
satellite_api_key_id = banyan_api_key.connector_key.id
}
# wait for a connector to be unhealthy before the API objects can be deleted
resource "time_sleep" "connector_health_check" {
depends_on = [banyan_connector.connector_spec]
destroy_duration = "5m"
}
resource "azurerm_network_interface" "connector_nic" {
name = "${var.name_prefix}-nic-connector"
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
}
}
locals {
init_script = <<INIT_SCRIPT
#!/bin/bash
# use the latest, or set the specific version
LATEST_VER=$(curl -sI https://www.banyanops.com/netting/connector/latest | awk '/Location:/ {print $2}' | grep -Po '(?<=connector-)\S+(?=.tar.gz)')
INPUT_VER="${var.package_version}"
VER="$LATEST_VER" && [[ ! -z "$INPUT_VAR" ]] && VER="$INPUT_VER"
# create folder for the Tarball
mkdir -p /opt/banyan-packages
cd /opt/banyan-packages
# download and unzip the files
wget https://www.banyanops.com/netting/connector-$VER.tar.gz
tar zxf connector-$VER.tar.gz
cd connector-$VER
# create the config file
echo 'command_center_url: ${var.banyan_host}' > connector-config.yaml
echo 'api_key_secret: ${banyan_api_key.connector_key.secret}' >> connector-config.yaml
echo 'connector_name: ${var.connector_name}' >> connector-config.yaml
./setup-connector.sh
echo 'Port 2222' >> /etc/ssh/sshd_config && /bin/systemctl restart sshd.service
INIT_SCRIPT
}
resource "azurerm_linux_virtual_machine" "connector_vm" {
depends_on = [time_sleep.connector_health_check]
name = "${var.name_prefix}-connector"
location = var.location
resource_group_name = var.resource_group_name
size = var.instance_size
admin_username = "adminuser"
tags = local.tags
network_interface_ids = [
azurerm_network_interface.connector_nic.id,
]
admin_ssh_key {
username = "adminuser"
public_key = file(var.ssh_key_path)
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
custom_data = base64encode(local.init_script)
}
resource "azurerm_network_security_group" "connector_sg" {
name = "${var.name_prefix}-connector_sg"
location = var.location
resource_group_name = var.resource_group_name
security_rule {
name = "Connector: Management"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "2222"
source_address_prefixes = var.management_cidrs
destination_address_prefix = azurerm_network_interface.connector_nic.private_ip_address
}
security_rule {
name = "Connector: Banyan Global Edge network"
priority = 101
direction = "Outbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = azurerm_network_interface.connector_nic.private_ip_address
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "connector_sg_assoc" {
subnet_id = var.subnet_id
network_security_group_id = azurerm_network_security_group.connector_sg.id
depends_on = [azurerm_linux_virtual_machine.connector_vm]
}