-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
141 lines (118 loc) · 3.79 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
locals {
data_device = "/dev/xvdcv"
}
data "aws_ami" "ecs_ami" {
most_recent = true
owners = var.aws_ami_owners
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-${var.ami_version}-amazon-ecs-optimized"]
}
}
data "aws_ebs_snapshot" "data" {
most_recent = "true"
owners = ["self"]
filter {
name = "tag:Name"
values = [var.ebs_snapshot_matcher]
}
}
data "template_file" "user_data" {
template = file("${path.module}/templates/user_data.tpl")
vars = {
additional_user_data_script = var.additional_user_data_script
cluster_name = aws_ecs_cluster.cluster.name
docker_storage_size = var.docker_storage_size
dockerhub_token = var.dockerhub_token
dockerhub_email = var.dockerhub_email
data_device = local.data_device
}
}
data "aws_vpc" "vpc" {
id = var.vpc_id
}
resource "aws_launch_configuration" "ecs" {
name_prefix = coalesce(var.name_prefix, "ecs-${var.name}-")
image_id = var.ami == "" ? format("%s", data.aws_ami.ecs_ami.id) : var.ami # Workaround until 0.9.6
instance_type = var.instance_type
key_name = var.key_name
iam_instance_profile = aws_iam_instance_profile.ecs_profile.name
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibility in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
security_groups = concat([aws_security_group.ecs.id], var.security_group_ids)
associate_public_ip_address = var.associate_public_ip_address
ebs_block_device {
device_name = "/dev/xvdcz"
volume_size = var.docker_storage_size
volume_type = "gp2"
delete_on_termination = true
}
ebs_block_device {
snapshot_id = data.aws_ebs_snapshot.data.id
device_name = local.data_device
volume_type = "gp2"
delete_on_termination = true
}
user_data = coalesce(var.user_data, data.template_file.user_data.rendered)
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "ecs" {
name_prefix = "asg-${aws_launch_configuration.ecs.name}-"
vpc_zone_identifier = var.subnet_id
launch_configuration = aws_launch_configuration.ecs.name
min_size = var.min_servers
max_size = var.max_servers
desired_capacity = var.servers
termination_policies = ["OldestLaunchConfiguration", "ClosestToNextInstanceHour", "Default"]
tags = concat([
{
key = "Name"
value = "${var.name} ${var.tagName}"
propagate_at_launch = true
}], var.extra_tags)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "ecs" {
name = "ecs-sg-${var.name}"
description = "Container Instance Allowed Ports"
vpc_id = data.aws_vpc.vpc.id
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
}
ingress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = var.allowed_cidr_blocks
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "ecs-sg-${var.name}"
}
}
# Make this a var that an get passed in?
resource "aws_ecs_cluster" "cluster" {
name = var.name
}