ts2asl: A TypeScript to Amazon States Language (ASL) transpiler
🚫 NOTICE: This open source project is not actively maintained 🚫
To all users and contributors,
As of Oct 26 2023, this project is no longer actively maintained. While the repository remains available for historical purposes and for any community members who wish to fork and continue the work, there will be no official updates, bug fixes, or merges of pull requests from the original maintainers.
ts2asl
allows developers to define AWS Step Functions using the TypeScript programming language. It allows developers to benefit from a familiar syntax, type safety, and mature ecosystem of tools for linting, editing, and automated testing.
The following code example can be executed and tested locally. The output is ASL, which can be deployed to your AWS account to create a Standard or Express Step Function.
You can also explore the TypeScript code and the corresponding ASL it generates in the interactive playground.
import * as asl from "@ts2asl/asl-lib"
//main will be converted to ASL and deployed as a state machine
export const main = asl.deploy.asStateMachine(async (input: IInput) => {
if (typeof input.name !== "string") {
input.name = "World";
}
const rnd = await random();
return {
greeting: `Hello ${input.name}`,
luckyNumber: rnd
}
});
//random will be deployed as a lambda function
export const random = asl.deploy.asLambda(async (input: { min?: number; max?: number } = {}) => {
const min = input.min ?? 0;
const max = input.max ?? 100;
return Math.round(Math.random() * (max - min) + min);
});
interface IInput {
name: string;
}
ts2asl
converts native TypeScript code to ASL. The following TypeScript langauge features are supported:
- variable assignments
- throwing errors
- input validation
- enclosed variables
- enum literals
- in keyword
- conditional expression
- optional property chaining
- null coalescing
- if statements
- for ... of statements
- do while statements
- switch statements
- try/catch/finally statements
- boolean evaluation logic
- promise.all
- array.map
- array.filter
- console.log
- evaluation of literal expressions
- string templates
- state machines invoking state machines
ts2asl
is integrated with the @ts2asl/asl-lib
module. This module can be used to integrate ASL features such as states and JsonPath with native TypeScript.
- ASL States (Pass, Wait, Choice, Parallel, Map, etc.)
- tasks with sync invocations, waitForTaskToken
- typesafe SDK Integrations
- intrinsic functions
- type conversions
- jsonPath expressions
- deploytime configuration & integration with CDK
asl-lib
implements the same behavior as AWS Step Functions, so you can execute your TypeScript code locally (using ts-node
) or write unit tests (using ts-jest
) without having to deploy anything to AWS.
There are some differences between TypeScript and ASL that a compiler won't be able to solve (fully):
- object references in TypeScript are passed by reference; object references in ASL references are passed by value.
Deployment using the AWS Cloud Development Kit (CDK)
ts2asl
features a CDK Construct that allows developers to integrate the TypeScript -> ASL conversion process into existing CI/CD pipelines. An example stack can be found in this repository.
import * as ts2asl from '@ts2asl/cdk-typescript-statemachine';
new ts2asl.TypescriptStateMachine(this, "TypescriptStateMachine", {
programName: "hello-world",
defaultFunctionProps: {},
defaultStepFunctionProps: {
stateMachineType: "EXPRESS",
roleArn: executionRole.roleArn
},
sourceFile: "./src/program.ts",
});
ts2asl
also features a CLI that can be used to transpile TypeScript code to ASL.
use the following example to get started:
echo "import * as asl from '@ts2asl/asl-lib'
export const main = asl.deploy.asStateMachine(async (input: unknown) => {
console.log(input);
return 'hello world'
});" > test.ts
npx ts2asl compile test.ts
- example project containing a simple program, CDK for deployment and Jest for testing
- waiting for completion of SDK state: organizations.createAccount.
- pagination over list: iam.listUsers.