forked from aws-samples/serverless-jenkins-on-aws-fargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs.tf
173 lines (144 loc) · 4.78 KB
/
ecs.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
// Jenkins Container Infra (Fargate)
resource "aws_ecs_cluster" "jenkins_controller" {
name = "${var.name_prefix}-main"
capacity_providers = ["FARGATE"]
tags = var.tags
setting {
name = "containerInsights"
value = "enabled"
}
}
resource "aws_ecs_cluster" "jenkins_agents" {
name = "${var.name_prefix}-spot"
capacity_providers = ["FARGATE_SPOT"]
tags = var.tags
setting {
name = "containerInsights"
value = "enabled"
}
}
locals {
base_secrets = {
ADMIN_PWD = "arn:aws:ssm:${var.region}:${var.account_id}:parameter/jenkins-pwd"
}
secrets = merge(local.base_secrets, var.secrets)
jenkins_controller_container_def = [
{
name = "${var.name_prefix}-controller"
image = aws_ecr_repository.jenkins_controller.repository_url
cpu = var.jenkins_controller_cpu
memory = var.jenkins_controller_memory
memoryReservation = var.jenkins_controller_memory
environment = [
{
name = "JAVA_OPTS"
value = "-Djenkins.install.runSetupWizard=false"
}
]
essential = true
mountPoints = [
{
containerPath = "/var/jenkins_home"
sourceVolume = "${var.name_prefix}-efs"
}
],
portMappings = [
{ containerPort = var.jenkins_controller_port },
{ containerPort = var.jenkins_jnlp_port }
],
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.jenkins_controller_log_group.name
awslogs-region = var.region
awslogs-stream-prefix = "controller"
}
},
secrets = [for name, valueFrom in local.secrets : {
name = name
valueFrom = valueFrom
}]
}
]
}
resource "aws_kms_key" "cloudwatch" {
description = "KMS for cloudwatch log group"
policy = data.aws_iam_policy_document.cloudwatch.json
}
resource "aws_cloudwatch_log_group" "jenkins_controller_log_group" {
name = var.name_prefix
retention_in_days = var.jenkins_controller_task_log_retention_days
kms_key_id = aws_kms_key.cloudwatch.arn
tags = var.tags
}
resource "aws_ecs_task_definition" "jenkins_controller" {
family = var.name_prefix
task_role_arn = var.jenkins_controller_task_role_arn != null ? var.jenkins_controller_task_role_arn : aws_iam_role.jenkins_controller_task_role[0].arn
execution_role_arn = var.ecs_execution_role_arn != null ? var.ecs_execution_role_arn : aws_iam_role.ecs_execution_role[0].arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.jenkins_controller_cpu
memory = var.jenkins_controller_memory
container_definitions = jsonencode(local.jenkins_controller_container_def)
volume {
name = "${var.name_prefix}-efs"
efs_volume_configuration {
file_system_id = aws_efs_file_system.this.id
transit_encryption = "ENABLED"
authorization_config {
access_point_id = aws_efs_access_point.this.id
iam = "ENABLED"
}
}
}
tags = var.tags
}
resource "aws_ecs_service" "jenkins_controller" {
name = "${var.name_prefix}-controller"
task_definition = aws_ecs_task_definition.jenkins_controller.arn
cluster = aws_ecs_cluster.jenkins_controller.id
desired_count = 1
launch_type = "FARGATE"
platform_version = "1.4.0"
// Assuming we cannot have more than one instance at a time. Ever.
deployment_maximum_percent = 100
deployment_minimum_healthy_percent = 0
service_registries {
registry_arn = aws_service_discovery_service.controller.arn
port = var.jenkins_jnlp_port
}
load_balancer {
target_group_arn = aws_lb_target_group.this.arn
container_name = "${var.name_prefix}-controller"
container_port = var.jenkins_controller_port
}
network_configuration {
subnets = var.jenkins_controller_subnet_ids
security_groups = [aws_security_group.jenkins_controller_security_group.id]
assign_public_ip = false
}
depends_on = [aws_lb_listener.https]
}
resource "aws_service_discovery_private_dns_namespace" "controller" {
name = var.name_prefix
vpc = var.vpc_id
description = "Serverless Jenkins discovery managed zone."
}
resource "aws_service_discovery_service" "controller" {
name = "controller"
dns_config {
namespace_id = aws_service_discovery_private_dns_namespace.controller.id
routing_policy = "MULTIVALUE"
dns_records {
ttl = 10
type = "A"
}
dns_records {
ttl = 10
type = "SRV"
}
}
health_check_custom_config {
failure_threshold = 5
}
}