Skip to main content

Job

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

config?

Type : Array<Secret | Parameter>

Configure environment variables for the job

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

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", bucket]
})

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

Methods

An instance of Job has the following methods.

addConfig

addConfig(config)

Parameters

Attaches additional configs to job

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

job.addConfig([STRIPE_KEY]);

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", bucket]);

JobCDKProps

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",
}),
}
})