diff --git a/config/secrets b/config/secrets index c1cf69a..03c90ed 160000 --- a/config/secrets +++ b/config/secrets @@ -1 +1 @@ -Subproject commit c1cf69a9de6f6b766750395875cd5bdcb16a0e96 +Subproject commit 03c90ed5b009df392bf7a3b086f2bdb3242f7adc diff --git a/environment/monitoring/main.tf b/environment/monitoring/main.tf index 7e6ba38..778cd4d 100644 --- a/environment/monitoring/main.tf +++ b/environment/monitoring/main.tf @@ -1 +1,29 @@ -# TODO:: 모니터링용 EC2 인스턴스 및 보안 그룹 리소스 정의 필요 +# 기본 VPC 정보 조회 +data "aws_vpc" "default" { + default = true +} + +module "monitoring_stack" { + # 기존 app_stack 모듈을 재사용하거나, 모니터링 전용 모듈이 있다면 경로 수정 + source = "../../modules/monitoring_stack" + + env_name = "monitoring" + vpc_id = data.aws_vpc.default.id + + ami_id = var.ami_id + + key_name = var.key_name + + instance_type = var.monitoring_instance_type + + private_ip = var.private_ip + + # Nginx 및 도메인 설정 + domain_name = var.domain_name + cert_email = var.cert_email + nginx_conf_name = var.nginx_conf_name + + + # Grafana(3000), Prometheus(9090), Loki(3100) 포트 개방 + monitoring_ingress_rules = var.monitoring_ingress_rules +} diff --git a/environment/monitoring/variables.tf b/environment/monitoring/variables.tf index fea9485..b1d78a6 100644 --- a/environment/monitoring/variables.tf +++ b/environment/monitoring/variables.tf @@ -1 +1,51 @@ -# TODO:: 모니터링 인스턴스용 변수 정의 +variable "ami_id" { + description = "AMI ID for the monitoring environment" + type = string +} + +variable "monitoring_instance_type" { + description = "Instance type for monitoring (e.g., t3.medium or larger recommended)" + type = string +} + +variable "key_name" { + description = "SSH Key pair name" + type = string +} + +variable "monitoring_ingress_rules" { + description = "Ingress rules for Grafana(3000), Prometheus(9090), Loki(3100)" + type = list(object({ + from_port = number + to_port = number + protocol = string + cidr_blocks = list(string) + description = string + })) +} + +variable "private_ip" { + description = "Fixed private ip for alloy config" + type = string +} + +variable "ebs_volume_size" { + description = "Disk size for Prometheus TSDB (GB)" + type = number + default = 50 +} + +variable "domain_name" { + description = "Domain name for Grafana dashboard (e.g., monitor.example.com)" + type = string +} + +variable "cert_email" { + description = "email for Domain Name Certbot" + type = string +} + +variable "nginx_conf_name" { + description = "Nginx conf name for the prod environment" + type = string +} diff --git a/modules/app_stack/ec2.tf b/modules/app_stack/ec2.tf index 704e676..fd0c315 100644 --- a/modules/app_stack/ec2.tf +++ b/modules/app_stack/ec2.tf @@ -6,7 +6,7 @@ data "cloudinit_config" "app_init" { # [Part 1] Docker 설치 스크립트 part { content_type = "text/x-shellscript" - content = file("${path.module}/scripts/docker_setup.sh") + content = file("${path.module}/../common/scripts/docker_setup.sh") filename = "1_docker_install.sh" } diff --git a/modules/app_stack/scripts/docker_setup.sh b/modules/common/scripts/docker_setup.sh similarity index 100% rename from modules/app_stack/scripts/docker_setup.sh rename to modules/common/scripts/docker_setup.sh diff --git a/modules/monitoring_stack/ec2.tf b/modules/monitoring_stack/ec2.tf new file mode 100644 index 0000000..9de07dd --- /dev/null +++ b/modules/monitoring_stack/ec2.tf @@ -0,0 +1,56 @@ +data "cloudinit_config" "app_init" { + gzip = true + base64_encode = true + + # [Part 1] Docker 설치 스크립트 + part { + content_type = "text/x-shellscript" + content = file("${path.module}/../common/scripts/docker_setup.sh") + filename = "1_docker_install.sh" + } + + # [Part 2] Nginx 설정 스크립트 파일 생성 (실행 안 함, 파일만 생성) + part { + content_type = "text/cloud-config" + content = <>> [수동 실행] $DOMAIN 에 대한 Nginx 및 SSL 설정을 시작합니다." + +# 1. 패키지 설치 +sudo apt-get update +sudo apt-get install -y nginx python3 python3-venv libaugeas0 + +# 2. Certbot 설치 및 링크 +sudo python3 -m venv /opt/certbot/ +sudo /opt/certbot/bin/pip install --upgrade pip +sudo /opt/certbot/bin/pip install certbot certbot-nginx +sudo ln -sf /opt/certbot/bin/certbot /usr/bin/certbot + +# 3. SSL 인증서 발급 +sudo systemctl stop nginx +sudo certbot certonly --standalone \ + --non-interactive \ + --agree-tos \ + --email "$EMAIL" \ + -d "$DOMAIN" + +# 4. Nginx 설정 파일 작성 +sudo tee /etc/nginx/sites-available/$CONF_NAME > /dev/null </dev/null | grep -q "certbot renew"; then + (crontab -l 2>/dev/null; echo "0 0,12 * * * /usr/bin/certbot renew --quiet --post-hook 'systemctl reload nginx'") | crontab - +fi + +# 7. Nginx 재시작 +sudo nginx -t +sudo systemctl restart nginx + +echo "Nginx setup complete!" diff --git a/modules/monitoring_stack/security_groups.tf b/modules/monitoring_stack/security_groups.tf new file mode 100644 index 0000000..c3a42eb --- /dev/null +++ b/modules/monitoring_stack/security_groups.tf @@ -0,0 +1,27 @@ +resource "aws_security_group" "monitoring_sg" { + name = "sc-${var.env_name}-sg" + description = "Security group for Monitoring Stack (Grafana, Prometheus, Loki)" + vpc_id = var.vpc_id + + dynamic "ingress" { + for_each = var.monitoring_ingress_rules + content { + from_port = ingress.value.from_port + to_port = ingress.value.to_port + protocol = ingress.value.protocol + cidr_blocks = ingress.value.cidr_blocks + description = ingress.value.description + } + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "${var.env_name}-sg" + } +} diff --git a/modules/monitoring_stack/variables.tf b/modules/monitoring_stack/variables.tf new file mode 100644 index 0000000..7534c61 --- /dev/null +++ b/modules/monitoring_stack/variables.tf @@ -0,0 +1,56 @@ +variable "env_name" { + description = "환경 이름" + type = string +} + +variable "vpc_id" { + description = "배포할 VPC ID" + type = string +} + +variable "ami_id" { + description = "EC2 AMI ID" + type = string +} + +variable "key_name" { + description = "EC2 Key Pair 이름" + type = string +} + +variable "instance_type" { + description = "EC2 인스턴스 타입" + type = string +} + +variable "private_ip" { + description = "alloy 설정을 위한 private ip 고정" + type = string +} + +variable "monitoring_ingress_rules" { + description = "모니터링 도구(Grafana, Prometheus, Loki)를 위한 보안 규칙" + type = list(object({ + from_port = number + to_port = number + protocol = string + cidr_blocks = list(string) + description = string + })) +} + +# [Nginx 관련 추가 변수] +variable "domain_name" { + description = "Domain name for Nginx" + type = string +} + +variable "cert_email" { + description = "Email for Let's Encrypt" + type = string +} + +variable "nginx_conf_name" { + description = "Nginx config filename" + type = string +}