-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlocalstack-ephemeral-instances.ts
More file actions
240 lines (221 loc) · 6.63 KB
/
Copy pathlocalstack-ephemeral-instances.ts
File metadata and controls
240 lines (221 loc) · 6.63 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { z } from "zod";
import { type ToolMetadata, type InferSchema } from "xmcp";
import { requireAuthToken } from "../core/preflight";
import { ResponseBuilder } from "../core/response-builder";
import { withToolAnalytics } from "../core/analytics";
import {
PlatformApiClient,
describePlatformError,
type EphemeralInstance,
} from "../lib/localstack/platform.client";
export const schema = {
action: z
.enum(["create", "list", "logs", "delete"])
.describe("The Ephemeral Instances action to perform."),
name: z
.string()
.optional()
.describe("Instance name. Required for create, logs, and delete actions."),
lifetime: z
.number()
.int()
.positive()
.optional()
.describe(
"Lifetime in minutes for create action. Defaults to the platform default when omitted."
),
extension: z
.string()
.optional()
.describe(
"Optional extension package to preload for create action. This is passed as EXTENSION_AUTO_INSTALL."
),
cloudPod: z
.string()
.optional()
.describe(
"Optional Cloud Pod name to initialize state for create action. This is passed as CLOUD_POD_NAME."
),
envVars: z
.record(z.string(), z.string())
.optional()
.describe(
"Additional environment variables to pass to the ephemeral instance (create action only)."
),
};
export const metadata: ToolMetadata = {
name: "localstack-ephemeral-instances",
description:
"Manage cloud-hosted LocalStack Ephemeral Instances: create, list, fetch logs, and delete.",
annotations: {
title: "LocalStack Ephemeral Instances",
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
},
};
export default async function localstackEphemeralInstances({
action,
name,
lifetime,
extension,
cloudPod,
envVars,
}: InferSchema<typeof schema>) {
return withToolAnalytics(
"localstack-ephemeral-instances",
{
action,
name,
lifetime,
extension,
cloudPod,
envVarKeys: envVars ? Object.keys(envVars) : [],
},
async () => {
const authError = requireAuthToken();
if (authError) return authError;
// Ephemeral instances are cloud-hosted: everything goes through the LocalStack
// platform API — no local container, CLI, or Docker daemon involved.
const client = new PlatformApiClient(process.env.LOCALSTACK_AUTH_TOKEN!.trim());
switch (action) {
case "create":
return await handleCreate(client, { name, lifetime, extension, cloudPod, envVars });
case "list":
return await handleList(client);
case "logs":
return await handleLogs(client, { name });
case "delete":
return await handleDelete(client, { name });
default:
return ResponseBuilder.error("Unknown action", `Unsupported action: ${action}`);
}
}
);
}
function formatCreateResponse(payload: EphemeralInstance): string {
const endpoint = String(payload.endpoint_url ?? "N/A");
const id = String(payload.id ?? payload.instance_name ?? "N/A");
const status = String(payload.status ?? "unknown");
const creationTime = String(payload.creation_time ?? "N/A");
const expiryTime = String(payload.expiry_time ?? "N/A");
return `## Ephemeral Instance Created
- **ID:** ${id}
- **Status:** ${status}
- **Endpoint URL:** ${endpoint}
- **Creation Time:** ${creationTime}
- **Expiry Time:** ${expiryTime}
\`\`\`json
${JSON.stringify(payload, null, 2)}
\`\`\`
Use this endpoint with your tools, for example:
\`aws --endpoint-url=${endpoint} s3 ls\``;
}
async function handleCreate(
client: PlatformApiClient,
{
name,
lifetime,
extension,
cloudPod,
envVars,
}: {
name?: string;
lifetime?: number;
extension?: string;
cloudPod?: string;
envVars?: Record<string, string>;
}
) {
if (!name?.trim()) {
return ResponseBuilder.error(
"Missing Required Parameter",
"The `create` action requires the `name` parameter."
);
}
const mergedEnvVars: Record<string, string> = { ...(envVars || {}) };
if (extension) {
mergedEnvVars.EXTENSION_AUTO_INSTALL = extension;
}
if (cloudPod) {
mergedEnvVars.CLOUD_POD_NAME = cloudPod;
}
for (const key of Object.keys(mergedEnvVars)) {
if (!key || key.includes("=")) {
return ResponseBuilder.error(
"Invalid Environment Variable Key",
`Invalid env var key '${key}'. Keys must be non-empty and cannot contain '='.`
);
}
}
try {
const instance = await client.createEphemeralInstance({
name: name.trim(),
lifetime,
envVars: mergedEnvVars,
});
return ResponseBuilder.markdown(formatCreateResponse(instance));
} catch (error) {
return ResponseBuilder.error(
"Create Failed",
describePlatformError(error, `ephemeral instance '${name}'`)
);
}
}
async function handleList(client: PlatformApiClient) {
try {
const instances = await client.listEphemeralInstances();
if (instances.length === 0) {
return ResponseBuilder.markdown(
"## Ephemeral Instances\n\nNo ephemeral instances found in your LocalStack Cloud workspace."
);
}
return ResponseBuilder.markdown(
`## Ephemeral Instances\n\n\`\`\`json\n${JSON.stringify(instances, null, 2)}\n\`\`\``
);
} catch (error) {
return ResponseBuilder.error(
"List Failed",
describePlatformError(error, "ephemeral instances")
);
}
}
async function handleLogs(client: PlatformApiClient, { name }: { name?: string }) {
if (!name?.trim()) {
return ResponseBuilder.error(
"Missing Required Parameter",
"The `logs` action requires the `name` parameter."
);
}
try {
const logs = await client.getEphemeralInstanceLogs(name.trim());
if (!logs.trim()) {
return ResponseBuilder.markdown(`No logs available for ephemeral instance '${name}'.`);
}
return ResponseBuilder.markdown(
`## Ephemeral Instance Logs: ${name}\n\n\`\`\`\n${logs}\n\`\`\``
);
} catch (error) {
return ResponseBuilder.error(
"Logs Failed",
describePlatformError(error, `ephemeral instance '${name}'`)
);
}
}
async function handleDelete(client: PlatformApiClient, { name }: { name?: string }) {
if (!name?.trim()) {
return ResponseBuilder.error(
"Missing Required Parameter",
"The `delete` action requires the `name` parameter."
);
}
try {
await client.deleteEphemeralInstance(name.trim());
return ResponseBuilder.markdown(`Successfully deleted instance: ${name} ✅`);
} catch (error) {
return ResponseBuilder.error(
"Delete Failed",
describePlatformError(error, `ephemeral instance '${name}'`)
);
}
}