-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
228 lines (196 loc) · 5.88 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
provider "aws" {
region = var.aws_region
default_tags {
tags = {
project_name = var.project_name
service_name = var.service_name
environment = var.environment
}
}
}
# Create an IAM role for the GitLab runner
resource "aws_iam_role" "gitlab_runner" {
name = "gitlab-runner-${var.environment}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}
# Create an IAM instance profile
resource "aws_iam_instance_profile" "gitlab_runner" {
name = "gitlab-runner-${var.environment}"
role = aws_iam_role.gitlab_runner.name
}
# Security group for GitLab runner
resource "aws_security_group" "gitlab_runner" {
name = "gitlab-runner-${var.environment}"
description = "Security group for GitLab runner"
vpc_id = data.aws_vpc.default.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "gitlab-runner-${var.environment}"
Environment = var.environment
}
}
# Launch template for GitLab runner - remove public IP
resource "aws_launch_template" "gitlab_runner" {
name_prefix = "gitlab-runner-${var.environment}"
image_id = data.aws_ami.ami.id
instance_type = var.instance_type
monitoring {
enabled = true # Enable detailed monitoring
}
network_interfaces {
associate_public_ip_address = false
security_groups = [aws_security_group.gitlab_runner.id]
}
iam_instance_profile {
name = aws_iam_instance_profile.gitlab_runner.name
}
user_data = base64encode(templatefile("${path.module}/scripts/user_data.sh", {
gitlab_url = var.gitlab_url
gitlab_runner_token = var.gitlab_runner_token
runner_tags = var.runner_tags
aws_cloudwatch_log_group = aws_cloudwatch_log_group.gitlab_runner.name
aws_region = var.aws_region
vpc_id = data.aws_vpc.default.id
subnet_id = data.aws_subnets.private.ids[0]
}))
tags = {
Name = "gitlab-runner-${var.environment}"
Timestamp = timestamp()
}
}
# Auto Scaling Group
resource "aws_autoscaling_group" "gitlab_runner" {
name = "gitlab-runner-${var.environment}"
desired_capacity = var.asg_desired_capacity
max_size = var.asg_max_size
min_size = var.asg_min_size
target_group_arns = []
vpc_zone_identifier = data.aws_subnets.private.ids
launch_template {
id = aws_launch_template.gitlab_runner.id
version = "$Latest"
}
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}
dynamic "tag" {
for_each = merge(
{
"Name" = "gitlab-runner-${var.environment}"
},
{
"project_name" = var.project_name
"service_name" = var.service_name
"environment" = var.environment
}
)
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}
# Schedule for scaling down during non-business hours
resource "aws_autoscaling_schedule" "scale_down" {
scheduled_action_name = "scale-down"
min_size = 0
max_size = 0
desired_capacity = 0
recurrence = "0 13 * * MON-FRI" # 20:00 Bangkok time
autoscaling_group_name = aws_autoscaling_group.gitlab_runner.name
}
# Schedule for scaling up during business hours
resource "aws_autoscaling_schedule" "scale_up" {
scheduled_action_name = "scale-up"
min_size = var.asg_min_size
max_size = var.asg_max_size
desired_capacity = var.asg_desired_capacity
recurrence = "30 1 * * MON-FRI" # 08:30 Bangkok time
autoscaling_group_name = aws_autoscaling_group.gitlab_runner.name
}
# Create NAT Gateway
resource "aws_eip" "nat" {
domain = "vpc"
}
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = data.aws_subnets.public.ids[0]
tags = {
Name = "gitlab-runner-nat-${var.environment}"
}
}
# Create route table for private subnets
resource "aws_route_table" "private" {
vpc_id = data.aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}
tags = {
Name = "gitlab-runner-private-${var.environment}"
}
}
# Associate private subnets with the route table
resource "aws_route_table_association" "private" {
count = length(data.aws_subnets.private.ids)
subnet_id = data.aws_subnets.private.ids[count.index]
route_table_id = aws_route_table.private.id
}
# Create CloudWatch Logs policy
resource "aws_iam_role_policy" "cloudwatch_logs" {
name = "gitlab-runner-cloudwatch-logs-${var.environment}"
role = aws_iam_role.gitlab_runner.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
]
Resource = ["arn:aws:logs:*:*:*"]
}
]
})
}
# Create CloudWatch Log Group
resource "aws_cloudwatch_log_group" "gitlab_runner" {
name = "${var.cloudwatch_log_group_name}/${var.environment}"
retention_in_days = 7
tags = {
Name = "gitlab-runner-${var.environment}"
}
}
# Add Session Manager policy to the IAM role
resource "aws_iam_role_policy_attachment" "ssm" {
role = aws_iam_role.gitlab_runner.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
# Add CloudWatch monitoring policy to the IAM role
resource "aws_iam_role_policy_attachment" "cloudwatch_monitoring" {
role = aws_iam_role.gitlab_runner.name
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}