Skip to content

Task

Reference doc for the `sst.aws.Task` component.

The Task component is internally used by the Cluster component to deploy tasks to Amazon ECS. It uses AWS Fargate.

This component is returned by the addTask method of the Cluster component.


Constructor

new Task(name, args, opts?)

Parameters

Properties

nodes

Type Object

The underlying resources this component creates.

nodes.executionRole

Type Role

The Amazon ECS Execution Role.

nodes.taskDefinition

Type Output<TaskDefinition>

The Amazon ECS Task Definition.

nodes.taskRole

Type Role

The Amazon ECS Task Role.

taskDefinition

Type Output<string>

The ARN of the ECS Task Definition.

SDK

Use the SDK in your runtime to interact with your infrastructure.


This is accessible through the Resource object in the SDK.

  • assignPublicIp boolean

    Whether to assign a public IP address to the task.

  • cluster string

    The ARN of the cluster this task is deployed to.

  • containers string

    The names of the containers in the task.

  • securityGroups string

    The security groups for the task.

  • subnets string

    The subnets for the task.

  • taskDefinition string

    The ARN of the ECS Task Definition.

describe

describe(resource, task, options?)

Parameters

Returns Promise<any>

Gets the details of a task. Tasks stopped longer than 1 hour are not returned.

For example, let’s say you have started task.

src/app.ts
import { Resource } from "sst";
import { task } from "sst/aws/task";
const runRet = await task.run(Resource.MyTask);
const taskArn = runRet.tasks[0].taskArn;

You can get the details of the task with the following.

src/app.ts
const describeRet = await task.describe(Resource.MyTask, taskArn);

run

run(resource, environment?, options?)

Parameters

  • resource Resource

  • environment? Record<string, string>

  • options? Object

Returns Promise<any>

Runs a task.

For example, let’s say you have a task.

sst.config.ts
cluster.addTask("MyTask");

You can run it in your application with the following.

src/app.ts
import { Resource } from "sst";
import { task } from "sst/aws/task";
const runRet = await task.run(Resource.MyTask);
const taskArn = runRet.tasks[0].taskArn;

taskArn is the ARN of the task. You can pass it to the describe function to get the status of the task; or to the stop function to stop the task.

You can also pass in environment variables to the task.

src/app.ts
await task.run(Resource.MyTask, {
MY_ENV_VAR: "my-value",
});

stop

stop(resource, task, options?)

Parameters

Returns Promise<any>

Stops a task.

For example, let’s say you have started a task.

src/app.ts
import { Resource } from "sst";
import { task } from "sst/aws/task";
const runRet = await task.run(Resource.MyTask);
const taskArn = runRet.tasks[0].taskArn;

You can stop the task with the following.

src/app.ts
const stopRet = await task.stop(Resource.MyTask, taskArn);
// check if the task is stopped
console.log(stopRet.task?.lastStatus);

Options

Options.aws?

Type Object

Configure the AWS client.

Options.aws.accessKeyId?

Type string

Options.aws.allHeaders?

Type boolean

Options.aws.appendSessionToken?

Type boolean

Options.aws.cache?

Type Map<string, ArrayBuffer<>>

Options.aws.datetime?

Type string

Options.aws.region?

Type string

Options.aws.secretAccessKey?

Type string

Options.aws.service?

Type string

Options.aws.sessionToken?

Type string

Options.aws.signQuery?

Type boolean

Options.aws.singleEncode?

Type boolean

Resource

Resource.assignPublicIp

Type boolean

Whether to assign a public IP address to the task.

Resource.cluster

Type string

The ARN of the cluster.

Resource.containers

Type string[]

The names of the containers in the task.

Resource.securityGroups

Type string[]

The security groups to use for the task.

Resource.subnets

Type string[]

The subnets to use for the task.

Resource.taskDefinition

Type string

The ARN of the task definition.