-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvirtual_node.tf
144 lines (123 loc) · 3.72 KB
/
virtual_node.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
resource "aws_appmesh_virtual_node" "default" {
count = "${var.virtual_nodes_count}"
name = "${lookup(var.virtual_nodes[count.index], "service_name")}"
mesh_name = "${local.app_mesh_id}"
spec {
listener {
port_mapping {
port = "${lookup(var.virtual_nodes[count.index], "port")}"
protocol = "${lookup(var.virtual_nodes[count.index], "protocol")}"
}
}
service_discovery {
dns {
hostname = "${format("%s.%s", lookup(var.virtual_nodes[count.index], "service_discovery_hostname_prefix"), var.ecs_services_domain)}"
}
}
}
}
resource "aws_appmesh_virtual_node" "backend" {
count = "${var.virtual_backend_nodes_count}"
name = "${lookup(var.virtual_backend_nodes[count.index], "service_name")}"
mesh_name = "${local.app_mesh_id}"
spec {
backend {
## Backend one
virtual_service {
virtual_service_name = "${
element(
formatlist(
"%s.%s",
split(",",
replace(
lookup(var.virtual_backend_nodes[count.index], "backend_virtual_service_hostname_prefixes")
, " ", "")),
var.ecs_services_domain),
0 % length(
split(",",
lookup(var.virtual_backend_nodes[count.index], "backend_virtual_service_hostname_prefixes")
)
)
)
}"
}
}
backend {
## Backend two
virtual_service {
virtual_service_name = "${
element(
formatlist(
"%s.%s",
split(",",
replace(
lookup(var.virtual_backend_nodes[count.index], "backend_virtual_service_hostname_prefixes")
, " ", "")),
var.ecs_services_domain),
1 % length(
split(",",
lookup(var.virtual_backend_nodes[count.index], "backend_virtual_service_hostname_prefixes")
)
)
)
}"
}
}
listener {
port_mapping {
port = "${lookup(var.virtual_backend_nodes[count.index], "port")}"
protocol = "${lookup(var.virtual_backend_nodes[count.index], "protocol")}"
}
}
service_discovery {
dns {
hostname = "${format("%s.%s", lookup(var.virtual_backend_nodes[count.index], "service_discovery_hostname_prefix"), var.ecs_services_domain)}"
}
}
}
depends_on = ["aws_appmesh_virtual_service.router", "aws_appmesh_virtual_service.node"]
}
## variables.tf
variable "virtual_nodes_count" {
default = "0"
}
variable "virtual_backend_nodes_count" {
default = "0"
}
variable "virtual_nodes" {
type = "list"
description = <<EOF
A list of maps that specifies the virtual node details
```
virtual_nodes = [{
service_discovery_hostname_prefix = "colorteller-red"
service_name = "colorteller-red-vn"
port = "8080"
protocol = "http"
}]
```
EOF
default = []
}
variable "virtual_backend_nodes" {
type = "list"
description = <<EOF
A list of maps that specifies the virtual node details with a backend.
Separate multiple backend virtual service hostname prefixes using a comma.i.e. "serviceA,serviceB"
This can support up to 2 nodes.
```
virtual_backend_nodes = [{
backend_virtual_service_hostname_prefixes = "tcpecho,colorteller"
service_discovery_hostname_prefix = "colorgateway"
service_name = "colorgateway-vn"
port = "9080"
protocol = "http"
}]
```
EOF
default = []
}
## outputs.tf
output "virtual_node_ids" {
value = "${aws_appmesh_virtual_node.default.*.id}"
}