-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
258 lines (216 loc) · 6.04 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
terraform {
required_providers {
aws = {
version = ">= 4.0"
source = "hashicorp/aws"
}
}
}
provider "aws" {}
data "aws_region" "current" {}
data "aws_availability_zones" "available" {
state = "available"
exclude_names = ["us-east-1e"] # AMG not supported
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~>4.0"
name = "validationcloud-test"
cidr = "10.5.0.0/16"
azs = data.aws_availability_zones.available.names
public_subnets = ["10.5.0.0/20", "10.5.16.0/20", "10.5.32.0/20", "10.5.48.0/20", "10.5.64.0/20"]
private_subnets = ["10.5.80.0/20", "10.5.96.0/20", "10.5.112.0/20", "10.5.128.0/20", "10.5.144.0/20"]
database_subnets = ["10.5.160.0/20", "10.5.176.0/20", "10.5.192.0/20", "10.5.208.0/20", "10.5.224.0/20"]
enable_nat_gateway = false
enable_vpn_gateway = false
create_database_subnet_group = false
}
resource "aws_iam_role" "this" {
name = "geth-nodes"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
data "aws_iam_policy_document" "log_retention" {
statement {
actions = ["logs:PutRetentionPolicy"]
resources = ["*"]
}
}
resource "aws_iam_policy" "this" {
name = "log-retention"
path = "/"
policy = data.aws_iam_policy_document.log_retention.json
}
resource "aws_iam_role_policy_attachment" "log_retention" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.this.arn
}
resource "aws_iam_role_policy_attachment" "ssm" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "cloudwatch" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
resource "aws_iam_role_policy_attachment" "aps" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess"
}
resource "aws_iam_instance_profile" "this" {
name = "geth-instance-profile"
role = aws_iam_role.this.name
}
resource "aws_cloudwatch_log_group" "this" {
name = var.cloudwatch_logs_group_name
skip_destroy = false
retention_in_days = 3
}
module "geth_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.17.0"
name = "geth-sg"
description = "Security group for Geth nodes"
vpc_id = module.vpc.vpc_id
ingress_with_cidr_blocks = [
{
from_port = 30303
to_port = 30303
protocol = "tcp"
description = "P2P"
cidr_blocks = "0.0.0.0/0"
},
{
from_port = 30303
to_port = 30303
protocol = "udp"
description = "P2P"
cidr_blocks = "0.0.0.0/0"
},
{
from_port = 8545
to_port = 8545
protocol = "tcp"
description = "JSON RPC"
cidr_blocks = "0.0.0.0/0"
},
]
ingress_with_source_security_group_id = [
{
from_port = 9090
to_port = 9090
protocol = "tcp"
description = "Prometheus"
source_security_group_id = module.grafana_sg.security_group_id
}]
egress_rules = ["all-all"]
}
module "grafana_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.17.0"
name = "geth-grafana-sg"
description = "Security group for Geth Grafana instance"
vpc_id = module.vpc.vpc_id
egress_rules = ["all-all"]
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "state"
values = ["available"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_grafana_role_association" "this" {
role = "ADMIN"
user_ids = var.sso_user_ids
workspace_id = aws_grafana_workspace.this.id
}
resource "aws_grafana_workspace" "this" {
account_access_type = "CURRENT_ACCOUNT"
authentication_providers = ["AWS_SSO"]
permission_type = "SERVICE_MANAGED"
role_arn = aws_iam_role.assume.arn
grafana_version = "8.4"
name = "geth"
vpc_configuration {
security_group_ids = [module.grafana_sg.security_group_id]
subnet_ids = module.vpc.public_subnets
}
provisioner "local-exec" {
command = "./bootstrap_grafana.sh ${self.id} ${aws_instance.node.private_ip}"
working_dir = "scripts"
}
lifecycle {
ignore_changes = [
vpc_configuration["subnet_ids"]
]
}
}
resource "aws_iam_role" "assume" {
name = "grafana-assume"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "grafana.amazonaws.com"
}
},
]
})
}
resource "aws_instance" "node" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
associate_public_ip_address = true
iam_instance_profile = aws_iam_instance_profile.this.name
subnet_id = module.vpc.public_subnets[0]
vpc_security_group_ids = [module.geth_sg.security_group_id]
user_data = templatefile("${path.module}/scripts/userdata.tftpl", { cloudwatch_logs_group_name = var.cloudwatch_logs_group_name, region = data.aws_region.current.name })
ebs_block_device {
delete_on_termination = true
device_name = "/dev/sdf"
iops = 10000
volume_size = 2000
volume_type = "gp3"
}
lifecycle {
ignore_changes = [
ami
]
}
tags = {
Name = "geth-node"
}
}