Skip to content

Commit ce4f3db

Browse files
committed
Merge branch 'dev'
2 parents dd8ebf6 + 8d8b802 commit ce4f3db

7 files changed

Lines changed: 429 additions & 1 deletion

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ vagrant/*.pem
2323

2424
# ignore Vagrant state
2525
vagrant/.vagrant
26+
27+
# ignore Terraform files
28+
.terraform/
29+
*.tfstate
30+
*.tfstate.backup
31+
*.tfvars
32+
*.pem
33+
*.key

k8s/promtail.yaml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: promtail-serviceaccount
5+
namespace: observability
6+
---
7+
apiVersion: rbac.authorization.k8s.io/v1
8+
kind: ClusterRole
9+
metadata:
10+
name: promtail-clusterrole
11+
rules:
12+
- apiGroups: [""]
13+
resources:
14+
- nodes
15+
- services
16+
- pods
17+
verbs:
18+
- get
19+
- watch
20+
- list
21+
---
22+
apiVersion: rbac.authorization.k8s.io/v1
23+
kind: ClusterRoleBinding
24+
metadata:
25+
name: promtail-clusterrolebinding
26+
subjects:
27+
- kind: ServiceAccount
28+
name: promtail-serviceaccount
29+
namespace: observability
30+
roleRef:
31+
kind: ClusterRole
32+
name: promtail-clusterrole
33+
apiGroup: rbac.authorization.k8s.io
34+
---
35+
apiVersion: v1
36+
kind: ConfigMap
37+
metadata:
38+
name: promtail-config
39+
namespace: observability
40+
data:
41+
promtail.yaml: |
42+
server:
43+
http_listen_port: 9080
44+
grpc_listen_port: 0
45+
46+
positions:
47+
filename: /tmp/positions.yaml
48+
49+
clients:
50+
- url: http://loki.observability.svc.cluster.local:3100/loki/api/v1/push
51+
52+
scrape_configs:
53+
# VERY SPECIFIC - Only Flask API logs
54+
- job_name: flask-api-only
55+
static_configs:
56+
- targets:
57+
- localhost
58+
labels:
59+
job: flask-api
60+
__path__: /var/log/pods/*student-api*flask-api*/*.log
61+
---
62+
apiVersion: apps/v1
63+
kind: DaemonSet
64+
metadata:
65+
name: promtail
66+
namespace: observability
67+
labels:
68+
app: promtail
69+
spec:
70+
selector:
71+
matchLabels:
72+
app: promtail
73+
template:
74+
metadata:
75+
labels:
76+
app: promtail
77+
spec:
78+
serviceAccountName: promtail-serviceaccount
79+
hostNetwork: true
80+
containers:
81+
- name: promtail
82+
image: grafana/promtail:2.8.0
83+
args:
84+
- -config.file=/etc/promtail/promtail.yaml
85+
- -log.level=info
86+
volumeMounts:
87+
- name: config
88+
mountPath: /etc/promtail
89+
- name: positions
90+
mountPath: /tmp
91+
- name: logs
92+
mountPath: /var/log
93+
resources:
94+
requests:
95+
memory: "64Mi"
96+
cpu: "50m"
97+
limits:
98+
memory: "128Mi"
99+
cpu: "100m"
100+
securityContext:
101+
runAsUser: 0
102+
runAsGroup: 0
103+
volumes:
104+
- name: config
105+
configMap:
106+
name: promtail-config
107+
- name: positions
108+
emptyDir: {}
109+
- name: logs
110+
hostPath:
111+
path: /var/log
112+
tolerations:
113+
- operator: Exists

terraform/.terraform.lock.hcl

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/main.tf

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# This block configures Terraform itself - it's like "settings" for how Terraform should run our code.
2+
# This prevents surprises and makes our infrastructure more reliable!
3+
4+
terraform {
5+
required_version = ">=1.0"
6+
required_providers {
7+
aws = {
8+
source = "hashicorp/aws"
9+
version = "~>5.0"
10+
}
11+
}
12+
}
13+
14+
# AWS provider
15+
16+
provider "aws" {
17+
region = var.aws_region
18+
}
19+
20+
# VPC
21+
22+
resource "aws_vpc" "main" {
23+
cidr_block = var.vpc_cidr
24+
enable_dns_hostnames = true
25+
enable_dns_support = true
26+
27+
tags = {
28+
Name = "${var.project_name}-vpc"
29+
}
30+
}
31+
32+
# Internet Gateway
33+
34+
resource "aws_internet_gateway" "main" {
35+
vpc_id = aws_vpc.main.id
36+
37+
tags = {
38+
Name = "${var.project_name}-igw"
39+
}
40+
}
41+
42+
# Public Subnet
43+
resource "aws_subnet" "public" {
44+
vpc_id = aws_vpc.main.id
45+
cidr_block = var.public_subnet_cidr
46+
availability_zone = var.availability_zone
47+
map_public_ip_on_launch = true
48+
49+
tags = {
50+
Name = "${var.project_name}-public-subnet"
51+
}
52+
}
53+
54+
# Route Table for Public Subnet
55+
resource "aws_route_table" "public" {
56+
vpc_id = aws_vpc.main.id
57+
58+
route {
59+
cidr_block = "0.0.0.0/0"
60+
gateway_id = aws_internet_gateway.main.id
61+
}
62+
63+
tags = {
64+
Name = "${var.project_name}-public-rt"
65+
}
66+
}
67+
68+
# Route Table Association
69+
resource "aws_route_table_association" "public" {
70+
subnet_id = aws_subnet.public.id
71+
route_table_id = aws_route_table.public.id
72+
}
73+
74+
# Security Group
75+
resource "aws_security_group" "api_server" {
76+
name = "${var.project_name}-sg"
77+
description = "Security group for API server"
78+
vpc_id = aws_vpc.main.id
79+
80+
ingress {
81+
description = "SSH"
82+
from_port = 22
83+
to_port = 22
84+
protocol = "tcp"
85+
cidr_blocks = ["0.0.0.0/0"]
86+
}
87+
88+
ingress {
89+
description = "HTTP"
90+
from_port = 80
91+
to_port = 80
92+
protocol = "tcp"
93+
cidr_blocks = ["0.0.0.0/0"]
94+
}
95+
96+
ingress {
97+
description = "HTTPS"
98+
from_port = 443
99+
to_port = 443
100+
protocol = "tcp"
101+
cidr_blocks = ["0.0.0.0/0"]
102+
}
103+
104+
ingress {
105+
description = "Kubernetes API"
106+
from_port = 6443
107+
to_port = 6443
108+
protocol = "tcp"
109+
cidr_blocks = ["0.0.0.0/0"]
110+
}
111+
112+
egress {
113+
from_port = 0
114+
to_port = 0
115+
protocol = "-1"
116+
cidr_blocks = ["0.0.0.0/0"]
117+
}
118+
119+
tags = {
120+
Name = "${var.project_name}-sg"
121+
}
122+
}
123+
124+
# IAM Role for EC2
125+
126+
resource "aws_iam_role" "ec2_role" {
127+
name = "${var.project_name}-ec2-role"
128+
129+
assume_role_policy = jsonencode({
130+
Version = "2012-10-17"
131+
Statement = [
132+
{
133+
Action = "sts:AssumeRole"
134+
Effect = "Allow"
135+
Principal = {
136+
Service = "ec2.amazonaws.com"
137+
}
138+
}
139+
]
140+
141+
})
142+
143+
tags = {
144+
Name = "${var.project_name}-ec2-role"
145+
}
146+
}
147+
148+
# IAM Role Policy Attachment
149+
150+
resource "aws_iam_role_policy_attachment" "ec2_ssm" {
151+
role = aws_iam_role.ec2_role.name
152+
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
153+
}
154+
155+
resource "aws_iam_role_policy_attachment" "ec2_s3" {
156+
role = aws_iam_role.ec2_role.name
157+
policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
158+
}
159+
160+
resource "aws_iam_role_policy_attachment" "ec2_ecr" {
161+
role = aws_iam_role.ec2_role.name
162+
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
163+
}
164+
165+
# IAM Instance Profile
166+
167+
resource "aws_iam_instance_profile" "ec2_profile" {
168+
name = "${var.project_name}-ec2-profile"
169+
role = aws_iam_role.ec2_role.name
170+
}
171+
172+
# EC2 Instance
173+
174+
resource "aws_instance" "api_server" {
175+
ami = var.ami_id
176+
instance_type = var.instance_type
177+
key_name = var.key_name
178+
subnet_id = aws_subnet.public.id
179+
vpc_security_group_ids = [aws_security_group.api_server.id]
180+
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
181+
182+
root_block_device {
183+
volume_type = "gp3"
184+
volume_size = 50
185+
encrypted = true
186+
}
187+
188+
tags = {
189+
Name = "${var.project_name}-api_server"
190+
}
191+
192+
lifecycle {
193+
ignore_changes = [ami]
194+
}
195+
}
196+
197+
# Elastic IP
198+
199+
resource "aws_eip" "api_server" {
200+
instance = aws_instance.api_server.id
201+
domain = "vpc"
202+
203+
tags = {
204+
Name = "${var.project_name}-eip"
205+
}
206+
}

terraform/outputs.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
output "instance_public_ip" {
2+
description = "Public IP address of the EC2 instance"
3+
value = aws_eip.api_server.public_ip
4+
}
5+
6+
output "instance_id" {
7+
description = "ID of the EC2 instance"
8+
value = aws_instance.api_server.id
9+
}
10+
11+
output "ssh_connection_command" {
12+
description = "SSH connection command"
13+
value = "ssh -i ${var.key_name}.pem ubuntu@${aws_eip.api_server.public_ip}"
14+
}
15+
16+
output "vpc_id" {
17+
description = "VPC ID"
18+
value = aws_vpc.main.id
19+
}
20+
21+
# Remove the problematic next_steps output or fix it:
22+
output "next_steps" {
23+
description = "Next steps after deployment"
24+
value = <<EOT
25+
Next steps:
26+
1. SSH to the instance: ssh -i ${var.key_name}.pem ubuntu@${aws_eip.api_server.public_ip}
27+
2. The instance is ready for Ansible configuration
28+
EOT
29+
}

0 commit comments

Comments
 (0)