-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdc-mpg-mkf.tf
More file actions
272 lines (223 loc) · 9.21 KB
/
Copy pathcdc-mpg-mkf.tf
File metadata and controls
272 lines (223 loc) · 9.21 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
261
262
263
264
265
266
267
268
269
270
271
272
# Infrastructure for change data capture from Yandex Managed Service for PostgreSQL and delivery to Yandex Managed Service for Apache Kafka® via Data Transfer
#
# RU: https://yandex.cloud/ru/docs/data-transfer/tutorials/cdc-mpg
# EN: https://yandex.cloud/en/docs/data-transfer/tutorials/cdc-mpg
#
# Specify the following settings:
locals {
# The following settings are to be specified by the user. Change them as you wish.
# Settings for the Network infrastructure
network_name = "" # Name of the network for the Managed Service for PostgreSQL and Managed Service for Apache Kafka® clusters
subnet_name = "" # Name of the subnet for the Managed Service for PostgreSQL and Managed Service for Apache Kafka® clusters
pg_sg_name = "" # Name of the security group for the Managed Service for PostgreSQL cluster
kf_sg_name = "" # Name of the security group for the Managed Service for Apache Kafka® cluster
# Settings for the Managed Service for PostgreSQL cluster
pg_cluster_version = "" # Version of the Managed Service for PostgreSQL cluster. For the list of supported versions, see https://yandex.cloud/en/docs/managed-postgresql/.
pg_cluster_name = "" # Name of the Managed Service for PostgreSQL cluster
pg_password = "" # Password of the Managed Service for PostgreSQL user
# Settings for the Managed Service for Apache Kafka®
kf_cluster_version = "" # Version of the Managed Service for Apache Kafka® cluster. For the list of supported versions, see https://yandex.cloud/en/docs/managed-kafka/.
kf_cluster_name = "" # Name of the Managed Service for Apache Kafka® cluster
kf_password = "" # Password of the Managed Service for Apache Kafka® user
# Settings for the Yandex Data Transfer
source_endpoint_name = "" # Name of the source endpoint for the Managed Service for PostgreSQL
target_endpoint_name = "" # Name of the target endpoint for the Managed Service for Apache Kafka®
transfer_name = "" # Name of the transfer from the Managed Service for PostgreSQL to the Managed Service for Apache Kafka®
# Specify this setting ONLY AFTER the clusters are created. Then run "terraform apply" command again.
transfer_enabled = 0 # Set to 1 to create Data Transfer endpoints and the transfer
# The following settings are predefined. Change them only if necessary.
# Settings for the Managed Service for PostgreSQL cluster
pg_db_name = "db1" # Name of the Managed Service for PostgreSQL database
pg_username = "pg-user" # Name of the Managed Service for PostgreSQL user
# Settings for the Managed Service for Apache Kafka® cluster
kf_topic = "cdc.public.measurements" # Name of the Managed Service for Apache Kafka® topic
kf_username = "kafka-user" # Name of the Managed Service for Apache Kafka® user
# Settings for the Network infrastructure
zone_a_v4_cidr_blocks = "10.1.0.0/16" # CIDR block for the subnet in the ru-central1-a availability zone
}
# Network infrastructure
resource "yandex_vpc_network" "network" {
description = "Network for the Managed Service for PostgreSQL and Managed Service for Apache Kafka® clusters"
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" "pg_sg" {
description = "Security group for the Managed Service for PostgreSQL"
network_id = yandex_vpc_network.network.id
name = local.pg_sg_name
ingress {
description = "Allow incoming traffic from the Internet"
protocol = "TCP"
port = 6432
v4_cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "Allow outgoing traffic to the Internet"
protocol = "ANY"
from_port = 0
to_port = 65535
v4_cidr_blocks = ["0.0.0.0/0"]
}
}
resource "yandex_vpc_security_group" "kf_sg" {
description = "Security group for the Managed Service for Apache Kafka®"
network_id = yandex_vpc_network.network.id
name = local.kf_sg_name
ingress {
description = "Allow incoming traffic from the port 9091"
protocol = "TCP"
port = 9091
v4_cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "Allow outgoing traffic to the Internet"
protocol = "ANY"
from_port = 0
to_port = 65535
v4_cidr_blocks = ["0.0.0.0/0"]
}
}
# Infrastructure for the Managed Service for PostgreSQL cluster
resource "yandex_mdb_postgresql_cluster" "pg_cluster" {
description = "Managed Service for PostgreSQL cluster"
name = local.pg_cluster_name
environment = "PRODUCTION"
network_id = yandex_vpc_network.network.id
security_group_ids = [yandex_vpc_security_group.pg_sg.id]
config {
version = local.pg_cluster_version
resources {
resource_preset_id = "s2.micro" # 2 vCPU, 8 GB RAM
disk_type_id = "network-ssd"
disk_size = "20" # GB
}
}
host {
zone = "ru-central1-a"
subnet_id = yandex_vpc_subnet.subnet-a.id
assign_public_ip = true # Required for connection from the Internet
}
}
# User of the Managed Service for PostgreSQL cluster
resource "yandex_mdb_postgresql_user" "pg_user" {
cluster_id = yandex_mdb_postgresql_cluster.pg_cluster.id
name = local.pg_username
password = local.pg_password
grants = ["mdb_replication"] # Grants permissions required for logical replication
}
# Database of the Managed Service for PostgreSQL cluster
resource "yandex_mdb_postgresql_database" "pg_db" {
cluster_id = yandex_mdb_postgresql_cluster.pg_cluster.id
name = local.pg_db_name
owner = yandex_mdb_postgresql_user.pg_user.name
depends_on = [
yandex_mdb_postgresql_user.pg_user
]
}
# Infrastructure for the Managed Service for Apache Kafka® cluster
resource "yandex_mdb_kafka_cluster" "kf_cluster" {
description = "Managed Service for Apache Kafka® cluster"
environment = "PRODUCTION"
name = local.kf_cluster_name
network_id = yandex_vpc_network.network.id
security_group_ids = [yandex_vpc_security_group.kf_sg.id]
config {
assign_public_ip = true # Required for connection from the Internet
brokers_count = 1
version = local.kf_cluster_version
kafka {
resources {
disk_size = 10 # GB
disk_type_id = "network-ssd"
resource_preset_id = "s2.micro" # 2 vCPU, 8 GB RAM
}
kafka_config {}
}
zones = ["ru-central1-a"]
}
depends_on = [
yandex_vpc_subnet.subnet-a
]
}
# Topic of the Managed Service for Apache Kafka® cluster
resource "yandex_mdb_kafka_topic" "kf_topic" {
cluster_id = yandex_mdb_kafka_cluster.kf_cluster.id
name = local.kf_topic
partitions = 1
replication_factor = 1
}
# User of the Managed Service for Apache Kafka® cluster
resource "yandex_mdb_kafka_user" "kf_user" {
cluster_id = yandex_mdb_kafka_cluster.kf_cluster.id
name = local.kf_username
password = local.kf_password
permission {
topic_name = yandex_mdb_kafka_topic.kf_topic.name
role = "ACCESS_ROLE_CONSUMER"
}
permission {
topic_name = yandex_mdb_kafka_topic.kf_topic.name
role = "ACCESS_ROLE_PRODUCER"
}
}
# Data Transfer infrastructure
resource "yandex_datatransfer_endpoint" "pg_source" {
description = "Source endpoint for the Managed Service for PostgreSQL cluster"
count = local.transfer_enabled
name = local.source_endpoint_name
settings {
postgres_source {
connection {
mdb_cluster_id = yandex_mdb_postgresql_cluster.pg_cluster.id
}
database = yandex_mdb_postgresql_database.pg_db.name
user = yandex_mdb_postgresql_user.pg_user.name
include_tables = ["public.measurements"]
password {
raw = local.pg_password
}
}
}
}
resource "yandex_datatransfer_endpoint" "kf_target" {
description = "Target endpoint for the Managed Service for Apache Kafka® cluster"
name = local.target_endpoint_name
count = local.transfer_enabled
settings {
kafka_target {
connection {
cluster_id = yandex_mdb_kafka_cluster.kf_cluster.id
}
auth {
sasl {
user = yandex_mdb_kafka_user.kf_user.name
password {
raw = local.kf_password
}
}
}
topic_settings {
topic {
topic_name = local.kf_topic
}
}
serializer {
serializer_auto {}
}
}
}
}
resource "yandex_datatransfer_transfer" "mpg-mkf-transfer" {
description = "Transfer from the Managed Service for PostgreSQL to the Managed Service for Apache Kafka®"
count = local.transfer_enabled
name = local.transfer_name
source_id = yandex_datatransfer_endpoint.pg_source[0].id
target_id = yandex_datatransfer_endpoint.kf_target[0].id
type = "INCREMENT_ONLY" # Data replication from the source Managed Service for PostgreSQL cluster to the target Managed Service for Apache Kafka® topic
}