-
Notifications
You must be signed in to change notification settings - Fork 102
/
main.tf
148 lines (125 loc) · 3.96 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
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.region}"
}
resource "aws_key_pair" "alex" {
key_name = "alex-key"
public_key = "${file(var.ssh_pubkey_file)}"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
}
resource "aws_route_table" "external" {
vpc_id = "${aws_vpc.main.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main.id}"
}
}
resource "aws_route_table_association" "external-main" {
subnet_id = "${aws_subnet.main.id}"
route_table_id = "${aws_route_table.external.id}"
}
# TODO: figure out how to support creating multiple subnets, one for each
# availability zone.
resource "aws_subnet" "main" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "${var.availability_zone}"
}
resource "aws_internet_gateway" "main" {
vpc_id = "${aws_vpc.main.id}"
}
resource "aws_security_group" "load_balancers" {
name = "load_balancers"
description = "Allows all traffic"
vpc_id = "${aws_vpc.main.id}"
# TODO: do we need to allow ingress besides TCP 80 and 443?
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# TODO: this probably only needs egress to the ECS security group.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "ecs" {
name = "ecs"
description = "Allows all traffic"
vpc_id = "${aws_vpc.main.id}"
# TODO: remove this and replace with a bastion host for SSHing into
# individual machines.
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
security_groups = ["${aws_security_group.load_balancers.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_ecs_cluster" "main" {
name = "${var.ecs_cluster_name}"
}
resource "aws_autoscaling_group" "ecs-cluster" {
availability_zones = ["${var.availability_zone}"]
name = "ECS ${var.ecs_cluster_name}"
min_size = "${var.autoscale_min}"
max_size = "${var.autoscale_max}"
desired_capacity = "${var.autoscale_desired}"
health_check_type = "EC2"
launch_configuration = "${aws_launch_configuration.ecs.name}"
vpc_zone_identifier = ["${aws_subnet.main.id}"]
}
resource "aws_launch_configuration" "ecs" {
name = "ECS ${var.ecs_cluster_name}"
image_id = "${lookup(var.amis, var.region)}"
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.ecs.id}"]
iam_instance_profile = "${aws_iam_instance_profile.ecs.name}"
# TODO: is there a good way to make the key configurable sanely?
key_name = "${aws_key_pair.alex.key_name}"
associate_public_ip_address = true
user_data = "#!/bin/bash\necho ECS_CLUSTER='${var.ecs_cluster_name}' > /etc/ecs/ecs.config"
}
resource "aws_iam_role" "ecs_host_role" {
name = "ecs_host_role"
assume_role_policy = "${file("policies/ecs-role.json")}"
}
resource "aws_iam_role_policy" "ecs_instance_role_policy" {
name = "ecs_instance_role_policy"
policy = "${file("policies/ecs-instance-role-policy.json")}"
role = "${aws_iam_role.ecs_host_role.id}"
}
resource "aws_iam_role" "ecs_service_role" {
name = "ecs_service_role"
assume_role_policy = "${file("policies/ecs-role.json")}"
}
resource "aws_iam_role_policy" "ecs_service_role_policy" {
name = "ecs_service_role_policy"
policy = "${file("policies/ecs-service-role-policy.json")}"
role = "${aws_iam_role.ecs_service_role.id}"
}
resource "aws_iam_instance_profile" "ecs" {
name = "ecs-instance-profile"
path = "/"
roles = ["${aws_iam_role.ecs_host_role.name}"]
}