forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alb-cross-region-load-balance.yml
814 lines (748 loc) · 27.5 KB
/
alb-cross-region-load-balance.yml
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
ROSTemplateFormatVersion: '2015-09-01'
Transform: Aliyun::Terraform-v1.2
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- ecs1_instance_type
- ecs1_system_disk_category
Label:
default:
zh-cn: ECS1实例配置
en: ECS1 Instance Configuration
- Parameters:
- ecs2_instance_type
- ecs2_system_disk_category
Label:
default:
zh-cn: ECS2实例配置
en: ECS2 Instance Configuration
- Parameters:
- ecs3_instance_type
- ecs3_system_disk_category
Label:
default:
zh-cn: ECS3实例配置
en: ECS3 Instance Configuration
- Parameters:
- ecs_password
Label:
default:
zh-cn: ECS实例密码配置
en: Ecs Password Configuration
TemplateTags:
- acs:technical-solution:network:ALB实现跨地域负载均衡-tech_solu_10
Workspace:
main.tf: |+
locals {
create_ecs = true
cen_name = "cen-test"
tr1_name = "TR1"
tr2_name = "TR2"
tr3_name = "TR3"
region1 = "cn-chengdu"
region2 = "cn-shanghai"
region3 = "cn-qingdao"
zone11_id = "cn-chengdu-a"
zone12_id = "cn-chengdu-b"
zone21_id = "cn-shanghai-e"
zone22_id = "cn-shanghai-f"
zone31_id = "cn-qingdao-c"
zone32_id = "cn-qingdao-b"
vpc1_cidr = "172.16.0.0/16"
vsw11_cidr = "172.16.20.0/24"
vsw12_cidr = "172.16.21.0/24"
vpc2_cidr = "10.0.0.0/16"
vsw21_cidr = "10.0.20.0/24"
vsw22_cidr = "10.0.21.0/24"
vpc3_cidr = "192.168.0.0/16"
vsw31_cidr = "192.168.20.0/24"
vsw32_cidr = "192.168.21.0/24"
alb_name = "alb-test"
alb_chengdu_back_to_source_routing_cidr1 = "100.117.130.0/25"
alb_chengdu_back_to_source_routing_cidr2 = "100.117.130.128/25"
alb_chengdu_back_to_source_routing_cidr3 = "100.117.131.0/25"
alb_chengdu_back_to_source_routing_cidr4 = "100.117.131.128/25"
alb_chengdu_back_to_source_routing_cidr5 = "100.122.175.64/26"
alb_chengdu_back_to_source_routing_cidr6 = "100.122.175.128/26"
alb_chengdu_back_to_source_routing_cidr7 = "100.122.175.192/26"
alb_chengdu_back_to_source_routing_cidr8 = "100.122.176.0/26"
}
# 定义环境
provider "alicloud" {
alias = "region1"
region = local.region1
}
provider "alicloud" {
alias = "region2"
region = local.region2
}
provider "alicloud" {
alias = "region3"
region = local.region3
}
# cen实例
resource "alicloud_cen_instance" "cen" {
provider = alicloud.region1
cen_instance_name = local.cen_name
}
data "alicloud_cen_transit_router_service" "open" {
enable = "On"
}
# tr实例
resource "alicloud_cen_transit_router" "tr1" {
provider = alicloud.region1
transit_router_name = local.tr1_name
cen_id = alicloud_cen_instance.cen.id
depends_on = [data.alicloud_cen_transit_router_service.open]
}
resource "alicloud_cen_transit_router" "tr2" {
provider = alicloud.region2
transit_router_name = local.tr2_name
cen_id = alicloud_cen_instance.cen.id
depends_on = [data.alicloud_cen_transit_router_service.open]
}
resource "alicloud_cen_transit_router" "tr3" {
provider = alicloud.region3
transit_router_name = local.tr3_name
cen_id = alicloud_cen_instance.cen.id
depends_on = [data.alicloud_cen_transit_router_service.open]
}
# vpc连接至tr
resource "alicloud_cen_transit_router_vpc_attachment" "vpc_att1" {
provider = alicloud.region1
cen_id = alicloud_cen_instance.cen.id
transit_router_id = alicloud_cen_transit_router.tr1.transit_router_id
vpc_id = module.vpc1.vpc_id
zone_mappings {
zone_id = local.zone11_id
vswitch_id = module.vpc1.vsw1_id
}
zone_mappings {
zone_id = local.zone12_id
vswitch_id = module.vpc1.vsw2_id
}
}
resource "alicloud_cen_transit_router_vpc_attachment" "vpc_att2" {
provider = alicloud.region2
cen_id = alicloud_cen_instance.cen.id
transit_router_id = alicloud_cen_transit_router.tr2.transit_router_id
vpc_id = module.vpc2.vpc_id
zone_mappings {
zone_id = local.zone21_id
vswitch_id = module.vpc2.vsw1_id
}
zone_mappings {
zone_id = local.zone22_id
vswitch_id = module.vpc2.vsw2_id
}
}
resource "alicloud_cen_transit_router_vpc_attachment" "vpc_att3" {
provider = alicloud.region3
cen_id = alicloud_cen_instance.cen.id
transit_router_id = alicloud_cen_transit_router.tr3.transit_router_id
vpc_id = module.vpc3.vpc_id
zone_mappings {
zone_id = local.zone31_id
vswitch_id = module.vpc3.vsw1_id
}
zone_mappings {
zone_id = local.zone32_id
vswitch_id = module.vpc3.vsw2_id
}
}
# 配置tr路由及学习关系
resource "alicloud_cen_transit_router_route_table" "tr1_route_table" {
provider = alicloud.region1
transit_router_id = alicloud_cen_transit_router.tr1.transit_router_id
}
resource "alicloud_cen_transit_router_route_table_association" "tr1_table_association" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att1.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr1_table_propagation" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att1.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table" "tr2_route_table" {
provider = alicloud.region2
transit_router_id = alicloud_cen_transit_router.tr2.transit_router_id
}
resource "alicloud_cen_transit_router_route_table_association" "tr2_table_association" {
provider = alicloud.region2
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr2_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att2.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr2_table_propagation" {
provider = alicloud.region2
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr2_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att2.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table" "tr3_route_table" {
provider = alicloud.region3
transit_router_id = alicloud_cen_transit_router.tr3.transit_router_id
}
resource "alicloud_cen_transit_router_route_table_association" "tr3_table_association" {
provider = alicloud.region3
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr3_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att3.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr3_table_propagation" {
provider = alicloud.region3
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr3_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_vpc_attachment.vpc_att3.transit_router_attachment_id
}
# 自定义vpc路由条目
resource "alicloud_route_entry" "vpc1_route_entry" {
provider = alicloud.region1
for_each = toset([local.vpc1_cidr, local.vpc2_cidr, local.vpc3_cidr, local.vsw21_cidr, local.vsw31_cidr])
route_table_id = module.vpc1.route_table_id
destination_cidrblock = each.key
nexthop_type = "Attachment"
nexthop_id = alicloud_cen_transit_router_vpc_attachment.vpc_att1.transit_router_attachment_id
}
resource "alicloud_route_entry" "vpc2_route_entry" {
provider = alicloud.region2
for_each = toset([local.vpc1_cidr, local.vpc2_cidr, local.vpc3_cidr, local.alb_chengdu_back_to_source_routing_cidr1, local.alb_chengdu_back_to_source_routing_cidr2, local.alb_chengdu_back_to_source_routing_cidr3, local.alb_chengdu_back_to_source_routing_cidr4, local.alb_chengdu_back_to_source_routing_cidr5, local.alb_chengdu_back_to_source_routing_cidr6, local.alb_chengdu_back_to_source_routing_cidr7, local.alb_chengdu_back_to_source_routing_cidr8])
route_table_id = module.vpc2.route_table_id
destination_cidrblock = each.key
nexthop_type = "Attachment"
nexthop_id = alicloud_cen_transit_router_vpc_attachment.vpc_att2.transit_router_attachment_id
}
resource "alicloud_route_entry" "vpc3_route_entry" {
provider = alicloud.region3
for_each = toset([local.vpc1_cidr, local.vpc2_cidr, local.vpc3_cidr, local.alb_chengdu_back_to_source_routing_cidr1, local.alb_chengdu_back_to_source_routing_cidr2, local.alb_chengdu_back_to_source_routing_cidr3, local.alb_chengdu_back_to_source_routing_cidr4, local.alb_chengdu_back_to_source_routing_cidr5, local.alb_chengdu_back_to_source_routing_cidr6, local.alb_chengdu_back_to_source_routing_cidr7, local.alb_chengdu_back_to_source_routing_cidr8])
route_table_id = module.vpc3.route_table_id
destination_cidrblock = each.key
nexthop_type = "Attachment"
nexthop_id = alicloud_cen_transit_router_vpc_attachment.vpc_att3.transit_router_attachment_id
}
# tr1添加路由
resource "alicloud_cen_transit_router_route_entry" "tr1_route_entry" {
provider = alicloud.region1
for_each = toset([local.alb_chengdu_back_to_source_routing_cidr1, local.alb_chengdu_back_to_source_routing_cidr2, local.alb_chengdu_back_to_source_routing_cidr3, local.alb_chengdu_back_to_source_routing_cidr4, local.alb_chengdu_back_to_source_routing_cidr5, local.alb_chengdu_back_to_source_routing_cidr6, local.alb_chengdu_back_to_source_routing_cidr7, local.alb_chengdu_back_to_source_routing_cidr8])
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_route_entry_destination_cidr_block = each.key
transit_router_route_entry_next_hop_type = "Attachment"
transit_router_route_entry_next_hop_id = alicloud_cen_transit_router_vpc_attachment.vpc_att1.transit_router_attachment_id
}
# tr12跨地域连接
resource "alicloud_cen_transit_router_peer_attachment" "peer12_attachment" {
provider = alicloud.region1
transit_router_attachment_name = "TR1-TR2-Cross-Region-Test"
cen_id = alicloud_cen_instance.cen.id
transit_router_id = alicloud_cen_transit_router.tr1.transit_router_id
peer_transit_router_region_id = local.region2
peer_transit_router_id = alicloud_cen_transit_router.tr2.transit_router_id
auto_publish_route_enabled = true
}
# tr12之间配置路由及学习关系
resource "alicloud_cen_transit_router_route_table_association" "tr1_association12" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer12_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr1_propagation12" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer12_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_association" "tr1_association21" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr2_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer12_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr1_propagation21" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr2_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer12_attachment.transit_router_attachment_id
}
# tr13跨地域连接
resource "alicloud_cen_transit_router_peer_attachment" "peer13_attachment" {
provider = alicloud.region1
transit_router_attachment_name = "TR1-TR3-Cross-Region-Test"
cen_id = alicloud_cen_instance.cen.id
transit_router_id = alicloud_cen_transit_router.tr1.transit_router_id
peer_transit_router_region_id = local.region3
peer_transit_router_id = alicloud_cen_transit_router.tr3.transit_router_id
auto_publish_route_enabled = true
}
# tr13之间配置路由及学习关系
resource "alicloud_cen_transit_router_route_table_association" "tr1_association13" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer13_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr1_propagation13" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr1_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer13_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_association" "tr1_association31" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr3_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer13_attachment.transit_router_attachment_id
}
resource "alicloud_cen_transit_router_route_table_propagation" "tr1_propagation31" {
provider = alicloud.region1
transit_router_route_table_id = alicloud_cen_transit_router_route_table.tr3_route_table.transit_router_route_table_id
transit_router_attachment_id = alicloud_cen_transit_router_peer_attachment.peer13_attachment.transit_router_attachment_id
}
# vpc实例
module "vpc1" {
source = "./vpc"
providers = {
alicloud: alicloud.region1
}
vpc_cidr = local.vpc1_cidr
vsw1_cidr = local.vsw11_cidr
vsw2_cidr = local.vsw12_cidr
zone1_id = local.zone11_id
zone2_id = local.zone12_id
}
module "vpc2" {
source = "./vpc"
providers = {
alicloud: alicloud.region2
}
vpc_cidr = local.vpc2_cidr
vsw1_cidr = local.vsw21_cidr
vsw2_cidr = local.vsw22_cidr
zone1_id = local.zone21_id
zone2_id = local.zone22_id
}
module "vpc3" {
source = "./vpc"
providers = {
alicloud: alicloud.region3
}
vpc_cidr = local.vpc3_cidr
vsw1_cidr = local.vsw31_cidr
vsw2_cidr = local.vsw32_cidr
zone1_id = local.zone31_id
zone2_id = local.zone32_id
}
# ecs实例
module "ecs1" {
source = "./ecs"
providers = {
alicloud: alicloud.region1
}
create_ecs = local.create_ecs
vpc_id = module.vpc1.vpc_id
vsw_id = module.vpc1.vsw1_id
zone_id = local.zone11_id
instance_type = var.ecs1_instance_type
system_disk_category = var.ecs1_system_disk_category
ecs_password = var.ecs_password
instance_name = "ECS1"
install_script = file("${path.cwd}/scripts/install_nginx_ecs1.sh")
}
module "ecs2" {
source = "./ecs"
providers = {
alicloud: alicloud.region2
}
create_ecs = local.create_ecs
vpc_id = module.vpc2.vpc_id
vsw_id = module.vpc2.vsw1_id
zone_id = local.zone21_id
instance_type = var.ecs2_instance_type
system_disk_category = var.ecs2_system_disk_category
ecs_password = var.ecs_password
instance_name = "ECS2"
install_script = file("${path.cwd}/scripts/install_nginx_ecs2.sh")
}
module "ecs3" {
source = "./ecs"
providers = {
alicloud: alicloud.region3
}
create_ecs = local.create_ecs
vpc_id = module.vpc3.vpc_id
vsw_id = module.vpc3.vsw1_id
zone_id = local.zone31_id
instance_type = var.ecs3_instance_type
system_disk_category = var.ecs3_system_disk_category
ecs_password = var.ecs_password
instance_name = "ECS3"
install_script = file("${path.cwd}/scripts/install_nginx_ecs3.sh")
}
# alb实例
module "alb" {
source = "./alb"
providers = {
alicloud: alicloud.region1
}
vpc_id = module.vpc1.vpc_id
vsw1_id = module.vpc1.vsw1_id
zone11_id = local.zone11_id
vsw2_id = module.vpc1.vsw2_id
zone12_id = local.zone12_id
ecs2_ip = module.ecs2.ecs_ip
ecs3_ip = module.ecs3.ecs_ip
}
.metadata: |-
{
"ALIYUN::ROS::Interface": {
"ParameterGroups": [
{
"Parameters": [
"ecs1_instance_type",
"ecs1_system_disk_category"
],
"Label": {
"default": {
"zh-cn": "ECS1实例配置",
"en": "ECS1 Instance Configuration"
}
}
},
{
"Parameters": [
"ecs2_instance_type",
"ecs2_system_disk_category"
],
"Label": {
"default": {
"zh-cn": "ECS2实例配置",
"en": "ECS2 Instance Configuration"
}
}
},
{
"Parameters": [
"ecs3_instance_type",
"ecs3_system_disk_category"
],
"Label": {
"default": {
"zh-cn": "ECS3实例配置",
"en": "ECS3 Instance Configuration"
}
}
},
{
"Parameters": [
"ecs_password"
],
"Label": {
"default": {
"zh-cn": "ECS实例密码配置",
"en": "Ecs Password Configuration"
}
}
}
],
"TemplateTags": [
"acs:technical-solution:network:ALB实现跨地域负载均衡-tech_solu_10"
]
}
}
alb/main.tf: |+
variable "vpc_id" {}
variable "vsw1_id" {}
variable "zone11_id" {}
variable "vsw2_id" {}
variable "zone12_id" {}
variable "ecs2_ip" {}
variable "ecs3_ip" {}
terraform {
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "~> 1.1"
}
}
}
resource "alicloud_alb_load_balancer" "alb" {
vpc_id = var.vpc_id
address_type = "Intranet"
address_allocated_mode = "Fixed"
load_balancer_name = "alb_name"
load_balancer_edition = "Basic"
load_balancer_billing_config {
pay_type = "PayAsYouGo"
}
zone_mappings {
vswitch_id = var.vsw1_id
zone_id = var.zone11_id
}
zone_mappings {
vswitch_id = var.vsw2_id
zone_id = var.zone12_id
}
}
resource "alicloud_alb_server_group" "alb_rs" {
protocol = "HTTP"
vpc_id = var.vpc_id
server_group_name = "rs_test"
server_group_type = "Ip"
health_check_config {
health_check_enabled = false
}
sticky_session_config {
sticky_session_enabled = false
}
servers {
port = 80
server_id = var.ecs2_ip
server_ip = var.ecs2_ip
server_type = "Ip"
remote_ip_enabled = true
weight = 100
}
servers {
port = 80
server_id = var.ecs3_ip
server_ip = var.ecs3_ip
server_type = "Ip"
remote_ip_enabled = true
weight = 100
}
}
resource "alicloud_alb_listener" "alb_listener" {
load_balancer_id = alicloud_alb_load_balancer.alb.id
listener_protocol = "HTTP"
listener_port = 80
default_actions {
type = "ForwardGroup"
forward_group_config {
server_group_tuples {
server_group_id = alicloud_alb_server_group.alb_rs.id
}
}
}
}
output "alb_dns_name" {
value = alicloud_alb_load_balancer.alb.dns_name
}
ecs/main.tf: |
variable "create_ecs" {}
variable "vpc_id" {}
variable "vsw_id" {}
variable "zone_id" {}
variable "instance_type" {}
variable "system_disk_category" {}
variable "ecs_password" {}
variable "instance_name" {}
variable "install_script" {}
terraform {
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "~> 1.1"
}
}
}
resource "alicloud_security_group" "group" {
count = var.create_ecs? 1:0
vpc_id = var.vpc_id
}
resource "alicloud_security_group_rule" "rule" {
count = var.create_ecs? 1:0
type = "ingress"
ip_protocol = "all"
nic_type = "intranet"
policy = "accept"
port_range = "-1/-1"
priority = 1
security_group_id = alicloud_security_group.group[0].id
cidr_ip = "0.0.0.0/0"
}
resource "alicloud_instance" "ecs" {
count = var.create_ecs? 1:0
availability_zone = var.zone_id
security_groups = alicloud_security_group.group[0].*.id
instance_type = var.instance_type
system_disk_category = var.system_disk_category
image_id = "aliyun_3_x64_20G_alibase_20230727.vhd"
instance_name = var.instance_name
vswitch_id = var.vsw_id
password = var.ecs_password
user_data = var.install_script
}
output "ecs_ip" {
value = var.create_ecs? alicloud_instance.ecs[0].private_ip:"No ecs created"
}
outputs.tf: |
output "alb_dns_name" {
value = module.alb.alb_dns_name
description = <<EOT
{
"Description": "ALB域名"
}
EOT
}
scripts/install_nginx_ecs1.sh: |
#!/bin/sh
scripts/install_nginx_ecs2.sh: |
#!/bin/sh
echo "Hello World ! This is ECS02." > index.html
nohup python3 -m http.server 80 &
scripts/install_nginx_ecs3.sh: |
#!/bin/sh
echo "Hello World ! This is ECS03." > index.html
nohup python3 -m http.server 80 &
variables.tf: |+
variable "ecs1_instance_type" {
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Instance::InstanceType",
"AssociationPropertyMetadata": {
"RegionId": "cn-chengdu",
"ZoneId": "cn-chengdu-a"
},
"Label": {
"zh-cn": "ECS1实例规格",
"en": "ECS1 Instance type."
}
}
EOT
default = "ecs.t5-lc2m1.nano"
}
variable "ecs1_system_disk_category" {
type = string
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Disk::SystemDiskCategory",
"AssociationPropertyMetadata": {
"RegionId": "cn-chengdu",
"ZoneId": "cn-chengdu-a",
"InstanceType": "$${ecs1_instance_type}"
},
"Label": {
"zh-cn": "系统盘类型",
"en": "System disk category"
}
}
EOT
default = "cloud_efficiency"
}
variable "ecs2_instance_type" {
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Instance::InstanceType",
"AssociationPropertyMetadata": {
"RegionId": "cn-shanghai",
"ZoneId": "cn-shanghai-e"
},
"Label": {
"zh-cn": "ECS2实例规格",
"en": "ECS2 Instance type."
}
}
EOT
default = "ecs.t5-lc2m1.nano"
}
variable "ecs2_system_disk_category" {
type = string
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Disk::SystemDiskCategory",
"AssociationPropertyMetadata": {
"RegionId": "cn-shanghai",
"ZoneId": "cn-shanghai-e",
"InstanceType": "$${ecs2_instance_type}"
},
"Label": {
"zh-cn": "系统盘类型",
"en": "System disk category"
}
}
EOT
default = "cloud_efficiency"
}
variable "ecs3_instance_type" {
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Instance::InstanceType",
"AssociationPropertyMetadata": {
"RegionId": "cn-qingdao",
"ZoneId": "cn-qingdao-c"
},
"Label": {
"zh-cn": "ECS3实例规格",
"en": "ECS3 Instance type."
}
}
EOT
default = "ecs.t5-lc2m1.nano"
}
variable "ecs3_system_disk_category" {
type = string
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Disk::SystemDiskCategory",
"AssociationPropertyMetadata": {
"RegionId": "cn-qingdao",
"ZoneId": "cn-qingdao-c",
"InstanceType": "$${ecs3_instance_type}"
},
"Label": {
"zh-cn": "系统盘类型",
"en": "System disk category"
}
}
EOT
default = "cloud_efficiency"
}
variable "ecs_password" {
type = string
sensitive = true
description = <<EOT
{
"AssociationProperty": "ALIYUN::ECS::Instance::Password",
"Label": {
"zh-cn": "登录密码",
"en": "Ecs password"
},
"Description":{
"zh-cn": "3个ecs实例相同的root用户登录密码",
"en": "Three ECS instances with the same root user login password."
}
}
EOT
}
vpc/main.tf: |
variable "vpc_cidr" {}
variable "vsw1_cidr" {}
variable "vsw2_cidr" {}
variable "zone1_id" {}
variable "zone2_id" {}
terraform {
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "~> 1.1"
}
}
}
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc_test"
cidr_block = var.vpc_cidr
}
resource "alicloud_vswitch" "vsw1" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vsw1_cidr
zone_id = var.zone1_id
}
resource "alicloud_vswitch" "vsw2" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vsw2_cidr
zone_id = var.zone2_id
}
output "vpc_id" {
value = alicloud_vpc.vpc.id
}
output "vsw1_id" {
value = alicloud_vswitch.vsw1.id
}
output "vsw2_id" {
value = alicloud_vswitch.vsw2.id
}
output "route_table_id" {
value = alicloud_vpc.vpc.route_table_id
}