-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathya-direct-to-mch.tf
246 lines (214 loc) · 8.5 KB
/
ya-direct-to-mch.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
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
# Infrastructure for Cloud Functions, Object Storage, the Managed Service for ClickHouse®, and Data Transfer
#
# RU: https://cloud.yandex.ru/docs/data-transfer/tutorials/direct-clickhouse
# EN: https://cloud.yandex.com/en/docs/data-transfer/tutorials/direct-clickhouse
#
# Specify the following settings:
locals {
folder_id = "" # Set your cloud folder ID, same as for provider.
app_token = "" # Set an application token.
bucket_name = "" # Set a unique bucket name.
ch_password = "" # Set a password for the ClickHouse® admin user.
path_to_zip_cf = "" # Path to ZIP archive with function code
create_function = 0 # Set to 1 to create the Cloud Function.
# Specify these settings ONLY AFTER the cluster is created. Then run "terraform apply" command again.
# You should set up a source endpoint for the Object Storage bucket using the GUI to obtain its ID.
source_endpoint_id = "" # Set the source endpoint ID.
transfer_enabled = 0 # Set to 1 to enable Transfer.
# The following settings are predefined. Change them only if necessary.
network_name = "mch-network" # Name of the network
subnet_name = "mch-subnet-a" # Name of the subnet
zone_a_v4_cidr_blocks = "10.1.0.0/16" # CIDR block for the subnet in the ru-central1-a availability zone
sa-name = "storage-lockbox-sa" # Name of the service account
function_name = "direct-to-objstorage" # Name of the function
security_group_name = "mch-security-group" # Name of the security group
mch_cluster_name = "mch-cluster" # Name of the ClickHouse cluster
database_name = "db1" # Name of the ClickHouse database
ch_username = "user1" # Name of the ClickHouse admin user
target_endpoint_name = "mch-target" # Name of the target endpoint for the ClickHouse® cluster
transfer_name = "s3-mch-transfer" # Name of the transfer from the Object Storage bucket to the Managed Service for ClickHouse® cluster
}
# Network infrastructure for the Managed Service for ClickHouse® cluster
resource "yandex_vpc_network" "network" {
description = "Network for the Managed Service for ClickHouse® cluster"
name = local.network_name
}
resource "yandex_vpc_subnet" "subnet-a" {
description = "Subnet in the ru-central1-a availability zone"
name = local.subnet_name
zone = "ru-central1-a"
network_id = yandex_vpc_network.network.id
v4_cidr_blocks = [local.zone_a_v4_cidr_blocks]
}
resource "yandex_vpc_security_group" "security-group" {
description = "Security group for the Managed Service for ClickHouse® cluster"
name = local.security_group_name
network_id = yandex_vpc_network.network.id
ingress {
description = "# Allow connections to the Managed Service for ClickHouse® cluster from the internet"
protocol = "TCP"
port = 9440
v4_cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "# Allow connections to the Managed Service for ClickHouse® cluster from the internet"
protocol = "TCP"
port = 8443
v4_cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "The rule allows all outgoing traffic"
protocol = "ANY"
v4_cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 65535
}
}
# Infrastructure for the Object Storage bucket and Cloud Function
# Create a service account
resource "yandex_iam_service_account" "example-sa" {
folder_id = local.folder_id
name = local.sa-name
}
# Create a static key for the service account
resource "yandex_iam_service_account_static_access_key" "example-sa-sk" {
service_account_id = yandex_iam_service_account.example-sa.id
}
# Grant the service account a role to create storages
resource "yandex_resourcemanager_folder_iam_binding" "s3-admin" {
folder_id = local.folder_id
role = "storage.admin"
members = [
"serviceAccount:${yandex_iam_service_account.example-sa.id}",
]
}
# Grant the service account a role to use Lockbox
resource "yandex_resourcemanager_folder_iam_binding" "lockbox" {
folder_id = local.folder_id
role = "lockbox.payloadViewer"
members = [
"serviceAccount:${yandex_iam_service_account.example-sa.id}",
]
}
# Create Yandex Object Storage bucket
resource "yandex_storage_bucket" "example-bucket" {
bucket = local.bucket_name
access_key = yandex_iam_service_account_static_access_key.example-sa-sk.access_key
secret_key = yandex_iam_service_account_static_access_key.example-sa-sk.secret_key
}
# Create a Lockbox secret
resource "yandex_lockbox_secret" "sa_key_token_secret" {
name = "sa_key_token_secret"
description = "Contains static key pair and Yandex Direct token in order to pass it later to Yandex Cloud Function"
folder_id = local.folder_id
}
# Create a version of Lockbox secret with static key pair and application token
resource "yandex_lockbox_secret_version" "first_version" {
secret_id = yandex_lockbox_secret.sa_key_token_secret.id
entries {
key = "access_key"
text_value = yandex_iam_service_account_static_access_key.example-sa-sk.access_key
}
entries {
key = "secret_key"
text_value = yandex_iam_service_account_static_access_key.example-sa-sk.secret_key
}
entries {
key = "app_token"
text_value = local.app_token
}
}
# Create a Yandex Cloud Function
resource "yandex_function" "example-function" {
count = local.create_function
name = local.function_name
user_hash = "example-function-hash"
folder_id = local.folder_id
runtime = "python39"
entrypoint = "example.foo"
memory = "128"
execution_timeout = "100"
service_account_id = yandex_iam_service_account.example-sa.id
content {
zip_filename = local.path_to_zip_cf
}
secrets {
id = yandex_lockbox_secret.sa_key_token_secret.id
version_id = yandex_lockbox_secret_version.first_version.id
key = "access_key"
environment_variable = "AWS_ACCESS_KEY_ID"
}
secrets {
id = yandex_lockbox_secret.sa_key_token_secret.id
version_id = yandex_lockbox_secret_version.first_version.id
key = "secret_key"
environment_variable = "AWS_SECRET_ACCESS_KEY"
}
secrets {
id = yandex_lockbox_secret.sa_key_token_secret.id
version_id = yandex_lockbox_secret_version.first_version.id
key = "app_token"
environment_variable = "TOKEN"
}
environment = {
BUCKET = yandex_storage_bucket.example-bucket.bucket
}
}
resource "yandex_mdb_clickhouse_cluster" "mch-cluster" {
description = "Managed Service for ClickHouse® cluster"
name = local.mch_cluster_name
environment = "PRODUCTION"
network_id = yandex_vpc_network.network.id
security_group_ids = [yandex_vpc_security_group.security-group.id]
clickhouse {
resources {
resource_preset_id = "s2.micro" # 2 vCPU, 8 GB RAM
disk_type_id = "network-ssd"
disk_size = 10 # GB
}
}
host {
type = "CLICKHOUSE"
zone = "ru-central1-a"
subnet_id = yandex_vpc_subnet.subnet-a.id
assign_public_ip = true # Required for connection from the internet
}
database {
name = local.database_name
}
user {
name = local.ch_username
password = local.ch_password
permission {
database_name = local.database_name
}
}
}
# Data Transfer infrastructure
resource "yandex_datatransfer_endpoint" "mch-target" {
description = "Target endpoint for ClickHouse® cluster"
name = local.target_endpoint_name
settings {
clickhouse_target {
connection {
connection_options {
mdb_cluster_id = yandex_mdb_clickhouse_cluster.mch-cluster.id
database = local.database_name
user = local.ch_username
password {
raw = local.ch_password
}
}
}
cleanup_policy = "CLICKHOUSE_CLEANUP_POLICY_DROP"
}
}
}
resource "yandex_datatransfer_transfer" "objstorage-mch-transfer" {
count = local.transfer_enabled
description = "Transfer from the Object Storage bucket to the Managed Service for ClickHouse® cluster"
name = "transfer-objstorage-mch"
source_id = local.source_endpoint_id
target_id = yandex_datatransfer_endpoint.mch-target.id
type = "SNAPSHOT_ONLY" # Copy data
}