Skip to main content

Job

caution

This is the SST v1.x Constructs doc. SST v2 is now released. If you are using v2, see the v2 Constructs doc. If you are looking to upgrade to v2, check out the upgrade steps.

The Job construct is a higher level CDK construct that makes it easy to perform long running jobs.

Examples

Creating a Job

import { Job } from "@serverless-stack/resources";

new Job(stack, "MyJob", {
handler: "src/job.main",
});

Setting additional props

new Job(stack, "MyJob", {
handler: "job.main",
srcPath: "services",
timeout: "30 minutes",
memorySize: "3 GB",
config: [STRIPE_KEY, API_URL],
permissions: ["ses", bucket],
});

Create a job in a VPC

import { Job } from "@serverless-stack/resources";
import { Vpc } from "aws-cdk-lib/aws-ec2";

new Job(stack, "MyJob", {
handler: "src/job.main",
cdk: {
vpc: Vpc.fromLookup(this, "VPC", {
vpcId: "vpc-xxxxxxxxxx",
})
}
});

Constructor

new Job(scope, id, props)

Parameters

JobProps

bind?

Type : Array<SSTConstruct>

Bind resources for the job

new Job(stack, "MyJob", {
handler: "src/job.handler",
bind: [STRIPE_KEY, bucket],
})

config?

Type : Array<Secret | Parameter>

Configure environment variables for the job

// Change
new Job(stack, "MyJob", {
handler: "src/job.handler",
config: [STRIPE_KEY, API_URL]
})

// To
new Job(stack, "MyJob", {
handler: "src/job.handler",
bind: [STRIPE_KEY, API_URL]
})

The "config" prop is deprecated, and will be removed in SST v2. Pass Parameters and Secrets in through the "bind" prop. Read more about how to upgrade here — https://docs.serverless-stack.com/constructs/function

enableLiveDev?

Type : boolean

Default : true

Can be used to disable Live Lambda Development when using sst start. Useful for things like Custom Resources that need to execute during deployment.

new Job(stack, "MyJob", {
handler: "src/job.handler",
enableLiveDev: false
})

environment?

Type : Record<string, string>

Configure environment variables for the job

new Job(stack, "MyJob", {
handler: "src/job.handler",
environment: {
DEBUG: "*",
}
})

handler

Type : string

Path to the entry point and handler function. Of the format: /path/to/file.function.

new Job(stack, "MyJob", {
handler: "src/job.handler",
})

memorySize?

Type : "3 GB" | "7 GB" | "15 GB" | "145 GB"

Default : "3 GB"

The amount of memory in MB allocated.

new Job(stack, "MyJob", {
handler: "src/job.handler",
memorySize: "3 GB",
})

permissions?

Type : Permissions

Attaches the given list of permissions to the job. Configuring this property is equivalent to calling attachPermissions() after the job is created.

new Job(stack, "MyJob", {
handler: "src/job.handler",
permissions: ["ses"]
})

srcPath?

Type : string

Default : Defaults to the same directory as sst.json

Root directory of the project, typically where package.json is located. Set if using a monorepo with multiple subpackages

new Job(stack, "MyJob", {
srcPath: "services",
handler: "job.handler",
})

timeout?

Type : ${number} second | ${number} seconds | ${number} minute | ${number} minutes | ${number} hour | ${number} hours | ${number} day | ${number} days

Default : "8 hours"

The execution timeout. Minimum 5 minutes. Maximum 8 hours.

new Job(stack, "MyJob", {
handler: "src/job.handler",
timeout: "30 minutes",
})

cdk?

Type : JobCDKProps

Properties

An instance of Job has the following properties.

id

Type : string

Methods

An instance of Job has the following methods.

addConfig

caution

This function signature has been deprecated.

addConfig(config)

Attaches additional configs to job.

const STRIPE_KEY = new Config.Secret(stack, "STRIPE_KEY");

// Change
job.addConfig([STRIPE_KEY]);

// To
job.bind([STRIPE_KEY]);

The "config" prop is deprecated, and will be removed in SST v2. Pass Parameters and Secrets in through the "bind" prop. Read more about how to upgrade here — https://docs.serverless-stack.com/constructs/function

addEnvironment

addEnvironment(name, value)

Parameters

  • name string
  • value string

Attaches additional environment variable to the job.

fn.addEnvironment({
DEBUG: "*"
});

attachPermissions

attachPermissions(permissions)

Parameters

Attaches the given list of permissions to the job. This allows the job to access other AWS resources.

job.attachPermissions(["ses"]);

bind

bind(constructs)

Parameters

  • constructs Array<SSTConstruct>

Binds additional resources to job.

job.bind([STRIPE_KEY, bucket]);

JobCDKProps

id?

Type : string

Allows you to override default id for this construct.

vpc?

Type : IVpc

Runs codebuild job in the specified VPC. Note this will only work once deployed.

new Job(stack, "MyJob", {
handler: "src/job.handler",
cdk: {
vpc: Vpc.fromLookup(this, "VPC", {
vpcId: "vpc-xxxxxxxxxx",
}),
}
})