Skip to content

extractAslFromTemplate

function extractAslFromTemplate(params): ExtractAslResult;

Defined in: src/cfn/extract.ts:437

Recovers a renderable ASL definition from a CloudFormation/SAM/CDK template.

Locates the AWS::StepFunctions::StateMachine or SAM AWS::Serverless::StateMachine resource (disambiguated with resourceId when the template has more than one), flattens the intrinsics in its DefinitionString/Definition, applies DefinitionSubstitutions to every string in it (whichever form it takes), and parses the result as ASL. SAM resources share the CloudFormation pipeline.

resourceId is a logical ID, or for a CDK template the construct path of the state machine: the aws:cdk:path that cdk synth writes (with or without its trailing /Resource), the path relative to the stack (OrderWorkflow for OrderWorkflow0015CDD9, worked out from CDK’s logical ID scheme, so it works without that metadata too), or a trailing part of the path. An exact logical ID wins.

A SAM state machine whose definition is a local DefinitionUri file is read through readFile when one is given: its DefinitionSubstitutions are applied to the file’s text, which is then parsed as JSON or YAML. Errors about that file never quote it.

A CDK state machine built with DefinitionBody.fromFile has only a DefinitionS3Location; with readFile and a templateFileName such as OrdersStack.template.json, its staged asset is found through OrdersStack.assets.json beside the template and read the same way. For a nested stack template (ParentChild8B49BB45.nested.template.json), which has no asset manifest of its own, the cloud assembly manifest.json beside it names the stacks, and the first whose asset manifest lists the template is used.

ExtractAslFromTemplateParams

Template source, optional format, optional resource id, optional readFile and templateFileName for definition files.

ExtractAslResult

The extracted ASL definition, the logical id used, and any warnings.

If no state machine is found, resourceId matches none or more than one, no resourceId is given and there are several, or the definition is external and not readable (no readFile, an S3 location, a network path, or a DefinitionS3Location with no CDK asset beside the template, or for a nested stack template, no stack in manifest.json that lists it).

const { aslDefinition } = extractAslFromTemplate({ template: cdkSynthJson });
// Pick one of several by the ID passed to `new sfn.StateMachine(this, 'OrderWorkflow', …)`.
const picked = extractAslFromTemplate({ resourceId: 'OrderWorkflow', template: cdkSynthJson });
// picked.resourceId === 'OrderWorkflow0015CDD9'
// Read a SAM DefinitionUri file relative to the template.
const sam = extractAslFromTemplate({
readFile: ({ path }) => readFileSync(join(dirname(templatePath), path), 'utf-8'),
template: readFileSync(templatePath, 'utf-8'),
});
// Pass templateFileName too ('cdk.out/OrdersStack.template.json') to read a CDK asset.