1import type * as ECS from "@distilled.cloud/aws/ecs";
2import type * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4import type { Cluster } from "./Cluster.ts";
5import type { Task } from "./Task.ts";
6
7export interface RunTaskRequest extends Omit<
8 ECS.RunTaskRequest,
9 "cluster" | "taskDefinition"
10> {}
11
12/**
13 * Runtime binding for `ecs:RunTask`.
14 *
15 * Bind this operation to a `Cluster` and `Task` inside a function runtime to
16 * get a callable that starts a Fargate task from the bound task
17 * definition. The cluster and task definition ARNs are injected automatically;
18 * the host is granted `ecs:RunTask` on the task definition plus `iam:PassRole`
19 * on the task and execution roles.
20 * ### Running Tasks
21 * **Example:** Launch a Fargate Task from a handler
22 * ```typescript
23 * const api = yield* AWS.Lambda.Function(
24 * "Api",
25 * { main: import.meta.url, functionUrl: true },
26 * Effect.gen(function* () {
27 * // init: bind the launch (IAM grants happen here)
28 * const runTask = yield* AWS.ECS.RunTask(cluster, task);
29 *
30 * return {
31 * fetch: Effect.gen(function* () {
32 * // runtime: launch a task per request
33 * const response = yield* runTask({
34 * launchType: "FARGATE",
35 * networkConfiguration: {
36 * awsvpcConfiguration: {
37 * subnets: [subnetId],
38 * assignPublicIp: "ENABLED",
39 * },
40 * },
41 * });
42 * return yield* HttpServerResponse.json({
43 * taskArn: response.tasks?.[0]?.taskArn,
44 * });
45 * }),
46 * };
47 * }),
48 * );
49 * ```
50 *
51 * @binding
52 */
53export interface RunTask extends Binding.Service<
54 RunTask,
55 "AWS.ECS.RunTask",
56 (
57 cluster: Cluster,
58 task: Task,
59 ) => Effect.Effect<
60 (
61 request: RunTaskRequest,
62 ) => Effect.Effect<ECS.RunTaskResponse, ECS.RunTaskError>
63 >
64> {}
65export const RunTask = Binding.Service<RunTask>("AWS.ECS.RunTask");