-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextcloud-integrate-storage-basic-config.tf
More file actions
260 lines (205 loc) · 6.37 KB
/
nextcloud-integrate-storage-basic-config.tf
File metadata and controls
260 lines (205 loc) · 6.37 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Объявление переменных
variable "folder_id" {
type = string
}
variable "bucket_name" {
type = string
}
variable "ssh_key_path" {
type = string
}
variable "db_password" {
type = string
sensitive = true
}
variable "domain_name" {
type = string
}
locals {
sa_name = "nextcloud-sa"
network_name = "nextcloud-network"
subnet_a_name = "nextcloud-subnet-ru-central1-a"
subnet-a-cidr = "192.168.11.0/24"
sg_name = "nextcloud-sg"
gw_name = "nextcloud-gateway"
vm_name = "nextcloud-vm"
vm_image_id = "fd8vsghfu10ev5gdatkh"
ig_name = "nexcloud-ig"
cluster_name = "nextcloud-db-cluster"
db_name = "nextcloud"
db_username = "user"
}
# Настройка провайдера
terraform {
required_providers {
yandex = {
source = "yandex-cloud/yandex"
}
}
required_version = ">= 0.13"
}
provider "yandex" {
folder_id = var.folder_id
}
# Создание сети
resource "yandex_vpc_network" "nextcloud-network" {
name = local.network_name
}
# Создание NAT-шлюза
resource "yandex_vpc_gateway" "nextcloud-gw" {
name = local.gw_name
shared_egress_gateway {}
}
# Создание таблицы маршрутизации
resource "yandex_vpc_route_table" "nextcloud-rt-table" {
network_id = yandex_vpc_network.nextcloud-network.id
static_route {
destination_prefix = "0.0.0.0/0"
gateway_id = yandex_vpc_gateway.nextcloud-gw.id
}
}
# Создание подсети
resource "yandex_vpc_subnet" "nextcloud-subnet-a" {
name = local.subnet_a_name
zone = "ru-central1-a"
network_id = yandex_vpc_network.nextcloud-network.id
v4_cidr_blocks = [local.subnet-a-cidr]
route_table_id = yandex_vpc_route_table.nextcloud-rt-table.id
}
# Создание группы безопасности
resource "yandex_vpc_security_group" "nextcloud-sg" {
name = local.sg_name
network_id = yandex_vpc_network.nextcloud-network.id
ingress {
protocol = "TCP"
description = "http"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 80
}
ingress {
protocol = "TCP"
description = "https"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 443
}
ingress {
protocol = "TCP"
description = "ssh"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 22
}
ingress {
protocol = "ANY"
description = "self"
predefined_target = "self_security_group"
from_port = 0
to_port = 65535
}
ingress {
protocol = "ANY"
description = "healthchecks"
predefined_target = "loadbalancer_healthchecks"
from_port = 0
to_port = 65535
}
egress {
protocol = "ANY"
description = "any"
v4_cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 65535
}
}
# Создание сервисного аккаунта
resource "yandex_iam_service_account" "nextcloud-sa" {
name = local.sa_name
}
# Назначение роли сервисному аккаунту
resource "yandex_resourcemanager_folder_iam_member" "editor" {
folder_id = var.folder_id
role = "editor"
member = "serviceAccount:${yandex_iam_service_account.nextcloud-sa.id}"
}
# Создание статического ключа доступа для СА
resource "yandex_iam_service_account_static_access_key" "sa-static-key" {
service_account_id = yandex_iam_service_account.nextcloud-sa.id
}
# Создание бакета
resource "yandex_storage_bucket" "nextcloud-bucket" {
access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key
secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key
bucket = var.bucket_name
}
# Создание загрузочного диска для ВМ
resource "yandex_compute_disk" "boot-disk" {
type = "network-ssd"
zone = "ru-central1-a"
size = "24"
image_id = local.vm_image_id
}
# Создание ВМ
resource "yandex_compute_instance" "nextcloud-vm" {
name = local.vm_name
platform_id = "standard-v2"
zone = "ru-central1-a"
resources {
cores = "2"
memory = "4"
}
boot_disk {
disk_id = yandex_compute_disk.boot-disk.id
}
network_interface {
subnet_id = yandex_vpc_subnet.nextcloud-subnet-a.id
nat = true
security_group_ids = [yandex_vpc_security_group.nextcloud-sg.id]
}
metadata = {
user-data = "#cloud-config\nusers:\n - name: yc-user\n groups: sudo\n shell: /bin/bash\n sudo: 'ALL=(ALL) NOPASSWD:ALL'\n ssh-authorized-keys:\n - ${file(var.ssh_key_path)}"
}
}
# Создание кластера MySQL
resource "yandex_mdb_mysql_cluster" "nextcloud-cluster" {
name = local.cluster_name
environment = "PRODUCTION"
network_id = yandex_vpc_network.nextcloud-network.id
version = "8.0"
security_group_ids = [yandex_vpc_security_group.nextcloud-sg.id]
resources {
resource_preset_id = "s2.micro"
disk_type_id = "network-ssd"
disk_size = 10
}
host {
zone = "ru-central1-a"
subnet_id = yandex_vpc_subnet.nextcloud-subnet-a.id
}
mysql_config = {
sql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
character_set_server = "utf8mb4"
collation_server = "utf8mb4_general_ci"
}
}
# Создание базы данных в кластере MySQL
resource "yandex_mdb_mysql_database" "nextcloud-db" {
cluster_id = yandex_mdb_mysql_cluster.nextcloud-cluster.id
name = local.db_name
}
# Создание пользователя БД в кластере MySQL
resource "yandex_mdb_mysql_user" "my_user" {
cluster_id = yandex_mdb_mysql_cluster.nextcloud-cluster.id
name = local.db_username
password = var.db_password
permission {
database_name = yandex_mdb_mysql_database.nextcloud-db.name
roles = ["ALL"]
}
}
# Вывод данных статического ключа доступа на экран
output "Access_key" {
value = yandex_iam_service_account_static_access_key.sa-static-key.access_key
}
output "Secret_key" {
value = yandex_iam_service_account_static_access_key.sa-static-key.secret_key
sensitive = true
}