Repository navigation
Expand file tree
/
Copy pathmain.tf
More file actions
123 lines (102 loc) · 2.6 KB
/
Copy pathmain.tf
File metadata and controls
123 lines (102 loc) · 2.6 KB
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
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
helm = { source = "hashicorp/helm", version = "~> 2.12" }
}
}
provider "aws" {
region = var.aws_region
}
# Working Modules
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "${var.app_name}-vpc"
cidr = "10.0.0.0/16"
azs = ["${var.aws_region}a", "${var.aws_region}b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
# PITFALL FIXES:
enable_nat_gateway = true
one_nat_gateway_per_az = true
enable_dns_hostnames = true
enable_dns_support = true
public_subnet_tags = {
"kubernetes.io/role/elb" = "1"
}
private_subnet_tags = {
"kubernetes.io/role/internal-elb" = "1"
}
}
# EKS Cluster
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.0"
cluster_name = "${var.app_name}-eks"
cluster_version = "1.29" # Stable version
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets # USE PRIVATE subnets only
cluster_endpoint_public_access = true
eks_managed_node_groups = {
default = {
min_size = 1
max_size = 3
desired_size = 2
instance_types = ["t3.medium"]
}
}
# Stable addons only
cluster_addons = {
coredns = {}
kube-proxy = {}
vpc-cni = {}
}
}
# SNS alerts
resource "aws_sns_topic" "alerts" {
name = "${var.app_name}-alerts"
}
resource "aws_sns_topic_subscription" "email" {
topic_arn = aws_sns_topic.alerts.arn
protocol = "email"
endpoint = var.alert_email
}
resource "aws_cloudwatch_metric_alarm" "node_cpu" {
alarm_name = "${var.app_name}-node-cpu-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
threshold = 80
alarm_actions = [aws_sns_topic.alerts.arn]
metric_name = "CPUUtilization"
namespace = "AWS/EKS"
period = 60
statistic = "Average"
dimensions = {
ClusterName = module.eks.cluster_id
NodegroupName = "default"
}
}
# CloudWatch config
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "monitoring-app-dashboard"
dashboard_body = jsonencode({
widgets = [
{
type = "metric"
x = 0
y = 0
width = 12
height = 6
properties = {
metrics = [
["AWS/EC2", "CPUUtilization"]
]
period = 300
stat = "Average"
region = "us-east-1" # Required!
title = "CPU Usage"
}
}
]
})
}