...
1/**
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17```hcl
18data "google_project" "project" {
19}
20
21resource "google_sql_database_instance" "instance" {
22 name = "my-instance"
23 database_version = "MYSQL_8_0"
24 region = "us-central1"
25 settings {
26 tier = "db-f1-micro"
27 backup_configuration {
28 enabled = true
29 binary_log_enabled = true
30 }
31
32 ip_configuration {
33
34 // Datastream IPs will vary by region.
35 authorized_networks {
36 value = "34.71.242.81"
37 }
38
39 authorized_networks {
40 value = "34.72.28.29"
41 }
42
43 authorized_networks {
44 value = "34.67.6.157"
45 }
46
47 authorized_networks {
48 value = "34.67.234.134"
49 }
50
51 authorized_networks {
52 value = "34.72.239.218"
53 }
54 }
55 }
56
57 deletion_protection = true
58}
59
60resource "google_sql_database" "db" {
61 instance = google_sql_database_instance.instance.name
62 name = "db"
63}
64
65resource "random_password" "pwd" {
66 length = 16
67 special = false
68}
69
70resource "google_sql_user" "user" {
71 name = "user"
72 instance = google_sql_database_instance.instance.name
73 host = "%"
74 password = random_password.pwd.result
75}
76
77resource "google_datastream_connection_profile" "source_connection_profile" {
78 display_name = "Source connection profile"
79 location = "us-central1"
80 connection_profile_id = "source-profile"
81
82 mysql_profile {
83 hostname = google_sql_database_instance.instance.public_ip_address
84 username = google_sql_user.user.name
85 password = google_sql_user.user.password
86 }
87}
88
89data "google_bigquery_default_service_account" "bq_sa" {
90}
91
92resource "google_kms_crypto_key_iam_member" "bigquery_key_user" {
93 crypto_key_id = "bigquery-kms-name"
94 role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
95 member = "serviceAccount:${data.google_bigquery_default_service_account.bq_sa.email}"
96}
97
98resource "google_datastream_connection_profile" "destination_connection_profile" {
99 display_name = "Connection profile"
100 location = "us-central1"
101 connection_profile_id = "destination-profile"
102
103 bigquery_profile {}
104}
105
106resource "google_datastream_stream" "default" {
107 depends_on = [
108 google_kms_crypto_key_iam_member.bigquery_key_user
109 ]
110 stream_id = "my-stream"
111 location = "us-central1"
112 display_name = "my stream"
113 source_config {
114 source_connection_profile = google_datastream_connection_profile.source_connection_profile.id
115 mysql_source_config {}
116 }
117 destination_config {
118 destination_connection_profile = google_datastream_connection_profile.destination_connection_profile.id
119 bigquery_destination_config {
120 source_hierarchy_datasets {
121 dataset_template {
122 location = "us-central1"
123 kms_key_name = "bigquery-kms-name"
124 }
125 }
126 }
127 }
128
129 backfill_none {
130 }
131}
132```
View as plain text