Skip to content

Commit 63a928f

Browse files
author
Frank
committed
Task: support assign public ip
1 parent 29a3c80 commit 63a928f

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

platform/src/components/aws/task.ts

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ export interface TaskArgs extends FargateBaseArgs {
7171
* You will need to pass in `image` as a part of the `containers`.
7272
*/
7373
containers?: Input<Prettify<FargateContainerArgs>>[];
74+
/**
75+
* Assign a public IP address to the task.
76+
*
77+
* Defaults:
78+
* - If an SST VPC component is passed to the `vpc` property, tasks run in public subnets
79+
* by default and `publicIp` defaults to `true`.
80+
* - If a non-SST VPC is used, tasks run in the specified subnets and `publicIp` defaults
81+
* to `false`.
82+
*
83+
* @example
84+
* ```ts
85+
* {
86+
* publicIp: true
87+
* }
88+
* ```
89+
*/
90+
publicIp?: Input<boolean>;
7491
/**
7592
* Configure how this component works in `sst dev`.
7693
*
@@ -106,18 +123,18 @@ export interface TaskArgs extends FargateBaseArgs {
106123
* [Live](/docs/live/) and [`sst dev`](/docs/reference/cli/#dev).
107124
*/
108125
dev?:
109-
| false
110-
| {
111-
/**
112-
* The command that `sst dev` runs in dev mode.
113-
*/
114-
command?: Input<string>;
115-
/**
116-
* Change the directory from where the `command` is run.
117-
* @default Uses the `image.dockerfile` path
118-
*/
119-
directory?: Input<string>;
120-
};
126+
| false
127+
| {
128+
/**
129+
* The command that `sst dev` runs in dev mode.
130+
*/
131+
command?: Input<string>;
132+
/**
133+
* Change the directory from where the `command` is run.
134+
* @default Uses the `image.dockerfile` path
135+
*/
136+
directory?: Input<string>;
137+
};
121138
}
122139

123140
/**
@@ -258,6 +275,7 @@ export class Task extends Component implements Link.Linkable {
258275
private readonly executionRole: iam.Role;
259276
private readonly taskRole: iam.Role;
260277
private readonly _taskDefinition: Output<ecs.TaskDefinition>;
278+
private readonly _publicIp: Output<boolean>;
261279
private readonly containerNames: Output<Output<string>[]>;
262280
private readonly dev: boolean;
263281

@@ -276,6 +294,7 @@ export class Task extends Component implements Link.Linkable {
276294
const storage = normalizeStorage(args);
277295
const containers = normalizeContainers("task", args, name, architecture);
278296
const vpc = normalizeVpc();
297+
const publicIp = normalizePublicIp();
279298

280299
const taskRole = createTaskRole(
281300
name,
@@ -285,11 +304,11 @@ export class Task extends Component implements Link.Linkable {
285304
dev,
286305
dev
287306
? [
288-
{
289-
actions: ["appsync:*"],
290-
resources: ["*"],
291-
},
292-
]
307+
{
308+
actions: ["appsync:*"],
309+
resources: ["*"],
310+
},
311+
]
293312
: [],
294313
);
295314
this.dev = dev;
@@ -303,23 +322,23 @@ export class Task extends Component implements Link.Linkable {
303322
self,
304323
dev
305324
? containers.apply(async (v) => {
306-
const appsync = await Function.appsync();
307-
return [
308-
{
309-
...v[0],
310-
image: output("ghcr.io/sst/sst/bridge-task:20241224005724"),
311-
environment: {
312-
...v[0].environment,
313-
SST_TASK_ID: name,
314-
SST_REGION: process.env.SST_AWS_REGION!,
315-
SST_APPSYNC_HTTP: appsync.http,
316-
SST_APPSYNC_REALTIME: appsync.realtime,
317-
SST_APP: $app.name,
318-
SST_STAGE: $app.stage,
325+
const appsync = await Function.appsync();
326+
return [
327+
{
328+
...v[0],
329+
image: output("ghcr.io/sst/sst/bridge-task:20241224005724"),
330+
environment: {
331+
...v[0].environment,
332+
SST_TASK_ID: name,
333+
SST_REGION: process.env.SST_AWS_REGION!,
334+
SST_APPSYNC_HTTP: appsync.http,
335+
SST_APPSYNC_REALTIME: appsync.realtime,
336+
SST_APP: $app.name,
337+
SST_STAGE: $app.stage,
338+
},
319339
},
320-
},
321-
];
322-
})
340+
];
341+
})
323342
: containers,
324343
architecture,
325344
cpu,
@@ -333,6 +352,7 @@ export class Task extends Component implements Link.Linkable {
333352
this.vpc = vpc;
334353
this.executionRole = executionRole;
335354
this._taskDefinition = taskDefinition;
355+
this._publicIp = publicIp;
336356
this.containerNames = containers.apply((v) => v.map((v) => output(v.name)));
337357
this.registerOutputs({
338358
_task: all([args.dev, containers]).apply(([v, containers]) => ({
@@ -374,6 +394,12 @@ export class Task extends Component implements Link.Linkable {
374394
),
375395
};
376396
}
397+
398+
function normalizePublicIp() {
399+
return all([args.publicIp, vpc.isSstVpc]).apply(
400+
([publicIp, isSstVpc]) => publicIp ?? isSstVpc,
401+
);
402+
}
377403
}
378404

379405
/**
@@ -420,7 +446,7 @@ export class Task extends Component implements Link.Linkable {
420446
* @internal
421447
*/
422448
public get assignPublicIp() {
423-
return this.vpc.isSstVpc;
449+
return this._publicIp;
424450
}
425451

426452
/**

0 commit comments

Comments
 (0)