-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfargate.tf
123 lines (101 loc) · 3.91 KB
/
fargate.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
module "ecs_task_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "3.18.0"
vpc_id = module.vpc.vpc_id
name = "${var.name_prefix}-ecs-task-sg"
use_name_prefix = false
description = "Access to public Application Load Balancer"
computed_ingress_with_source_security_group_id = [
{
rule = "http-80-tcp"
description = "Connections and Health Checks from Application Load Balancer"
source_security_group_id = module.alb_sg.this_security_group_id
}
]
number_of_computed_ingress_with_source_security_group_id = 1
egress_cidr_blocks = ["0.0.0.0/0"]
egress_rules = ["all-all"]
tags = var.common_tags
}
resource "aws_ecs_cluster" "wp" {
name = "${var.name_prefix}-fargate-cluster"
}
resource "aws_ecs_service" "wp" {
name = "${var.name_prefix}-fargate-service"
cluster = aws_ecs_cluster.wp.id
launch_type = "FARGATE"
platform_version = "1.4.0"
task_definition = aws_ecs_task_definition.wordpress.arn
desired_count = var.task_desired_count
network_configuration {
security_groups = [
module.efs_sg.this_security_group_id,
module.ecs_task_sg.this_security_group_id
]
subnets = module.vpc.private_subnets
}
load_balancer {
target_group_arn = module.alb.target_group_arns[0] // register to "blue" target by default
container_name = var.container_name
container_port = var.container_port
}
tags = var.common_tags
lifecycle {
ignore_changes = [desired_count]
}
}
resource "aws_appautoscaling_target" "wp-service" {
min_capacity = var.ecs_service_autoscaling_min_capacity
max_capacity = var.ecs_service_autoscaling_max_capacity
resource_id = "service/${aws_ecs_cluster.wp.name}/${aws_ecs_service.wp.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "wp-service" {
name = "${var.name_prefix}.fargate-service-autoscaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.wp-service.id
scalable_dimension = aws_appautoscaling_target.wp-service.scalable_dimension
service_namespace = aws_appautoscaling_target.wp-service.service_namespace
target_tracking_scaling_policy_configuration {
target_value = var.ecs_service_autoscaling_cpu_average_utilization_target
scale_in_cooldown = var.ecs_service_autoscaling_scale_in_cooldown
scale_out_cooldown = var.ecs_service_autoscaling_scale_out_cooldown
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
}
}
resource "aws_ecs_task_definition" "wordpress" {
family = "${var.name_prefix}-wordpress"
execution_role_arn = module.ecs_task_execution_role.this_iam_role_arn
task_role_arn = module.ecs_task_role.this_iam_role_arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_cpu
memory = var.task_memory
container_definitions = templatefile(
"wordpress_container_definition.json",
{
db_user = aws_ssm_parameter.db_master_user.arn,
db_password = aws_ssm_parameter.db_master_password.arn,
db_host = module.wp_db.this_rds_cluster_endpoint,
container_image = var.container_image_url,
container_name = var.container_name,
container_port = var.container_port,
log_group = aws_cloudwatch_log_group.wordpress.name,
region = data.aws_region.current.name
}
)
volume {
name = "efs"
efs_volume_configuration {
file_system_id = aws_efs_file_system.persistent_data.id
}
}
}
resource "aws_cloudwatch_log_group" "wordpress" {
name = "/${var.name_prefix}/wp-task"
tags = var.common_tags
retention_in_days = var.log_retention_in_days
}