-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
140 lines (118 loc) · 3.07 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
resource "aws_default_vpc" "default" {
tags {
Name = "Default VPC"
}
}
resource "aws_default_subnet" "default" {
availability_zone = "${var.availability_zone}"
tags {
Name = "Default subnet for availablity zone ${var.availability_zone}"
}
}
resource "aws_security_group" "geth" {
name = "ethereum-geth-${var.network}"
description = "Ethereum geth container"
vpc_id = "${aws_default_vpc.default.id}"
ingress {
from_port = 8545
to_port = 8545
protocol = "tcp"
description = "web3 http port"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8546
to_port = 8546
protocol = "tcp"
description = "web3 websocket port"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 30303
to_port = 30303
protocol = "udp"
description = "Ethereum geth node p2p port"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_iam_role" "geth" {
name = "ethereum-geth-${var.network}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_instance_profile" "geth" {
name = "ethereum-geth-${var.network}"
role = "${aws_iam_role.geth.name}"
}
resource "aws_key_pair" "geth" {
key_name = "ethereum-geth-${var.network}-instance_key"
public_key = "${var.public_key}"
}
data "aws_ami" "ubuntu" {
# using the most recent ubuntu AMI is causing terraform to want to
# destroy/recreate EC2 instances for no other reason than it has found
# a more recent ubuntu AMI, which seems to happen frequently. Hardcoding
# For resources that consume this data, they should ignore updates
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "geth" {
ami = "${data.aws_ami.ubuntu.id}"
lifecycle = {
ignore_changes = "ami"
}
instance_type = "${var.instance_type}"
subnet_id = "${aws_default_subnet.default.id}"
vpc_security_group_ids = ["${aws_security_group.geth.id}"]
associate_public_ip_address = true
iam_instance_profile = "${aws_iam_instance_profile.geth.name}"
key_name = "${aws_key_pair.geth.key_name}"
tags {
Name = "Ethereum geth ${var.network}"
}
# Storage must be SSD, otherwise blocks are mined faster than they can be written to storage
root_block_device = {
volume_type = "io1"
iops = "32000"
volume_size = "${var.volume_size}"
}
user_data = <<EOF
#!/bin/bash
ETH_NETWORK=${var.network}
DEVICE_NAME=${var.volume_device_name}
${file("${path.module}/provision-geth.sh")}
EOF
}