-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
1314 lines (1184 loc) · 55.7 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
locals {
single = var.type == "single" ? true : false
cluster = var.type == "cluster" ? true : false
autoscaling = var.type == "autoscaling" ? true : false
google_cloud_project = data.google_project.existing_gcp_project.project_id
ssh_private_key_path = var.create_new_ssh_keys ? local_file.red5pro_ssh_key_pem[0].filename : var.existing_private_ssh_key_path
public_ssh_key = var.create_new_ssh_keys ? tls_private_key.red5pro_ssh_key[0].public_key_openssh : file(var.existing_public_ssh_key_path)
private_ssh_key = var.create_new_ssh_keys ? tls_private_key.red5pro_ssh_key[0].private_key_pem : file(var.existing_private_ssh_key_path)
vpc_network_name = var.vpc_create ? google_compute_network.vpc_red5_network[0].name : data.google_compute_network.existing_vpc_network[0].name
single_server_ip = local.single ? google_compute_instance.red5_single_server[0].network_interface.0.access_config.0.nat_ip : null
cluster_or_autoscaling = local.cluster || local.autoscaling ? true : false
mysql_local_enable = local.autoscaling ? false : local.cluster && var.mysql_database_create ? false : local.cluster && var.terraform_service_instance_create ? false : true
mysql_db_system_create = local.autoscaling ? true : local.cluster && var.mysql_database_create ? true : local.cluster && var.terraform_service_instance_create ? true : false
mysql_host = local.autoscaling ? google_sql_database_instance.mysql_database[0].ip_address.0.ip_address : local.cluster && var.mysql_database_create ? google_sql_database_instance.mysql_database[0].ip_address.0.ip_address : local.cluster && var.terraform_service_instance_create ? google_sql_database_instance.mysql_database[0].ip_address.0.ip_address : "localhost"
terraform_service_ip = local.autoscaling ? google_compute_instance.red5pro_terraform_service[0].network_interface.0.access_config.0.nat_ip : local.cluster && var.terraform_service_instance_create ? google_compute_instance.red5pro_terraform_service[0].network_interface.0.access_config.0.nat_ip : "localhost"
terraform_service_local_enable = local.autoscaling ? false : local.cluster && var.terraform_service_instance_create ? false : true
dedicated_terraform_service_create = local.autoscaling ? true : local.cluster && var.terraform_service_instance_create ? true : false
stream_manager_ip = local.autoscaling ? local.lb_ip_address : local.cluster ? local.sm_nat_ip : null
lb_ssl_certificate = local.autoscaling && var.create_new_lb_ssl_cert && var.create_lb_with_ssl ? google_compute_ssl_certificate.new_lb_ssl_cert[0].id : local.cluster || local.single ? null : var.create_lb_with_ssl ? data.google_compute_ssl_certificate.existing_ssl_lb_cert[0].id : null
lb_ip_address = local.autoscaling && var.create_new_global_reserved_ip_for_lb ? google_compute_global_address.lb_reserved_ip[0].address : local.autoscaling ? data.google_compute_global_address.existing_lb_reserved_ip[0].address : null
create_sm_reserved_ip = local.cluster && var.create_new_reserved_ip_for_stream_manager ? true : false
sm_nat_ip = local.create_sm_reserved_ip ? google_compute_address.sm_reserved_ip[0].address : local.cluster ? data.google_compute_address.existing_sm_reserved_ip[0].address : null
sm_port = local.autoscaling ? var.lb_http_port_required : "5080"
single_server_firewall = local.single ? var.vpc_create ? true : var.create_new_firewall_for_single_server ? true : false : false
stream_manager_firewall = local.cluster_or_autoscaling ? var.vpc_create ? true : var.create_new_firewall_for_stream_manager ? true : false : false
terraform_service_firewall = local.cluster_or_autoscaling ? var.vpc_create ? true : var.create_new_firewall_for_terraform_service ? true : false : false
red5_node_firewall = local.cluster_or_autoscaling ? var.vpc_create ? true : var.create_new_firewall_for_nodes ? true : false : false
}
################################################################################
# Google Cloud Project
################################################################################
# Existing google project of google cloud account
data "google_project" "existing_gcp_project" {
project_id = var.google_project_id
}
################################################################################
# SSH_KEY
################################################################################
# SSH key pair generation
resource "tls_private_key" "red5pro_ssh_key" {
count = var.create_new_ssh_keys ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
# Save SSH key pair files to local folder
resource "local_file" "red5pro_ssh_key_pem" {
count = var.create_new_ssh_keys ? 1 : 0
filename = "./${var.new_ssh_key_name}.pem"
content = tls_private_key.red5pro_ssh_key[0].private_key_pem
file_permission = "0400"
}
resource "local_file" "red5pro_ssh_key_pub" {
count = var.create_new_ssh_keys ? 1 : 0
filename = "./${var.new_ssh_key_name}.pub"
content = tls_private_key.red5pro_ssh_key[0].public_key_openssh
}
################################################################################
# VPC and Network Configuration
################################################################################
# Create VPC
resource "google_compute_network" "vpc_red5_network" {
count = var.vpc_create ? 1 : 0
name = "${var.name}-vpc"
project = local.google_cloud_project
auto_create_subnetworks = true
delete_default_routes_on_create = false
}
data "google_compute_network" "existing_vpc_network" {
count = var.vpc_create ? 0 : 1
name = var.existing_vpc_network_name
project = local.google_cloud_project
}
data "google_compute_zones" "available_zone" {
region = var.google_region
project = local.google_cloud_project
status = "UP"
}
################################################################################
# Red5 Single Server Configuration
################################################################################
# Create security group
resource "google_compute_firewall" "red5_single_firewall" {
count = local.single && local.single_server_firewall ? 1 : 0
name = "${var.name}-single-firewall"
network = local.vpc_network_name
priority = 1000
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = var.red5_single_firewall_ports
}
source_ranges = ["0.0.0.0/0"]
project = local.google_cloud_project
target_tags = tolist([var.new_or_existing_network_tag_for_single_server])
}
resource "google_compute_firewall" "red5_single_ssh_firewall" {
count = local.single && local.single_server_firewall ? 1 : 0
name = "${var.name}-single-ssh-firewall"
network = local.vpc_network_name
priority = 1000
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = var.red5_single_ssh_connection_source_ranges
project = local.google_cloud_project
target_tags = tolist([var.new_or_existing_network_tag_for_single_server])
}
# Red5 Pro single server instance
resource "google_compute_instance" "red5_single_server" {
count = local.single ? 1 : 0
name = "${var.name}-single-server"
machine_type = var.single_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.single_server_boot_disk_type
}
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_red5pro_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_red5pro_build)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export LICENSE_KEY='${var.red5pro_license_key}'",
"export NODE_API_ENABLE='${var.red5pro_api_enable}'",
"export NODE_API_KEY='${var.red5pro_api_key}'",
"export NODE_INSPECTOR_ENABLE='${var.red5pro_inspector_enable}'",
"export NODE_RESTREAMER_ENABLE='${var.red5pro_restreamer_enable}'",
"export NODE_SOCIALPUSHER_ENABLE='${var.red5pro_socialpusher_enable}'",
"export NODE_SUPPRESSOR_ENABLE='${var.red5pro_suppressor_enable}'",
"export NODE_HLS_ENABLE='${var.red5pro_hls_enable}'",
"export NODE_ROUND_TRIP_AUTH_ENABLE='${var.red5pro_round_trip_auth_enable}'",
"export NODE_ROUND_TRIP_AUTH_HOST='${var.red5pro_round_trip_auth_host}'",
"export NODE_ROUND_TRIP_AUTH_PORT='${var.red5pro_round_trip_auth_port}'",
"export NODE_ROUND_TRIP_AUTH_PROTOCOL='${var.red5pro_round_trip_auth_protocol}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_VALIDATE='${var.red5pro_round_trip_auth_endpoint_validate}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_INVALIDATE='${var.red5pro_round_trip_auth_endpoint_invalidate}'",
"export NODE_CLOUDSTORAGE_ENABLE='${var.red5pro_cloudstorage_enable}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_ACCESS_KEY='${var.red5pro_google_storage_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_SECRET_ACCESS_KEY='${var.red5pro_google_storage_secret_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_BUCKET_NAME='${var.red5pro_google_storage_bucket_name}'",
"export NODE_CLOUDSTORAGE_POSTPROCESSOR_ENABLE='${var.red5pro_cloudstorage_postprocessor_enable}'",
"export SSL_ENABLE='${var.https_letsencrypt_enable}'",
"export SSL_DOMAIN='${var.https_letsencrypt_certificate_domain_name}'",
"export SSL_MAIL='${var.https_letsencrypt_certificate_email}'",
"export SSL_PASSWORD='${var.https_letsencrypt_certificate_password}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_server_basic.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node_apps_plugins.sh",
"sudo systemctl daemon-reload && sudo systemctl start red5pro",
"nohup sudo -E /home/ubuntu/red5pro-installer/r5p_ssl_check_install.sh >> /home/ubuntu/red5pro-installer/r5p_ssl_check_install.log &",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
tags = tolist([var.new_or_existing_network_tag_for_single_server])
}
################################################################################
# Red5 Stream Manager Server Configuration
################################################################################
# Create security group for stream manager
resource "google_compute_firewall" "red5_stream_manager_firewall" {
count = local.cluster_or_autoscaling && local.stream_manager_firewall ? 1 : 0
name = "${var.name}-stream-manager-firewall"
network = local.vpc_network_name
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = var.red5_stream_manager_firewall_tcp_ports
}
allow {
protocol = "udp"
ports = var.red5_stream_manager_firewall_udp_ports
}
source_ranges = ["0.0.0.0/0"]
project = local.google_cloud_project
target_tags = tolist([var.new_or_existing_network_tag_for_stream_manager])
}
# Reserved IP address for Stream Manager
resource "google_compute_address" "sm_reserved_ip" {
count = local.autoscaling ? 0 : local.create_sm_reserved_ip ? 1 : 0
name = "${var.name}-sm-reserver-ip"
address_type = "EXTERNAL"
project = local.google_cloud_project
region = var.google_region
}
# Already created reserved IP for Stream Manager
data "google_compute_address" "existing_sm_reserved_ip" {
count = local.single ? 0 : local.autoscaling ? 0 : local.create_sm_reserved_ip ? 0 : 1
name = var.existing_sm_reserved_ip_name
project = local.google_cloud_project
region = var.google_region
lifecycle {
postcondition {
condition = self.address != null
error_message = "The existing IP address with name: ${var.existing_sm_reserved_ip_name} does not exist in region ${var.google_region}."
}
}
}
# Red5 Pro Stream Manager Instance
resource "google_compute_instance" "red5_stream_manager_server" {
count = local.cluster_or_autoscaling ? 1 : 0
name = "${var.name}-stream-manager"
machine_type = var.stream_manager_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.stream_manager_server_boot_disk_type
size = var.stream_manager_server_disk_size
}
}
network_interface {
network = local.vpc_network_name
access_config {
nat_ip = local.autoscaling ? null : local.sm_nat_ip
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_red5pro_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_red5pro_build)}"
}
provisioner "file" {
source = var.path_to_terraform_service_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_terraform_service_build)}"
}
provisioner "file" {
source = var.path_to_terraform_cloud_controller
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_terraform_cloud_controller)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export LICENSE_KEY='${var.red5pro_license_key}'",
"export SM_API_KEY='${var.stream_manager_api_key}'",
"export NODE_API_KEY='${var.red5pro_api_key}'",
"export NODE_CLUSTER_KEY='${var.red5pro_cluster_key}'",
"export NODE_PREFIX_NAME='${var.name}-node'",
"export SSL_ENABLE='${var.https_letsencrypt_enable}'",
"export SSL_DOMAIN='${var.https_letsencrypt_certificate_domain_name}'",
"export SSL_MAIL='${var.https_letsencrypt_certificate_email}'",
"export SSL_PASSWORD='${var.https_letsencrypt_certificate_password}'",
"export DB_LOCAL_ENABLE='${local.mysql_local_enable}'",
"export DB_HOST='${local.mysql_host}'",
"export DB_PORT='${var.mysql_port}'",
"export DB_USER='${var.mysql_username}'",
"export DB_PASSWORD='${nonsensitive(var.mysql_password)}'",
"export GOGOLE_PROJECT_ID='${local.google_cloud_project}'",
"export GOOGLE_DEFAULT_ZONE_ID='${self.zone}'",
"export GOOGLE_VPC_NETWORK_NAME='${local.vpc_network_name}'",
# For Terraform Service
"export TF_SVC_ENABLE='${local.terraform_service_local_enable}'",
"export TERRA_HOST='${local.terraform_service_ip}'",
"export TERRA_API_KEY='${var.terraform_service_api_key}'",
"export TERRA_PARALLELISM='${var.terraform_service_parallelism}'",
"export GCP_PROJECT_ID='${var.google_project_id}'",
"export GCP_VPC_NAME='${local.vpc_network_name}'",
"export GCP_NODE_DISK_TYPE='${var.terraform_service_boot_disk_type_for_nodes}'",
"export GCP_NODE_NETWORK_TAG='${var.new_or_existing_network_tag_for_nodes}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_server_basic.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_mysql_local.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_terraform_svc.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_stream_manager.sh",
"sudo systemctl daemon-reload && sudo systemctl start red5pro",
"nohup sudo -E /home/ubuntu/red5pro-installer/r5p_ssl_check_install.sh >> /home/ubuntu/red5pro-installer/r5p_ssl_check_install.log &",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
service_account {
scopes = [ "cloud-platform" ]
}
tags = tolist([var.new_or_existing_network_tag_for_stream_manager])
lifecycle {
ignore_changes = all
precondition {
condition = var.stream_manager_server_disk_size >= 10
error_message = "The Stream Manager Disk size should not be less than 10GB"
}
}
depends_on = [ google_compute_address.sm_reserved_ip, data.google_compute_address.existing_sm_reserved_ip ]
}
################################################################################
# Terraform Service
################################################################################
resource "google_compute_instance" "red5pro_terraform_service" {
count = local.dedicated_terraform_service_create ? 1 : 0
name = "${var.name}-red5-terraform-service"
machine_type = var.terraform_service_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.terraform_service_boot_disk_type
}
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_terraform_service_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_terraform_service_build)}"
}
provisioner "file" {
source = var.path_to_terraform_cloud_controller
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_terraform_cloud_controller)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export TF_SVC_ENABLE=true",
"export TERRA_API_KEY='${var.terraform_service_api_key}'",
"export TERRA_PARALLELISM='${var.terraform_service_parallelism}'",
"export DB_HOST='${local.mysql_host}'",
"export DB_PORT='${var.mysql_port}'",
"export DB_USER='${var.mysql_username}'",
"export DB_PASSWORD='${nonsensitive(var.mysql_password)}'",
"export GCP_PROJECT_ID='${var.google_project_id}'",
"export GCP_VPC_NAME='${local.vpc_network_name}'",
"export GCP_NODE_DISK_TYPE='${var.terraform_service_boot_disk_type_for_nodes}'",
"export GCP_NODE_NETWORK_TAG='${var.new_or_existing_network_tag_for_nodes}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_terraform_svc.sh",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
service_account {
scopes = [ "cloud-platform" ]
}
tags = tolist([var.new_or_existing_network_tag_for_terraform_service])
}
resource "google_compute_firewall" "red5_terraform_service_firewall" {
count = local.dedicated_terraform_service_create && local.terraform_service_firewall ? 1 : 0
name = "${var.name}-terraform-service-firewall"
network = local.vpc_network_name
priority = 1000
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = var.terraform_service_firewall_ports
}
source_ranges = ["0.0.0.0/0"]
project = local.google_cloud_project
target_tags = tolist([var.new_or_existing_network_tag_for_terraform_service])
}
################################################################################
# MySQL Database Configuration
################################################################################
resource "google_sql_database_instance" "mysql_database" {
count = local.mysql_db_system_create ? 1 : 0
name = "${var.name}-mysql-database"
database_version = "MYSQL_8_0"
region = var.google_region
project = local.google_cloud_project
root_password = var.mysql_password
deletion_protection = false
settings {
tier = var.mysql_instance_type
edition = "ENTERPRISE"
availability_type = "REGIONAL"
disk_autoresize = true
backup_configuration {
binary_log_enabled = true
enabled = true
}
ip_configuration {
ipv4_enabled = true
ssl_mode = "ALLOW_UNENCRYPTED_AND_ENCRYPTED"
authorized_networks {
name = "SM-Connection"
value = "0.0.0.0/0"
}
}
location_preference {
zone = element(data.google_compute_zones.available_zone.names, count.index)
}
}
}
# Creating MySQL Database user
resource "google_sql_user" "database_new_user" {
count = local.mysql_db_system_create ? 1 : 0
name = var.mysql_username
instance = google_sql_database_instance.mysql_database[0].name
password = var.mysql_password
project = local.google_cloud_project
}
################################################################################
# Load Balancer Configuration
################################################################################
# Reserved IP address for Load Balancer
resource "google_compute_global_address" "lb_reserved_ip" {
count = local.autoscaling && var.create_new_global_reserved_ip_for_lb ? 1 : 0
name = "${var.name}-lb-reserver-ip"
ip_version = "IPV4"
address_type = "EXTERNAL"
project = local.google_cloud_project
}
# Already created reserved IP for Stream Manager
data "google_compute_global_address" "existing_lb_reserved_ip" {
count = local.single || local.cluster ? 0 : local.autoscaling && var.create_new_global_reserved_ip_for_lb ? 0 : 1
name = var.existing_global_lb_reserved_ip_name
project = local.google_cloud_project
lifecycle {
postcondition {
condition = self.address != null
error_message = "The existing IP address with name: ${var.existing_global_lb_reserved_ip_name} does not exist."
}
}
}
# New SSL cerificate for Load Balancer
resource "google_compute_ssl_certificate" "new_lb_ssl_cert" {
count = local.autoscaling && var.create_new_lb_ssl_cert && var.create_lb_with_ssl ? 1 : 0
name_prefix = "${var.name}-lb-cert"
private_key = file(var.new_ssl_private_key_path)
certificate = file(var.new_ssl_certificate_key_path)
project = local.google_cloud_project
lifecycle {
create_before_destroy = true
}
}
# Existing SSL cerificate for Load Balancer
data "google_compute_ssl_certificate" "existing_ssl_lb_cert" {
count = var.create_lb_with_ssl ? var.create_new_lb_ssl_cert ? 0 : 1 : 0
name = var.existing_ssl_certificate_name
project = local.google_cloud_project
}
resource "google_compute_instance_template" "stream_manager_template" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-stream-manager"
machine_type = var.stream_manager_server_instance_type
tags = tolist([var.new_or_existing_network_tag_for_stream_manager])
project = local.google_cloud_project
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
disk {
auto_delete = true
source_image = google_compute_image.red5_sm_image[0].self_link
disk_type = var.stream_manager_server_boot_disk_type
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
service_account {
scopes = ["cloud-platform"]
}
depends_on = [google_compute_image.red5_sm_image]
lifecycle {
ignore_changes = [ disk ]
}
}
# Health check for stream Manager
resource "google_compute_health_check" "sm_health_check" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-sm-health-check"
project = local.google_cloud_project
timeout_sec = 5
check_interval_sec = 10
healthy_threshold = 5
unhealthy_threshold = 5
http_health_check {
port = 5080
}
}
# Autoscaling Instance Group Manager
resource "google_compute_instance_group_manager" "stream_manager_instance_group" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-sm-instance-group-manager"
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
named_port {
name = "${var.name}-sm-http-port"
port = 5080
}
version {
instance_template = google_compute_instance_template.stream_manager_template[0].self_link_unique
name = "${var.name}-sm-version"
}
base_instance_name = "${var.name}-stream-manager"
target_size = var.count_of_stream_managers
auto_healing_policies {
health_check = google_compute_health_check.sm_health_check[0].id
initial_delay_sec = 300
}
instance_lifecycle_policy {
force_update_on_repair = "NO"
}
}
# Backend service for
resource "google_compute_backend_service" "sm_backend_service" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-backend-service"
project = local.google_cloud_project
protocol = "HTTP"
port_name = "${var.name}-sm-http-port"
load_balancing_scheme = "EXTERNAL"
timeout_sec = 60
enable_cdn = false
health_checks = [google_compute_health_check.sm_health_check[0].id]
backend {
group = google_compute_instance_group_manager.stream_manager_instance_group[0].instance_group
balancing_mode = "UTILIZATION"
capacity_scaler = 1.0
}
}
# Load Balancer URL map
resource "google_compute_url_map" "lb_url_map" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-load-balancer-map"
project = local.google_cloud_project
default_service = google_compute_backend_service.sm_backend_service[0].id
}
# Load Balancer HTTP proxy
resource "google_compute_target_http_proxy" "lb_http_proxy" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-http-proxy"
project = local.google_cloud_project
url_map = google_compute_url_map.lb_url_map[0].id
}
# Load Balancer HTTP forwarding rule
resource "google_compute_global_forwarding_rule" "lb_http_forward_rule" {
count = local.autoscaling ? 1 : 0
name = "${var.name}-forwarding-rule-http"
project = local.google_cloud_project
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL"
port_range = var.lb_http_port_required
target = google_compute_target_http_proxy.lb_http_proxy[0].id
ip_address = local.lb_ip_address
}
# Load Balancer HTTPS proxy
resource "google_compute_target_https_proxy" "lb_https_proxy" {
count = local.autoscaling && var.create_lb_with_ssl ? 1 : 0
name = "${var.name}-https-proxy"
project = local.google_cloud_project
url_map = google_compute_url_map.lb_url_map[0].id
ssl_certificates = [local.lb_ssl_certificate]
}
# Load Balancer forwarding rule
resource "google_compute_global_forwarding_rule" "lb_https_forward_rule" {
count = local.autoscaling && var.create_lb_with_ssl ? 1 : 0
name = "${var.name}-forwarding-rule-https"
project = local.google_cloud_project
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL"
port_range = "443"
target = google_compute_target_https_proxy.lb_https_proxy[0].id
ip_address = local.lb_ip_address
}
################################################################################
# Red5 Node Server Configuration
################################################################################
# Create security group for nodes
resource "google_compute_firewall" "red5_node_firewall" {
count = local.cluster_or_autoscaling && local.red5_node_firewall? 1 : 0
name = "${var.name}-node-firewall"
network = local.vpc_network_name
priority = 1000
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = var.red5_node_firewall_tcp_ports
}
allow {
protocol = "udp"
ports = var.red5_node_firewall_udp_ports
}
source_ranges = ["0.0.0.0/0"]
project = local.google_cloud_project
target_tags = var.new_or_existing_network_tag_for_nodes != "null" ? tolist([var.new_or_existing_network_tag_for_nodes]) : []
}
# Red5 Pro Origin server instance
resource "google_compute_instance" "red5_origin_server" {
count = var.origin_image_create ? 1 : 0
name = "${var.name}-node-origin-image"
machine_type = var.origin_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.origin_server_boot_disk_type
}
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_red5pro_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_red5pro_build)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export LICENSE_KEY='${var.red5pro_license_key}'",
"export SM_IP='${local.stream_manager_ip}'",
"export NODE_CLUSTER_KEY='${var.red5pro_cluster_key}'",
"export NODE_API_ENABLE='${var.red5pro_api_enable}'",
"export NODE_API_KEY='${var.red5pro_api_key}'",
"export SM_PORT='${local.sm_port}'",
"export NODE_INSPECTOR_ENABLE='${var.origin_image_red5pro_inspector_enable}'",
"export NODE_RESTREAMER_ENABLE='${var.origin_image_red5pro_restreamer_enable}'",
"export NODE_SOCIALPUSHER_ENABLE='${var.origin_image_red5pro_socialpusher_enable}'",
"export NODE_SUPPRESSOR_ENABLE='${var.origin_image_red5pro_suppressor_enable}'",
"export NODE_HLS_ENABLE='${var.origin_image_red5pro_hls_enable}'",
"export NODE_ROUND_TRIP_AUTH_ENABLE='${var.origin_image_red5pro_round_trip_auth_enable}'",
"export NODE_ROUND_TRIP_AUTH_HOST='${var.origin_image_red5pro_round_trip_auth_host}'",
"export NODE_ROUND_TRIP_AUTH_PORT='${var.origin_image_red5pro_round_trip_auth_port}'",
"export NODE_ROUND_TRIP_AUTH_PROTOCOL='${var.origin_image_red5pro_round_trip_auth_protocol}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_VALIDATE='${var.origin_image_red5pro_round_trip_auth_endpoint_validate}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_INVALIDATE='${var.origin_image_red5pro_round_trip_auth_endpoint_invalidate}'",
"export NODE_CLOUDSTORAGE_ENABLE='${var.origin_red5pro_cloudstorage_enable}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_ACCESS_KEY='${var.origin_red5pro_google_storage_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_SECRET_ACCESS_KEY='${var.origin_red5pro_google_storage_secret_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_BUCKET_NAME='${var.origin_red5pro_google_storage_bucket_name}'",
"export NODE_CLOUDSTORAGE_POSTPROCESSOR_ENABLE='${var.origin_red5pro_cloudstorage_postprocessor_enable}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_server_basic.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node_apps_plugins.sh",
"sudo systemctl daemon-reload && sudo systemctl start red5pro",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
lifecycle {
ignore_changes = all
}
tags = tolist([var.new_or_existing_network_tag_for_nodes])
depends_on = [ google_compute_instance.red5_stream_manager_server ]
}
# Red5 Pro Edge server instance
resource "google_compute_instance" "red5_edge_server" {
count = var.edge_image_create ? 1 : 0
name = "${var.name}-node-edge-image"
machine_type = var.edge_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.edge_server_boot_disk_type
}
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_red5pro_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_red5pro_build)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export LICENSE_KEY='${var.red5pro_license_key}'",
"export SM_IP='${local.stream_manager_ip}'",
"export NODE_CLUSTER_KEY='${var.red5pro_cluster_key}'",
"export NODE_API_ENABLE='${var.red5pro_api_enable}'",
"export NODE_API_KEY='${var.red5pro_api_key}'",
"export SM_PORT='${local.sm_port}'",
"export NODE_INSPECTOR_ENABLE='${var.edge_image_red5pro_inspector_enable}'",
"export NODE_RESTREAMER_ENABLE='${var.edge_image_red5pro_restreamer_enable}'",
"export NODE_SOCIALPUSHER_ENABLE='${var.edge_image_red5pro_socialpusher_enable}'",
"export NODE_SUPPRESSOR_ENABLE='${var.edge_image_red5pro_suppressor_enable}'",
"export NODE_HLS_ENABLE='${var.edge_image_red5pro_hls_enable}'",
"export NODE_ROUND_TRIP_AUTH_ENABLE='${var.edge_image_red5pro_round_trip_auth_enable}'",
"export NODE_ROUND_TRIP_AUTH_HOST='${var.edge_image_red5pro_round_trip_auth_host}'",
"export NODE_ROUND_TRIP_AUTH_PORT='${var.edge_image_red5pro_round_trip_auth_port}'",
"export NODE_ROUND_TRIP_AUTH_PROTOCOL='${var.edge_image_red5pro_round_trip_auth_protocol}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_VALIDATE='${var.edge_image_red5pro_round_trip_auth_endpoint_validate}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_INVALIDATE='${var.edge_image_red5pro_round_trip_auth_endpoint_invalidate}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_server_basic.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node_apps_plugins.sh",
"sudo systemctl daemon-reload && sudo systemctl start red5pro",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
lifecycle {
ignore_changes = all
}
tags = tolist([var.new_or_existing_network_tag_for_nodes])
}
# Red5 Pro Transcoder server instance
resource "google_compute_instance" "red5_transcoder_server" {
count = var.transcoder_image_create ? 1 : 0
name = "${var.name}-node-transcoder-image"
machine_type = var.transcoder_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project
boot_disk {
initialize_params {
image = lookup(var.ubuntu_images_gcp, var.ubuntu_version, "what?")
type = var.transcoder_server_boot_disk_type
}
}
network_interface {
network = local.vpc_network_name
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${local.public_ssh_key}"
}
provisioner "file" {
source = "${abspath(path.module)}/red5pro-installer"
destination = "/home/ubuntu/"
}
provisioner "file" {
source = var.path_to_red5pro_build
destination = "/home/ubuntu/red5pro-installer/${basename(var.path_to_red5pro_build)}"
}
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
provisioner "remote-exec" {
inline = [
"sudo cloud-init status --wait",
"sudo iptables -F",
"export LICENSE_KEY='${var.red5pro_license_key}'",
"export SM_IP='${local.stream_manager_ip}'",
"export NODE_CLUSTER_KEY='${var.red5pro_cluster_key}'",
"export NODE_API_ENABLE='${var.red5pro_api_enable}'",
"export NODE_API_KEY='${var.red5pro_api_key}'",
"export SM_PORT='${local.sm_port}'",
"export NODE_INSPECTOR_ENABLE='${var.transcoder_image_red5pro_inspector_enable}'",
"export NODE_RESTREAMER_ENABLE='${var.transcoder_image_red5pro_restreamer_enable}'",
"export NODE_SOCIALPUSHER_ENABLE='${var.transcoder_image_red5pro_socialpusher_enable}'",
"export NODE_SUPPRESSOR_ENABLE='${var.transcoder_image_red5pro_suppressor_enable}'",
"export NODE_HLS_ENABLE='${var.transcoder_image_red5pro_hls_enable}'",
"export NODE_ROUND_TRIP_AUTH_ENABLE='${var.transcoder_image_red5pro_round_trip_auth_enable}'",
"export NODE_ROUND_TRIP_AUTH_HOST='${var.transcoder_image_red5pro_round_trip_auth_host}'",
"export NODE_ROUND_TRIP_AUTH_PORT='${var.transcoder_image_red5pro_round_trip_auth_port}'",
"export NODE_ROUND_TRIP_AUTH_PROTOCOL='${var.transcoder_image_red5pro_round_trip_auth_protocol}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_VALIDATE='${var.transcoder_image_red5pro_round_trip_auth_endpoint_validate}'",
"export NODE_ROUND_TRIP_AUTH_ENDPOINT_INVALIDATE='${var.transcoder_image_red5pro_round_trip_auth_endpoint_invalidate}'",
"export NODE_CLOUDSTORAGE_ENABLE='${var.transcoder_red5pro_cloudstorage_enable}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_ACCESS_KEY='${var.transcoder_red5pro_google_storage_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_SECRET_ACCESS_KEY='${var.transcoder_red5pro_google_storage_secret_access_key}'",
"export NODE_CLOUDSTORAGE_GOOGLE_STORAGE_BUCKET_NAME='${var.transcoder_red5pro_google_storage_bucket_name}'",
"export NODE_CLOUDSTORAGE_POSTPROCESSOR_ENABLE='${var.transcoder_red5pro_cloudstorage_postprocessor_enable}'",
"cd /home/ubuntu/red5pro-installer/",
"sudo chmod +x /home/ubuntu/red5pro-installer/*.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_install_server_basic.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node.sh",
"sudo -E /home/ubuntu/red5pro-installer/r5p_config_node_apps_plugins.sh",
"sudo systemctl daemon-reload && sudo systemctl start red5pro",
"sleep 2"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "ubuntu"
private_key = "${local.private_ssh_key}"
}
}
lifecycle {
ignore_changes = all
}
tags = tolist([var.new_or_existing_network_tag_for_nodes])
}
# Red5 Pro Relay server instance
resource "google_compute_instance" "red5_relay_server" {
count = var.relay_image_create ? 1 : 0
name = "${var.name}-node-relay-image"
machine_type = var.relay_server_instance_type
zone = element(data.google_compute_zones.available_zone.names, count.index)
project = local.google_cloud_project