forked from Developer-DAO/ario-gateway-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.tf
160 lines (130 loc) · 3.63 KB
/
instances.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
resource "aws_security_group" "ar_io_nodes_sg" {
name = "ar-io-node-${var.alias}-sg"
description = "Allow inbound traffic from the internet"
vpc_id = var.vpc_id
ingress {
description = "Allow inbound HTTP traffic on port 3000 in the private VPC"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = [var.vpc_cidr]
}
egress {
description = "Allow outbound traffic to the internet"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "ubuntu" {
most_recent = true
# 20.04 (not later) is required for CodeDeploy
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical Ltd.
}
resource "aws_cloudwatch_log_group" "ar_io_nodes_log_group" {
name = "ar-io-nodes-${var.alias}"
retention_in_days = 365
}
resource "aws_launch_template" "ar_io_nodes_launch_template" {
name = "ar-io-nodes-${var.alias}"
image_id = data.aws_ami.ubuntu.id
instance_type = var.instance_type
monitoring {
enabled = true
}
vpc_security_group_ids = [
aws_security_group.ar_io_nodes_sg.id
]
iam_instance_profile {
name = aws_iam_instance_profile.ar_io_nodes_instance_profile.name
}
user_data = base64encode(templatefile("${path.module}/resources/userdata.sh", {
region = var.region,
fs_id = aws_efs_file_system.cache_fs.id
log_group_name = aws_cloudwatch_log_group.ar_io_nodes_log_group.name
}))
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 500
volume_type = "gp3"
}
}
tag_specifications {
resource_type = "instance"
tags = {
Environment = var.alias
Service = "ar-io-nodes"
DeploymentGroup = "ar-io-nodes-${var.alias}"
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "ar_io_nodes_asg" {
name = "ar-io-nodes-${var.alias}"
desired_capacity = 2
max_size = 2
min_size = 1
vpc_zone_identifier = var.private_subnets
launch_template {
id = aws_launch_template.ar_io_nodes_launch_template.id
version = aws_launch_template.ar_io_nodes_launch_template.latest_version
}
target_group_arns = [
aws_lb_target_group.ar_io_nodes_tg.arn
]
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}
}
resource "aws_efs_file_system" "cache_fs" {
creation_token = "ar-io-nodes-${var.alias}-cache-fs"
tags = {
Environment = var.alias
Service = "ar-io-nodes"
}
lifecycle {
# As this file system will contain database snapshots,
# it should not get deleted or replace accident.
prevent_destroy = true
# workaround for old version of terraform (manually to 'elastic')
ignore_changes = [throughput_mode]
}
}
resource "aws_efs_mount_target" "cache_fs" {
count = length(var.private_subnets)
file_system_id = aws_efs_file_system.cache_fs.id
security_groups = [aws_security_group.cache_fs.id]
subnet_id = var.private_subnets[count.index]
}
resource "aws_security_group" "cache_fs" {
name = "ar-io-nodes-${var.alias}-cache-fs"
description = "Allow access to EFS from the AR IO nodes"
vpc_id = var.vpc_id
ingress {
from_port = 2049
to_port = 2049
protocol = "tcp"
cidr_blocks = [var.vpc_cidr]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}