-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecs_service_ec2_ghost.tf
132 lines (100 loc) · 3.07 KB
/
ecs_service_ec2_ghost.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
resource "aws_ecs_task_definition" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
family = "ghost"
container_definitions = templatefile("templates/tasks/ghost.json", {
url = "http://${aws_alb.ecs_service_ghost[0].dns_name}"
database_host = aws_rds_cluster.ghost[0].endpoint
database_name = aws_rds_cluster.ghost[0].database_name
database_user = aws_rds_cluster.ghost[0].master_username
database_port = aws_rds_cluster.ghost[0].port
})
}
resource "aws_ecs_service" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
cluster = aws_ecs_cluster.default.name
name = "tf-cluster-ghost"
task_definition = aws_ecs_task_definition.ghost[0].arn
desired_count = "2"
enable_ecs_managed_tags = "true"
propagate_tags = "SERVICE"
# to avoid possible race condition error on creation
depends_on = [aws_alb.ecs_service_ghost]
load_balancer {
target_group_arn = aws_alb_target_group.ghost[0].arn
container_name = "ghost"
container_port = 2368
}
# https://github.com/hashicorp/terraform-provider-aws/issues/11351
lifecycle {
ignore_changes = [
capacity_provider_strategy
]
}
}
resource "aws_alb" "ecs_service_ghost" {
count = var.enable_ec2_ghost ? 1 : 0
name = "ghost-ec2"
subnets = [
module.vpc.subnet_public1,
module.vpc.subnet_public2,
module.vpc.subnet_public3,
]
security_groups = [
module.vpc.sg_allow_egress,
module.vpc.sg_allow_80,
]
}
resource "aws_alb_listener" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
default_action {
target_group_arn = aws_alb_target_group.ghost[0].arn
type = "forward"
}
load_balancer_arn = aws_alb.ecs_service_ghost[0].arn
port = 80
}
resource "aws_alb_target_group" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
name = "ghost-ec2"
vpc_id = module.vpc.vpc_id
port = 2368
protocol = "HTTP"
deregistration_delay = 0
health_check {
healthy_threshold = 2
unhealthy_threshold = 10
interval = 5
timeout = 2
}
}
resource "aws_rds_cluster" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
cluster_identifier = "ghost"
engine = "aurora-mysql"
engine_version = "5.7.12"
database_name = "ghostdb"
master_username = "ghost"
master_password = "ghost_password"
skip_final_snapshot = true
final_snapshot_identifier = "ghost"
db_subnet_group_name = aws_db_subnet_group.ghost[0].name
vpc_security_group_ids = [module.vpc.sg_allow_vpc]
}
resource "aws_rds_cluster_instance" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
cluster_identifier = aws_rds_cluster.ghost[0].id
engine = "aurora-mysql"
engine_version = "5.7.12"
instance_class = "db.t2.small"
}
resource "aws_db_subnet_group" "ghost" {
count = var.enable_ec2_ghost ? 1 : 0
subnet_ids = [
module.vpc.subnet_private1,
module.vpc.subnet_private2,
module.vpc.subnet_private3,
]
}
output "ghost" {
value = join("", aws_alb.ecs_service_ghost.*.dns_name)
}