-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
170 lines (140 loc) · 4.23 KB
/
Copy pathmain.tf
File metadata and controls
170 lines (140 loc) · 4.23 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
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
# Get availability zones
data "aws_availability_zones" "available" {
state = "available"
}
# Get EKS cluster authentication
data "aws_eks_cluster_auth" "this" {
name = module.eks.cluster_name
}
# Get current AWS account ID
data "aws_caller_identity" "current" {}
# Create VPC
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "${var.cluster_name}-vpc"
cidr = var.vpc_cidr
azs = slice(data.aws_availability_zones.available.names, 0, 2)
private_subnets = var.private_subnets
public_subnets = var.public_subnets
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
public_subnet_tags = {
"kubernetes.io/role/elb" = 1
}
private_subnet_tags = {
"kubernetes.io/role/internal-elb" = 1
"karpenter.sh/discovery" = var.cluster_name
}
}
# Create EKS cluster
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 19.0"
cluster_name = var.cluster_name
cluster_version = var.cluster_version
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
cluster_endpoint_public_access = true
eks_managed_node_groups = {
cpu = {
name = "cpu-node"
min_size = 1
max_size = 5
desired_size = 2
instance_types = ["m5.xlarge"]
capacity_type = "ON_DEMAND"
}
}
manage_aws_auth_configmap = true
}
# Create GPU node group
resource "aws_eks_node_group" "gpu_nodes" {
cluster_name = module.eks.cluster_name
node_group_name = "gpu-nodes"
node_role_arn = module.eks.eks_managed_node_groups["cpu"].iam_role_arn
subnet_ids = module.vpc.private_subnets
ami_type = "AL2_x86_64_GPU"
instance_types = [var.gpu_instance_type]
capacity_type = "SPOT"
scaling_config {
desired_size = var.gpu_node_desired_size
max_size = var.gpu_node_max_size
min_size = var.gpu_node_min_size
}
depends_on = [module.eks]
}
# Create FSx for Lustre filesystem
resource "aws_fsx_lustre_file_system" "llm_storage" {
storage_capacity = var.fsx_storage_size
deployment_type = "PERSISTENT_1"
per_unit_storage_throughput = 200
subnet_ids = [module.vpc.private_subnets[0]]
security_group_ids = [module.eks.cluster_primary_security_group_id]
tags = {
Name = "llm-training-storage"
}
}
# Create S3 bucket for training data
resource "aws_s3_bucket" "llm_data" {
bucket = "${var.cluster_name}-llm-data-${data.aws_caller_identity.current.account_id}"
tags = {
Name = "LLM Training Data"
}
}
# Create IAM role for training jobs
resource "aws_iam_role" "training_job_role" {
name = "${var.cluster_name}-training-job-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRoleWithWebIdentity"
Effect = "Allow"
Principal = {
Federated = module.eks.oidc_provider_arn
}
}
]
})
}
# Attach policies to IAM role
resource "aws_iam_role_policy_attachment" "s3_access" {
role = aws_iam_role.training_job_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
}
resource "aws_iam_role_policy_attachment" "fsx_access" {
role = aws_iam_role.training_job_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonFSxReadOnlyAccess"
}
# Install NVIDIA device plugin
resource "helm_release" "nvidia_device_plugin" {
name = "nvidia-device-plugin"
repository = "https://nvidia.github.io/k8s-device-plugin"
chart = "nvidia-device-plugin"
namespace = "kube-system"
}
# Install FSx CSI Driver
resource "helm_release" "aws_fsx_csi_driver" {
name = "aws-fsx-csi-driver"
repository = "https://kubernetes-sigs.github.io/aws-fsx-csi-driver"
chart = "aws-fsx-csi-driver"
namespace = "kube-system"
}
# Create Kubernetes namespace
resource "kubernetes_namespace" "llm_training" {
metadata {
name = "llm-training"
}
}
# Create Kubernetes service account
resource "kubernetes_service_account" "training_job_sa" {
metadata {
name = "training-job-sa"
namespace = kubernetes_namespace.llm_training.metadata[0].name
annotations = {
"eks.amazonaws.com/role-arn" = aws_iam_role.training_job_role.arn
}
}
}