@sst-community/sst 0.0.0-placeholder.0 → 4.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -0
- package/bin/sst.mjs +33 -0
- package/dist/auth/adapter/adapter.d.ts +23 -0
- package/dist/auth/adapter/adapter.js +4 -0
- package/dist/auth/adapter/apple.d.ts +5 -0
- package/dist/auth/adapter/apple.js +22 -0
- package/dist/auth/adapter/code.d.ts +7 -0
- package/dist/auth/adapter/code.js +47 -0
- package/dist/auth/adapter/facebook.d.ts +5 -0
- package/dist/auth/adapter/facebook.js +27 -0
- package/dist/auth/adapter/github.d.ts +7 -0
- package/dist/auth/adapter/github.js +15 -0
- package/dist/auth/adapter/google.d.ts +17 -0
- package/dist/auth/adapter/google.js +22 -0
- package/dist/auth/adapter/index.d.ts +11 -0
- package/dist/auth/adapter/index.js +10 -0
- package/dist/auth/adapter/link.d.ts +5 -0
- package/dist/auth/adapter/link.js +27 -0
- package/dist/auth/adapter/microsoft.d.ts +11 -0
- package/dist/auth/adapter/microsoft.js +16 -0
- package/dist/auth/adapter/oauth.d.ts +33 -0
- package/dist/auth/adapter/oauth.js +82 -0
- package/dist/auth/adapter/oidc.d.ts +19 -0
- package/dist/auth/adapter/oidc.js +48 -0
- package/dist/auth/adapter/spotify.d.ts +12 -0
- package/dist/auth/adapter/spotify.js +22 -0
- package/dist/auth/example/bun.d.ts +2 -0
- package/dist/auth/example/bun.js +46 -0
- package/dist/auth/handler.d.ts +57 -0
- package/dist/auth/handler.js +206 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.js +10 -0
- package/dist/auth/session.d.ts +25 -0
- package/dist/auth/session.js +28 -0
- package/dist/aws/auth.d.ts +9 -0
- package/dist/aws/auth.js +12 -0
- package/dist/aws/bus.d.ts +28 -0
- package/dist/aws/bus.js +66 -0
- package/dist/aws/client.d.ts +8 -0
- package/dist/aws/client.js +54 -0
- package/dist/aws/realtime.d.ts +118 -0
- package/dist/aws/realtime.js +83 -0
- package/dist/aws/task.d.ts +228 -0
- package/dist/aws/task.js +232 -0
- package/dist/aws/workflow.d.ts +324 -0
- package/dist/aws/workflow.js +504 -0
- package/dist/event/index.d.ts +72 -0
- package/dist/event/index.js +35 -0
- package/dist/event/validator.d.ts +4 -0
- package/dist/event/validator.js +11 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -0
- package/dist/realtime/index.d.ts +25 -0
- package/dist/realtime/index.js +24 -0
- package/dist/resource/cloudflare.d.ts +5 -0
- package/dist/resource/cloudflare.js +30 -0
- package/dist/resource/index.d.ts +1 -0
- package/dist/resource/index.js +1 -0
- package/dist/resource/node.d.ts +4 -0
- package/dist/resource/node.js +38 -0
- package/dist/resource/shared.d.ts +9 -0
- package/dist/resource/shared.js +62 -0
- package/dist/resource.d.ts +1 -0
- package/dist/resource.js +1 -0
- package/dist/util/prettify.d.ts +3 -0
- package/dist/util/prettify.js +1 -0
- package/dist/vector/index.d.ts +247 -0
- package/dist/vector/index.js +107 -0
- package/package.json +73 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { IoTCustomAuthorizerHandler, PolicyDocument } from "aws-lambda";
|
|
2
|
+
/**
|
|
3
|
+
* The `realtime` client SDK is available through the following.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js title="src/authorizer.ts"
|
|
7
|
+
* import { realtime } from "sst/aws/realtime";
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare namespace realtime {
|
|
11
|
+
interface AuthResult {
|
|
12
|
+
/**
|
|
13
|
+
* The principal ID of the authorized client. This could be a user ID, username, or
|
|
14
|
+
* phone number.
|
|
15
|
+
*
|
|
16
|
+
* The value must be an alphanumeric string with at least one, and no more than 128,
|
|
17
|
+
* characters and match the regex pattern, `([a-zA-Z0-9]){1,128}`.
|
|
18
|
+
*/
|
|
19
|
+
principalId?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The maximum duration in seconds of the connection to IoT Core.
|
|
22
|
+
*
|
|
23
|
+
* :::note
|
|
24
|
+
* This is set when the connection is established and cannot be modified during subsequent
|
|
25
|
+
* policy refresh authorization handler invocations.
|
|
26
|
+
* :::
|
|
27
|
+
*
|
|
28
|
+
* The minimum value is 300 seconds, and the maximum is 86400 seconds.
|
|
29
|
+
* @default `86400`
|
|
30
|
+
*/
|
|
31
|
+
disconnectAfterInSeconds?: number;
|
|
32
|
+
/**
|
|
33
|
+
* The duration in seconds between policy refreshes. After the given duration, IoT Core
|
|
34
|
+
* will invoke the authorization handler function.
|
|
35
|
+
*
|
|
36
|
+
* The minimum value is 300 seconds, and the maximum value is 86400 seconds.
|
|
37
|
+
*/
|
|
38
|
+
refreshAfterInSeconds?: number;
|
|
39
|
+
/**
|
|
40
|
+
* The topics the client can subscribe to.
|
|
41
|
+
* @example
|
|
42
|
+
* For example, this subscribes to two specific topics.
|
|
43
|
+
* ```js
|
|
44
|
+
* {
|
|
45
|
+
* subscribe: ["chat/room1", "chat/room2"]
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* And to subscribe to all topics under a given prefix.
|
|
50
|
+
* ```js
|
|
51
|
+
* {
|
|
52
|
+
* subscribe: ["chat/*"]
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
subscribe?: string[];
|
|
57
|
+
/**
|
|
58
|
+
* The topics the client can publish to.
|
|
59
|
+
* @example
|
|
60
|
+
* For example, this publishes to two specific topics.
|
|
61
|
+
* ```js
|
|
62
|
+
* {
|
|
63
|
+
* publish: ["chat/room1", "chat/room2"]
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
* And to publish to all topics under a given prefix.
|
|
67
|
+
* ```js
|
|
68
|
+
* {
|
|
69
|
+
* publish: ["chat/*"]
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
publish?: string[];
|
|
74
|
+
/**
|
|
75
|
+
* Any additional [IoT Core policy documents](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) to attach to the client.
|
|
76
|
+
*
|
|
77
|
+
* There's a maximum of 10 policy documents. Where each document can contain a maximum of
|
|
78
|
+
* 2048 characters.
|
|
79
|
+
* @example
|
|
80
|
+
* ```js
|
|
81
|
+
* {
|
|
82
|
+
* policyDocuments: [
|
|
83
|
+
* {
|
|
84
|
+
* Version: "2012-10-17",
|
|
85
|
+
* Statement: [
|
|
86
|
+
* {
|
|
87
|
+
* Action: "iot:Publish",
|
|
88
|
+
* Effect: "Allow",
|
|
89
|
+
* Resource: "*"
|
|
90
|
+
* }
|
|
91
|
+
* ]
|
|
92
|
+
* }
|
|
93
|
+
* ]
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
policyDocuments?: PolicyDocument[];
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates an authorization handler for the `Realtime` component. It validates
|
|
101
|
+
* the token and grants permissions for the topics the client can subscribe and publish to.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```js title="src/authorizer.ts" "realtime.authorizer"
|
|
105
|
+
* export const handler = realtime.authorizer(async (token) => {
|
|
106
|
+
* // Validate the token
|
|
107
|
+
* console.log(token);
|
|
108
|
+
*
|
|
109
|
+
* // Return the topics to subscribe and publish
|
|
110
|
+
* return {
|
|
111
|
+
* subscribe: ["*"],
|
|
112
|
+
* publish: ["*"],
|
|
113
|
+
* };
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function authorizer(input: (token: string) => Promise<AuthResult>): IoTCustomAuthorizerHandler;
|
|
118
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `realtime` client SDK is available through the following.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```js title="src/authorizer.ts"
|
|
6
|
+
* import { realtime } from "sst/aws/realtime";
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export var realtime;
|
|
10
|
+
(function (realtime) {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an authorization handler for the `Realtime` component. It validates
|
|
13
|
+
* the token and grants permissions for the topics the client can subscribe and publish to.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```js title="src/authorizer.ts" "realtime.authorizer"
|
|
17
|
+
* export const handler = realtime.authorizer(async (token) => {
|
|
18
|
+
* // Validate the token
|
|
19
|
+
* console.log(token);
|
|
20
|
+
*
|
|
21
|
+
* // Return the topics to subscribe and publish
|
|
22
|
+
* return {
|
|
23
|
+
* subscribe: ["*"],
|
|
24
|
+
* publish: ["*"],
|
|
25
|
+
* };
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function authorizer(input) {
|
|
30
|
+
return async (evt, context) => {
|
|
31
|
+
const [, partition, , region, accountId] = context.invokedFunctionArn.split(":");
|
|
32
|
+
const token = Buffer.from(evt.protocolData.mqtt?.password ?? "", "base64").toString();
|
|
33
|
+
const { principalId = evt.protocolData.mqtt?.username || Date.now().toString(), disconnectAfterInSeconds = 86400, refreshAfterInSeconds = 300, subscribe, publish, policyDocuments, } = await input(token);
|
|
34
|
+
return {
|
|
35
|
+
isAuthenticated: true,
|
|
36
|
+
principalId,
|
|
37
|
+
disconnectAfterInSeconds,
|
|
38
|
+
refreshAfterInSeconds,
|
|
39
|
+
policyDocuments: [
|
|
40
|
+
{
|
|
41
|
+
Version: "2012-10-17",
|
|
42
|
+
Statement: [
|
|
43
|
+
{
|
|
44
|
+
Action: "iot:Connect",
|
|
45
|
+
Effect: "Allow",
|
|
46
|
+
Resource: "*",
|
|
47
|
+
},
|
|
48
|
+
...(subscribe
|
|
49
|
+
? [
|
|
50
|
+
{
|
|
51
|
+
Action: "iot:Receive",
|
|
52
|
+
Effect: "Allow",
|
|
53
|
+
Resource: subscribe.map((t) => `arn:${partition}:iot:${region}:${accountId}:topic/${t}`),
|
|
54
|
+
},
|
|
55
|
+
]
|
|
56
|
+
: []),
|
|
57
|
+
...(subscribe
|
|
58
|
+
? [
|
|
59
|
+
{
|
|
60
|
+
Action: "iot:Subscribe",
|
|
61
|
+
Effect: "Allow",
|
|
62
|
+
Resource: subscribe.map((t) => `arn:${partition}:iot:${region}:${accountId}:topicfilter/${t}`),
|
|
63
|
+
},
|
|
64
|
+
]
|
|
65
|
+
: []),
|
|
66
|
+
...(publish
|
|
67
|
+
? [
|
|
68
|
+
{
|
|
69
|
+
Action: "iot:Publish",
|
|
70
|
+
Effect: "Allow",
|
|
71
|
+
Resource: publish.map((t) => `arn:${partition}:iot:${region}:${accountId}:topic/${t}`),
|
|
72
|
+
},
|
|
73
|
+
]
|
|
74
|
+
: []),
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
...(policyDocuments ?? []),
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
realtime.authorizer = authorizer;
|
|
83
|
+
})(realtime || (realtime = {}));
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { type AwsOptions } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* The `task` client SDK is available through the following.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js title="src/app.ts"
|
|
7
|
+
* import { task } from "sst/aws/task";
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* If you are not using Node.js, you can use the AWS SDK instead. For example, you can call
|
|
11
|
+
* [`RunTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html) to
|
|
12
|
+
* run a task.
|
|
13
|
+
*/
|
|
14
|
+
export declare namespace task {
|
|
15
|
+
/**
|
|
16
|
+
* The link data for the task.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* For example, let's say you have a task.
|
|
20
|
+
*
|
|
21
|
+
* ```js title="sst.config.ts"
|
|
22
|
+
* new sst.aws.Task("MyTask", { cluster });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* `Resource.MyTask` will have all the link data.
|
|
26
|
+
*
|
|
27
|
+
* ```js title="src/app.ts"
|
|
28
|
+
* import { Resource } from "sst";
|
|
29
|
+
*
|
|
30
|
+
* console.log(Resource.MyTask);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export interface Resource {
|
|
34
|
+
/**
|
|
35
|
+
* The ARN of the cluster.
|
|
36
|
+
*/
|
|
37
|
+
cluster: string;
|
|
38
|
+
/**
|
|
39
|
+
* The ARN of the task definition.
|
|
40
|
+
*/
|
|
41
|
+
taskDefinition: string;
|
|
42
|
+
/**
|
|
43
|
+
* The subnets to use for the task.
|
|
44
|
+
*/
|
|
45
|
+
subnets: string[];
|
|
46
|
+
/**
|
|
47
|
+
* The security groups to use for the task.
|
|
48
|
+
*/
|
|
49
|
+
securityGroups: string[];
|
|
50
|
+
/**
|
|
51
|
+
* Whether to assign a public IP address to the task.
|
|
52
|
+
*/
|
|
53
|
+
assignPublicIp: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The names of the containers in the task.
|
|
56
|
+
*/
|
|
57
|
+
containers: string[];
|
|
58
|
+
}
|
|
59
|
+
export interface Options {
|
|
60
|
+
/**
|
|
61
|
+
* Configure the options for the [aws4fetch](https://github.com/mhart/aws4fetch)
|
|
62
|
+
* [`AWSClient`](https://github.com/mhart/aws4fetch?tab=readme-ov-file#new-awsclientoptions) used internally by the SDK.
|
|
63
|
+
*/
|
|
64
|
+
aws?: AwsOptions;
|
|
65
|
+
}
|
|
66
|
+
export interface RunOptions extends Options {
|
|
67
|
+
/**
|
|
68
|
+
* Configure the capacity provider; regular Fargate or Fargate Spot, for this task.
|
|
69
|
+
*
|
|
70
|
+
* @default `"fargate"`
|
|
71
|
+
*/
|
|
72
|
+
capacity?: "fargate" | "spot";
|
|
73
|
+
/**
|
|
74
|
+
* Overrides the memory allocated for this task in the task definition.
|
|
75
|
+
*/
|
|
76
|
+
memory?: `${number} GB`;
|
|
77
|
+
/**
|
|
78
|
+
* Overrides the CPU allocated for this task in the task definition.
|
|
79
|
+
*/
|
|
80
|
+
cpu?: `${number} vCPU`;
|
|
81
|
+
/**
|
|
82
|
+
* Overrides the ephemeral storage allocated for this task in the task definition.
|
|
83
|
+
*/
|
|
84
|
+
storage?: `${number} GB`;
|
|
85
|
+
}
|
|
86
|
+
interface Task {
|
|
87
|
+
/**
|
|
88
|
+
* The ARN of the task.
|
|
89
|
+
*/
|
|
90
|
+
arn: string;
|
|
91
|
+
/**
|
|
92
|
+
* The status of the task.
|
|
93
|
+
*/
|
|
94
|
+
status: string;
|
|
95
|
+
}
|
|
96
|
+
export interface DescribeResponse extends Task {
|
|
97
|
+
/**
|
|
98
|
+
* The raw response from the AWS ECS DescribeTasks API.
|
|
99
|
+
* @see [@aws-sdk/client-ecs.DescribeTasksResponse](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ecs/Interface/DescribeTasksResponse/)
|
|
100
|
+
*/
|
|
101
|
+
response: any;
|
|
102
|
+
}
|
|
103
|
+
export interface RunResponse extends Task {
|
|
104
|
+
/**
|
|
105
|
+
* The raw response from the AWS ECS RunTask API.
|
|
106
|
+
* @see [@aws-sdk/client-ecs.RunTaskResponse](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ecs/Interface/RunTaskResponse/)
|
|
107
|
+
*/
|
|
108
|
+
response: any;
|
|
109
|
+
}
|
|
110
|
+
export interface StopResponse extends Task {
|
|
111
|
+
/**
|
|
112
|
+
* The raw response from the AWS ECS StopTask API.
|
|
113
|
+
* @see [@aws-sdk/client-ecs.StopTaskResponse](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ecs/Interface/StopTaskResponse/)
|
|
114
|
+
*/
|
|
115
|
+
response: any;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get the details of a given task.
|
|
119
|
+
*
|
|
120
|
+
* :::note
|
|
121
|
+
* If a task had been stopped over an hour ago, it's not returned.
|
|
122
|
+
* :::
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
*
|
|
126
|
+
* For example, let's say you had previously started a task.
|
|
127
|
+
*
|
|
128
|
+
* ```js title="src/app.ts"
|
|
129
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
130
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* You can use that to get the details of the task.
|
|
134
|
+
*
|
|
135
|
+
* ```js title="src/app.ts"
|
|
136
|
+
* const describeRet = await task.describe(Resource.MyTask, taskArn);
|
|
137
|
+
* console.log(describeRet.status);
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
141
|
+
* [`DescribeTasks`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_DescribeTasks.html).
|
|
142
|
+
*/
|
|
143
|
+
export function describe(resource: Resource, task: string, options?: Options): Promise<DescribeResponse>;
|
|
144
|
+
/**
|
|
145
|
+
* Runs a task.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
*
|
|
149
|
+
* For example, let's say you have defined a task.
|
|
150
|
+
*
|
|
151
|
+
* ```js title="sst.config.ts"
|
|
152
|
+
* new sst.aws.Task("MyTask", { cluster });
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* You can then run the task in your application with the SDK.
|
|
156
|
+
*
|
|
157
|
+
* ```js title="src/app.ts" {4}
|
|
158
|
+
* import { Resource } from "sst";
|
|
159
|
+
* import { task } from "sst/aws/task";
|
|
160
|
+
*
|
|
161
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
162
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* This internally calls an AWS SDK API that returns an array of tasks. But in our case,
|
|
166
|
+
* there's only one task.
|
|
167
|
+
*
|
|
168
|
+
* The `taskArn` is the ARN of the task. You can use it to call the `describe` or `stop`
|
|
169
|
+
* functions.
|
|
170
|
+
*
|
|
171
|
+
* You can also pass in any environment variables to the task.
|
|
172
|
+
*
|
|
173
|
+
* ```js title="src/app.ts"
|
|
174
|
+
* await task.run(Resource.MyTask, {
|
|
175
|
+
* MY_ENV_VAR: "my-value"
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
180
|
+
* [`RunTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html).
|
|
181
|
+
*/
|
|
182
|
+
export function run(resource: Resource, environment?: Record<string, string>, options?: RunOptions): Promise<RunResponse>;
|
|
183
|
+
/**
|
|
184
|
+
* Stops a task.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
*
|
|
188
|
+
* For example, let's say you had previously started a task.
|
|
189
|
+
*
|
|
190
|
+
* ```js title="src/app.ts"
|
|
191
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
192
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* You can stop the task with the following.
|
|
196
|
+
*
|
|
197
|
+
* ```js title="src/app.ts"
|
|
198
|
+
* const stopRet = await task.stop(Resource.MyTask, taskArn);
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* Stopping a task is asnychronous. When you call `stop`, AWS marks a task to be stopped,
|
|
202
|
+
* but it may take a few minutes for the task to actually stop.
|
|
203
|
+
*
|
|
204
|
+
* :::note
|
|
205
|
+
* Stopping a task in asyncrhonous.
|
|
206
|
+
* :::
|
|
207
|
+
*
|
|
208
|
+
* In most cases you probably don't need to check if it has been stopped. But if necessary,
|
|
209
|
+
* you can use the `describe` function to get a task's status.
|
|
210
|
+
*
|
|
211
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
212
|
+
* [`StopTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_StopTask.html).
|
|
213
|
+
*/
|
|
214
|
+
export function stop(resource: Resource, task: string, options?: Options): Promise<StopResponse>;
|
|
215
|
+
export class DescribeError extends Error {
|
|
216
|
+
readonly response: Response;
|
|
217
|
+
constructor(response: Response);
|
|
218
|
+
}
|
|
219
|
+
export class RunError extends Error {
|
|
220
|
+
readonly response: Response;
|
|
221
|
+
constructor(response: Response);
|
|
222
|
+
}
|
|
223
|
+
export class StopError extends Error {
|
|
224
|
+
readonly response: Response;
|
|
225
|
+
constructor(response: Response);
|
|
226
|
+
}
|
|
227
|
+
export {};
|
|
228
|
+
}
|
package/dist/aws/task.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { awsFetch } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* The `task` client SDK is available through the following.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js title="src/app.ts"
|
|
7
|
+
* import { task } from "sst/aws/task";
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* If you are not using Node.js, you can use the AWS SDK instead. For example, you can call
|
|
11
|
+
* [`RunTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html) to
|
|
12
|
+
* run a task.
|
|
13
|
+
*/
|
|
14
|
+
export var task;
|
|
15
|
+
(function (task_1) {
|
|
16
|
+
/**
|
|
17
|
+
* Get the details of a given task.
|
|
18
|
+
*
|
|
19
|
+
* :::note
|
|
20
|
+
* If a task had been stopped over an hour ago, it's not returned.
|
|
21
|
+
* :::
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* For example, let's say you had previously started a task.
|
|
26
|
+
*
|
|
27
|
+
* ```js title="src/app.ts"
|
|
28
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
29
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* You can use that to get the details of the task.
|
|
33
|
+
*
|
|
34
|
+
* ```js title="src/app.ts"
|
|
35
|
+
* const describeRet = await task.describe(Resource.MyTask, taskArn);
|
|
36
|
+
* console.log(describeRet.status);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
40
|
+
* [`DescribeTasks`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_DescribeTasks.html).
|
|
41
|
+
*/
|
|
42
|
+
async function describe(resource, task, options) {
|
|
43
|
+
const res = await awsFetch("ecs", "/", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: {
|
|
46
|
+
"X-Amz-Target": "AmazonEC2ContainerServiceV20141113.DescribeTasks",
|
|
47
|
+
"Content-Type": "application/x-amz-json-1.1",
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
cluster: resource.cluster,
|
|
51
|
+
tasks: [task],
|
|
52
|
+
}),
|
|
53
|
+
}, options);
|
|
54
|
+
if (!res.ok)
|
|
55
|
+
throw new DescribeError(res);
|
|
56
|
+
const data = (await res.json());
|
|
57
|
+
if (!data.tasks?.length)
|
|
58
|
+
throw new DescribeError(res);
|
|
59
|
+
return {
|
|
60
|
+
arn: data.tasks[0].taskArn,
|
|
61
|
+
status: data.tasks[0].lastStatus,
|
|
62
|
+
response: data,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
task_1.describe = describe;
|
|
66
|
+
/**
|
|
67
|
+
* Runs a task.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
*
|
|
71
|
+
* For example, let's say you have defined a task.
|
|
72
|
+
*
|
|
73
|
+
* ```js title="sst.config.ts"
|
|
74
|
+
* new sst.aws.Task("MyTask", { cluster });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* You can then run the task in your application with the SDK.
|
|
78
|
+
*
|
|
79
|
+
* ```js title="src/app.ts" {4}
|
|
80
|
+
* import { Resource } from "sst";
|
|
81
|
+
* import { task } from "sst/aws/task";
|
|
82
|
+
*
|
|
83
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
84
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* This internally calls an AWS SDK API that returns an array of tasks. But in our case,
|
|
88
|
+
* there's only one task.
|
|
89
|
+
*
|
|
90
|
+
* The `taskArn` is the ARN of the task. You can use it to call the `describe` or `stop`
|
|
91
|
+
* functions.
|
|
92
|
+
*
|
|
93
|
+
* You can also pass in any environment variables to the task.
|
|
94
|
+
*
|
|
95
|
+
* ```js title="src/app.ts"
|
|
96
|
+
* await task.run(Resource.MyTask, {
|
|
97
|
+
* MY_ENV_VAR: "my-value"
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
102
|
+
* [`RunTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html).
|
|
103
|
+
*/
|
|
104
|
+
async function run(resource, environment, options) {
|
|
105
|
+
const res = await awsFetch("ecs", "/", {
|
|
106
|
+
method: "POST",
|
|
107
|
+
headers: {
|
|
108
|
+
"X-Amz-Target": "AmazonEC2ContainerServiceV20141113.RunTask",
|
|
109
|
+
"Content-Type": "application/x-amz-json-1.1",
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
capacityProviderStrategy: [
|
|
113
|
+
{
|
|
114
|
+
capacityProvider: options?.capacity === "spot" ? "FARGATE_SPOT" : "FARGATE",
|
|
115
|
+
weight: 1,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
cluster: resource.cluster,
|
|
119
|
+
taskDefinition: resource.taskDefinition,
|
|
120
|
+
networkConfiguration: {
|
|
121
|
+
awsvpcConfiguration: {
|
|
122
|
+
subnets: resource.subnets,
|
|
123
|
+
securityGroups: resource.securityGroups,
|
|
124
|
+
assignPublicIp: resource.assignPublicIp ? "ENABLED" : "DISABLED",
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
overrides: {
|
|
128
|
+
...(options?.cpu ? { cpu: (parseFloat(options.cpu.replace(" vCPU", "")) * 1024).toString() } : {}),
|
|
129
|
+
...(options?.memory ? { memory: (parseFloat(options.memory.replace(" GB", "")) * 1024).toString() } : {}),
|
|
130
|
+
...(options?.storage ? { ephemeralStorage: { sizeInGiB: parseInt(options.storage.replace(" GB", "")) } } : {}),
|
|
131
|
+
containerOverrides: resource.containers.map((name) => ({
|
|
132
|
+
name,
|
|
133
|
+
environment: Object.entries(environment ?? {}).map(([key, value]) => ({
|
|
134
|
+
name: key,
|
|
135
|
+
value,
|
|
136
|
+
})),
|
|
137
|
+
})),
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
}, options);
|
|
141
|
+
if (!res.ok)
|
|
142
|
+
throw new RunError(res);
|
|
143
|
+
const data = (await res.json());
|
|
144
|
+
if (!data.tasks?.length)
|
|
145
|
+
throw new RunError(res);
|
|
146
|
+
return {
|
|
147
|
+
arn: data.tasks[0].taskArn,
|
|
148
|
+
status: data.tasks[0].lastStatus,
|
|
149
|
+
response: data,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
task_1.run = run;
|
|
153
|
+
/**
|
|
154
|
+
* Stops a task.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
*
|
|
158
|
+
* For example, let's say you had previously started a task.
|
|
159
|
+
*
|
|
160
|
+
* ```js title="src/app.ts"
|
|
161
|
+
* const runRet = await task.run(Resource.MyTask);
|
|
162
|
+
* const taskArn = runRet.tasks[0].taskArn;
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* You can stop the task with the following.
|
|
166
|
+
*
|
|
167
|
+
* ```js title="src/app.ts"
|
|
168
|
+
* const stopRet = await task.stop(Resource.MyTask, taskArn);
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* Stopping a task is asnychronous. When you call `stop`, AWS marks a task to be stopped,
|
|
172
|
+
* but it may take a few minutes for the task to actually stop.
|
|
173
|
+
*
|
|
174
|
+
* :::note
|
|
175
|
+
* Stopping a task in asyncrhonous.
|
|
176
|
+
* :::
|
|
177
|
+
*
|
|
178
|
+
* In most cases you probably don't need to check if it has been stopped. But if necessary,
|
|
179
|
+
* you can use the `describe` function to get a task's status.
|
|
180
|
+
*
|
|
181
|
+
* If you are not using Node.js, you can use the AWS SDK and call
|
|
182
|
+
* [`StopTask`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_StopTask.html).
|
|
183
|
+
*/
|
|
184
|
+
async function stop(resource, task, options) {
|
|
185
|
+
const res = await awsFetch("ecs", "/", {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: {
|
|
188
|
+
"X-Amz-Target": "AmazonEC2ContainerServiceV20141113.StopTask",
|
|
189
|
+
"Content-Type": "application/x-amz-json-1.1",
|
|
190
|
+
},
|
|
191
|
+
body: JSON.stringify({
|
|
192
|
+
cluster: resource.cluster,
|
|
193
|
+
task,
|
|
194
|
+
}),
|
|
195
|
+
}, options);
|
|
196
|
+
if (!res.ok)
|
|
197
|
+
throw new StopError(res);
|
|
198
|
+
const data = (await res.json());
|
|
199
|
+
if (!data.task)
|
|
200
|
+
throw new StopError(res);
|
|
201
|
+
return {
|
|
202
|
+
arn: data.task.taskArn,
|
|
203
|
+
status: data.task.lastStatus,
|
|
204
|
+
response: data,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
task_1.stop = stop;
|
|
208
|
+
class DescribeError extends Error {
|
|
209
|
+
response;
|
|
210
|
+
constructor(response) {
|
|
211
|
+
super("Failed to describe task");
|
|
212
|
+
this.response = response;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
task_1.DescribeError = DescribeError;
|
|
216
|
+
class RunError extends Error {
|
|
217
|
+
response;
|
|
218
|
+
constructor(response) {
|
|
219
|
+
super("Failed to run task");
|
|
220
|
+
this.response = response;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
task_1.RunError = RunError;
|
|
224
|
+
class StopError extends Error {
|
|
225
|
+
response;
|
|
226
|
+
constructor(response) {
|
|
227
|
+
super("Failed to stop task");
|
|
228
|
+
this.response = response;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
task_1.StopError = StopError;
|
|
232
|
+
})(task || (task = {}));
|