-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
147 lines (129 loc) · 3.42 KB
/
main.tf
File metadata and controls
147 lines (129 loc) · 3.42 KB
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
random = {
source = "hashicorp/random"
version = "3.1.0"
}
}
}
resource "random_pet" "cluster" {
keepers = {
cluster = var.cluster_name
}
}
resource "aws_autoscaling_group" "ecs" {
name = "ecs-${var.cluster_name}-asg-${random_pet.cluster.id}"
min_size = var.cluster_min_size
max_size = var.cluster_max_size
vpc_zone_identifier = var.subnets
launch_configuration = aws_launch_configuration.ecs_instance.name
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_instance_profile" "ecs_instance" {
name = "ecs_instance_profile-${random_pet.cluster.id}"
role = aws_iam_role.ecs_instance_role.name
}
resource "aws_iam_role" "ecs_instance_role" {
name = "ecs_instance_role-${random_pet.cluster.id}"
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": [
"sts:AssumeRole"
]
}
]
}
EOF
}
resource "aws_iam_policy" "instance_policy" {
name = "ecs-instance-policy-${random_pet.cluster.id}"
description = "ECS Instance Policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeTags",
"ecs:CreateCluster",
"ecs:DeregisterContainerInstance",
"ecs:DiscoverPollEndpoint",
"ecs:Poll",
"ecs:RegisterContainerInstance",
"ecs:StartTelemetrySession",
"ecs:UpdateContainerInstancesState",
"ecs:Submit*",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
${var.extra_instance_policy_actions}
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "instance_policy_attach" {
role = aws_iam_role.ecs_instance_role.name
policy_arn = aws_iam_policy.instance_policy.arn
}
resource "aws_launch_configuration" "ecs_instance" {
name = "ecs-instance-${var.cluster_name}-${random_pet.cluster.id}"
image_id = var.ecs_ami
instance_type = var.ecs_instance_type
key_name = var.ecs_instance_key
iam_instance_profile = aws_iam_instance_profile.ecs_instance.name
user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${var.cluster_name} >> /etc/ecs/ecs.config
${var.extra_user_data}
EOF
security_groups = [aws_security_group.cluster_instance.id]
lifecycle {
create_before_destroy = true
}
}
resource "aws_ecs_cluster" "cluster" {
name = var.cluster_name
}
resource "aws_security_group" "cluster_instance" {
name = "ecs-cluster-${var.cluster_name}-${random_pet.cluster.id}"
vpc_id = var.vpc_id
ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
self = true
}
ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
security_groups = var.allowed_sgs
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}