-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.tf
175 lines (162 loc) · 5.73 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#------------------------------------------------------------------------------
# AWS Cloudwatch Logs
#------------------------------------------------------------------------------
module "aws_cw_logs" {
source = "cn-terraform/cloudwatch-logs/aws"
version = "1.0.12"
# source = "../terraform-aws-cloudwatch-logs"
create_kms_key = var.create_kms_key
log_group_kms_key_id = var.log_group_kms_key_id
log_group_retention_in_days = var.log_group_retention_in_days
logs_path = "/ecs/service/${var.name_prefix}-jenkins-master"
}
#------------------------------------------------------------------------------
# Locals
#------------------------------------------------------------------------------
locals {
container_name = "${var.name_prefix}-jenkins"
healthcheck = {
command = ["CMD-SHELL", "curl -f http://localhost:8080 || exit 1"]
retries = 3
timeout = 5
interval = 30
startPeriod = 120
}
td_port_mappings = [
{
containerPort = 8080
hostPort = 8080
protocol = "tcp"
},
{
containerPort = 50000
hostPort = 50000
protocol = "tcp"
}
]
service_http_ports = {
ui = {
listener_port = 80
target_group_port = 8080
},
workers = {
listener_port = 50000
target_group_port = 50000
}
}
service_https_ports = {}
}
#------------------------------------------------------------------------------
# ECS Cluster
#------------------------------------------------------------------------------
module "ecs-cluster" {
source = "cn-terraform/ecs-cluster/aws"
version = "1.0.11"
# source = "../terraform-aws-ecs-cluster"
name = "${var.name_prefix}-jenkins"
}
#------------------------------------------------------------------------------
# EFS
#------------------------------------------------------------------------------
resource "aws_efs_file_system" "jenkins_data" {
creation_token = "${var.name_prefix}-jenkins-efs"
tags = {
Name = "${var.name_prefix}-jenkins-efs"
}
}
resource "aws_security_group" "jenkins_data_allow_nfs_access" {
name = "${var.name_prefix}-jenkins-efs-allow-nfs"
description = "Allow NFS inbound traffic to EFS"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.name_prefix}-jenkins-efs-allow-nfs"
}
}
data "aws_subnet" "private_subnets" {
count = length(var.private_subnets_ids)
id = element(var.private_subnets_ids, count.index)
}
resource "aws_security_group_rule" "jenkins_data_allow_nfs_access_rule" {
security_group_id = aws_security_group.jenkins_data_allow_nfs_access.id
type = "ingress"
from_port = 2049
to_port = 2049
protocol = "tcp"
source_security_group_id = module.ecs-fargate-service.ecs_tasks_sg_id
}
resource "aws_efs_mount_target" "jenkins_data_mount_targets" {
count = length(var.private_subnets_ids)
file_system_id = aws_efs_file_system.jenkins_data.id
subnet_id = element(var.private_subnets_ids, count.index)
security_groups = [aws_security_group.jenkins_data_allow_nfs_access.id]
}
#------------------------------------------------------------------------------
# ECS Task Definition
#------------------------------------------------------------------------------
module "td" {
source = "cn-terraform/ecs-fargate-task-definition/aws"
version = "1.0.35"
# source = "../terraform-aws-ecs-fargate-task-definition"
name_prefix = "${var.name_prefix}-jenkins"
container_name = local.container_name
container_image = var.container_image
container_cpu = var.container_cpu
container_memory = var.container_memory
port_mappings = local.td_port_mappings
healthcheck = local.healthcheck
log_configuration = {
logDriver = "awslogs"
options = {
"awslogs-region" = var.region
"awslogs-group" = module.aws_cw_logs.logs_path
"awslogs-stream-prefix" = "ecs"
}
secretOptions = null
}
volumes = [{
name = "jenkins_efs"
host_path = null
docker_volume_configuration = []
efs_volume_configuration = [{
file_system_id = aws_efs_file_system.jenkins_data.id
root_directory = "/"
transit_encryption = "DISABLED"
transit_encryption_port = null
authorization_config = []
}]
}]
mount_points = [
{
sourceVolume = "jenkins_efs"
containerPath = "/var/jenkins_home"
readOnly = false
}
]
}
#------------------------------------------------------------------------------
# ECS Service
#------------------------------------------------------------------------------
module "ecs-fargate-service" {
source = "cn-terraform/ecs-fargate-service/aws"
version = "2.0.39"
# source = "../terraform-aws-ecs-fargate-service"
name_prefix = "${var.name_prefix}-jenkins"
vpc_id = var.vpc_id
ecs_cluster_arn = module.ecs-cluster.aws_ecs_cluster_cluster_arn
health_check_grace_period_seconds = 120
task_definition_arn = module.td.aws_ecs_task_definition_td_arn
public_subnets = var.public_subnets_ids
private_subnets = var.private_subnets_ids
container_name = local.container_name
enable_autoscaling = var.enable_autoscaling
ecs_cluster_name = module.ecs-cluster.aws_ecs_cluster_cluster_name
waf_web_acl_arn = var.lb_waf_web_acl_arn
lb_http_ports = local.service_http_ports
lb_https_ports = local.service_https_ports
}