@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 235 lines 8.38 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Kubernetes from "@alchemy.run/kubernetes";
3 import * as Output from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import { FetchHyperPodChart } from "./hyperpod-chart.ts";
6 import { UploadLifecycleScript } from "./lifecycle.ts";
7
8 /**
9 * Shared infrastructure for the EKS-orchestrated HyperPod stack:
10 * network → EKS control plane → HyperPod cluster → task governance.
11 *
12 * Resources are memoized by logical id, so this Effect can be yielded from
13 * the stack program and from workload modules and always converges on the
14 * same instances.
15 */
16 export const HyperPodEksInfra = Effect.gen(function* () {
17 // HyperPod nodes must live in private subnets; NAT gives them a path to
18 // pull container images.
19 const network = yield* AWS.EC2.Network("HyperPodNetwork", {
20 cidrBlock: "10.72.0.0/16",
21 availabilityZones: 2,
22 nat: "single",
23 });
24
25 // The control-plane role for the orchestrating EKS cluster.
26 const eksRole = yield* AWS.IAM.Role("OrchestratorRole", {
27 assumeRolePolicyDocument: {
28 Version: "2012-10-17",
29 Statement: [
30 {
31 Effect: "Allow",
32 Principal: { Service: "eks.amazonaws.com" },
33 Action: ["sts:AssumeRole"],
34 },
35 ],
36 },
37 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"],
38 });
39
40 // The orchestrator: a plain EKS control plane. HyperPod requires the
41 // `API` (or `API_AND_CONFIG_MAP`) authentication mode — EKS's own
42 // CONFIG_MAP default is rejected by CreateCluster. HyperPod provides the
43 // nodes, so there is no compute config here.
44 const eks = yield* AWS.EKS.Cluster("Orchestrator", {
45 roleArn: eksRole.roleArn,
46 // HyperPod trails the newest EKS Kubernetes version — pin one it
47 // supports (1.28-1.35 today) rather than taking the EKS default.
48 version: "1.34",
49 resourcesVpcConfig: { subnetIds: network.privateSubnetIds },
50 accessConfig: {
51 authenticationMode: "API",
52 bootstrapClusterCreatorAdminPermissions: true,
53 },
54 });
55
56 // Lifecycle scripts are REQUIRED for EKS-orchestrated instance groups
57 // (and Slurm continuous provisioning): every node runs its `OnCreate`
58 // script from S3 when it boots.
59 const lifecycleBucket = yield* AWS.S3.Bucket("EksLifecycleScripts", {
60 forceDestroy: true,
61 });
62 const script = yield* UploadLifecycleScript({
63 bucketName: lifecycleBucket.bucketName,
64 });
65
66 // The HyperPod instance role: the AWS-managed cluster-instance policy
67 // plus the EKS-orchestration networking/ECR/pod-identity permissions
68 // from the HyperPod IAM guide.
69 const role = yield* AWS.IAM.Role("HyperPodEksInstanceRole", {
70 assumeRolePolicyDocument: {
71 Version: "2012-10-17",
72 Statement: [
73 {
74 Effect: "Allow",
75 Principal: { Service: "sagemaker.amazonaws.com" },
76 Action: ["sts:AssumeRole"],
77 },
78 ],
79 },
80 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonSageMakerClusterInstanceRolePolicy"],
81 inlinePolicies: {
82 "hyperpod-eks-nodes": {
83 Version: "2012-10-17",
84 Statement: [
85 {
86 Effect: "Allow",
87 Action: [
88 "ec2:AssignPrivateIpAddresses",
89 "ec2:AttachNetworkInterface",
90 "ec2:CreateNetworkInterface",
91 "ec2:CreateNetworkInterfacePermission",
92 "ec2:DeleteNetworkInterface",
93 "ec2:DeleteNetworkInterfacePermission",
94 "ec2:DescribeInstances",
95 "ec2:DescribeInstanceTypes",
96 "ec2:DescribeNetworkInterfaces",
97 "ec2:DescribeTags",
98 "ec2:DescribeVpcs",
99 "ec2:DescribeDhcpOptions",
100 "ec2:DescribeSubnets",
101 "ec2:DescribeSecurityGroups",
102 "ec2:DetachNetworkInterface",
103 "ec2:ModifyNetworkInterfaceAttribute",
104 "ec2:UnassignPrivateIpAddresses",
105 "ecr:BatchCheckLayerAvailability",
106 "ecr:BatchGetImage",
107 "ecr:GetAuthorizationToken",
108 "ecr:GetDownloadUrlForLayer",
109 "eks-auth:AssumeRoleForPodIdentity",
110 ],
111 Resource: "*",
112 },
113 {
114 Effect: "Allow",
115 Action: ["ec2:CreateTags"],
116 Resource: ["arn:aws:ec2:*:*:network-interface/*"],
117 },
118 {
119 Effect: "Allow",
120 Action: ["s3:GetObject", "s3:ListBucket"],
121 Resource: [
122 lifecycleBucket.bucketArn,
123 Output.interpolate`${lifecycleBucket.bucketArn}/*`,
124 ],
125 },
126 ],
127 },
128 },
129 });
130
131 // The HyperPod dependencies Helm chart (health-monitoring agent,
132 // training operators, device plugins, RBAC) is MANDATORY — SageMaker
133 // validates it and fails the cluster with "missing one or more required
134 // dependencies" otherwise. Rendered locally and applied to the EKS
135 // cluster before the HyperPod cluster attaches.
136 const chart = yield* FetchHyperPodChart({
137 repo: "https://github.com/aws/sagemaker-hyperpod-cli.git",
138 });
139 const dependencies = yield* Kubernetes.HelmChart("HyperPodDependencies", {
140 cluster: eks,
141 chart: chart.chartPath,
142 releaseName: "dependencies",
143 namespace: "kube-system",
144 values: {
145 // The vendored legacy mpi-operator CRD fails Kubernetes >=1.34
146 // strict server-side-apply validation, and MPI jobs aren't part of
147 // this example.
148 "mpi-operator": { enabled: false },
149 },
150 });
151
152 // The HyperPod cluster, attached to the EKS control plane. SageMaker
153 // creates the EKS access entry for the node role automatically.
154 // `Output.all` holds the attach until the dependencies chart is applied.
155 // Instance-group keys carry through to the cluster's attributes, so
156 // workloads reference `hyperpod.instanceGroups.workers` — typed per key.
157 const hyperpod = yield* AWS.SageMaker.Cluster("HyperPod", {
158 orchestrator: {
159 Eks: {
160 ClusterArn: Output.map(
161 Output.all(eks.clusterArn, dependencies.releaseName),
162 ([clusterArn]) => clusterArn,
163 ),
164 },
165 },
166 vpcConfig: {
167 // The EKS-managed cluster security group already allows node ↔
168 // control-plane and intra-cluster traffic.
169 SecurityGroupIds: [Output.map(eks.resourcesVpcConfig, (vpc) => vpc.clusterSecurityGroupId!)],
170 Subnets: network.privateSubnetIds,
171 },
172 instanceGroups: {
173 workers: {
174 InstanceType: "ml.t3.medium",
175 InstanceCount: 1,
176 ExecutionRole: role.roleArn,
177 LifeCycleConfig: {
178 SourceS3Uri: script.sourceS3Uri,
179 OnCreate: script.onCreate,
180 },
181 },
182 },
183 // Node auto-replacement is not supported for CPU instances.
184 nodeRecovery: "None",
185 tags: { app: "aws-hyperpod-example" },
186 });
187
188 // Task governance (Kueue + the HyperPod scheduler) ships as an EKS
189 // add-on. Deriving the cluster name from the HyperPod cluster's
190 // orchestrator ARN (instead of using eks.clusterName directly) makes the
191 // add-on deploy AFTER the HyperPod nodes join — its controllers need a
192 // schedulable node.
193 const governance = yield* AWS.EKS.Addon("TaskGovernance", {
194 clusterName: Output.map(hyperpod.orchestratorEksClusterArn, (arn) => arn!.split("/").pop()!),
195 addonName: "amazon-sagemaker-hyperpod-taskgovernance",
196 });
197
198 // The cluster policy: priority classes + fair-share arbitration.
199 // `Output.all` makes the policy wait for the governance add-on (which
200 // hosts the controllers that reconcile it) while still reading the
201 // HyperPod cluster's ARN.
202 const scheduler = yield* AWS.SageMaker.ClusterSchedulerConfig("Scheduler", {
203 clusterArn: Output.map(
204 Output.all(hyperpod.clusterArn, governance.addonArn),
205 ([clusterArn]) => clusterArn,
206 ),
207 schedulerConfig: {
208 PriorityClasses: [
209 { Name: "inference", Weight: 100 },
210 { Name: "training", Weight: 75 },
211 ],
212 FairShare: "Enabled",
213 },
214 });
215
216 // The research team's quota. Creating it materializes the
217 // `hyperpod-ns-research` namespace and its Kueue LocalQueue.
218 const researchQuota = yield* AWS.SageMaker.ComputeQuota("ResearchQuota", {
219 clusterArn: scheduler.clusterArn,
220 computeQuotaTarget: { TeamName: "research", FairShareWeight: 10 },
221 computeQuotaConfig: {
222 ComputeQuotaResources: [{ InstanceType: "ml.t3.medium", Count: 1 }],
223 },
224 });
225
226 return {
227 network,
228 eks,
229 role,
230 hyperpod,
231 governance,
232 scheduler,
233 researchQuota,
234 };
235 });