forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stepfunctions): custom state as an escape hatch
Custom State which enables the capability to provide Amazon States Language (ASL) JSON as an escape hatch. Useful when there are capabilities that are offered through Step Functions such as service integrations, and state properties but there isn't support through the CDK yet. It enables the usage of all service integrations we don't currently support.
- Loading branch information
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/@aws-cdk/aws-stepfunctions/lib/states/custom-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Chain } from '..'; | ||
import { IChainable, INextable } from '../types'; | ||
import { State } from './state'; | ||
|
||
/** | ||
* Properties for defining a custom state definition | ||
*/ | ||
export interface CustomStateProps { | ||
/** | ||
* Amazon States Language (JSON-based) definition of the state | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html | ||
*/ | ||
readonly stateJson: { [key: string]: any }; | ||
} | ||
|
||
/** | ||
* State defined by supplying Amazon States Language (ASL) in the state machine. | ||
* | ||
* @experimental | ||
*/ | ||
export class CustomState extends State implements IChainable, INextable { | ||
public readonly endStates: INextable[]; | ||
|
||
/** | ||
* Amazon States Language (JSON-based) definition of the state | ||
*/ | ||
private readonly stateJson: { [key: string]: any}; | ||
|
||
constructor(scope: cdk.Construct, id: string, props: CustomStateProps) { | ||
super(scope, id, {}); | ||
|
||
this.endStates = [this]; | ||
this.stateJson = props.stateJson; | ||
} | ||
|
||
/** | ||
* Continue normal execution with the given state | ||
*/ | ||
public next(next: IChainable): Chain { | ||
super.makeNext(next.startState); | ||
return Chain.sequence(this, next); | ||
} | ||
|
||
/** | ||
* Returns the Amazon States Language object for this state | ||
*/ | ||
public toStateJson(): object { | ||
return { | ||
...this.renderNextEnd(), | ||
...this.stateJson, | ||
}; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/@aws-cdk/aws-stepfunctions/test/custom-state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as stepfunctions from '../lib'; | ||
|
||
describe('Custom State', () => { | ||
test('maintains the state Json provided during construction', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
const stateJson = { | ||
Type: 'Task', | ||
Resource: 'arn:aws:states:::dynamodb:putItem', | ||
Parameters: { | ||
TableName: 'MyTable', | ||
Item: { | ||
id: { | ||
S: 'MyEntry', | ||
}, | ||
}, | ||
}, | ||
ResultPath: null, | ||
}; | ||
|
||
// WHEN | ||
const customState = new stepfunctions.CustomState(stack, 'Custom', { | ||
stateJson, | ||
}); | ||
|
||
// THEN | ||
expect(customState.toStateJson()).toStrictEqual({ | ||
...stateJson, | ||
End: true, | ||
}); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/@aws-cdk/aws-stepfunctions/test/integ.custom-state.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"Resources": { | ||
"StateMachineRoleB840431D": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"states.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".amazonaws.com" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"StateMachine2E01A3A5": { | ||
"Type": "AWS::StepFunctions::StateMachine", | ||
"Properties": { | ||
"DefinitionString": "{\"StartAt\":\"my custom task\",\"States\":{\"my custom task\":{\"Next\":\"final step\",\"Type\":\"Task\",\"Resource\":\"arn:aws:states:::dynamodb:putItem\",\"Parameters\":{\"TableName\":\"my-cool-table\",\"Item\":{\"id\":{\"S\":\"my-entry\"}}},\"ResultPath\":null},\"final step\":{\"Type\":\"Pass\",\"End\":true}},\"TimeoutSeconds\":30}", | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"StateMachineRoleB840431D", | ||
"Arn" | ||
] | ||
} | ||
}, | ||
"DependsOn": [ | ||
"StateMachineRoleB840431D" | ||
] | ||
} | ||
}, | ||
"Outputs": { | ||
"StateMachineARN": { | ||
"Value": { | ||
"Ref": "StateMachine2E01A3A5" | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/@aws-cdk/aws-stepfunctions/test/integ.custom-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as sfn from '../lib'; | ||
|
||
/* | ||
* Stack verification steps: | ||
* | ||
* -- aws stepfunctions describe-state-machine --state-machine-arn <stack-output> has a status of `ACTIVE` | ||
*/ | ||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'aws-stepfunctions-custom-state-integ'); | ||
|
||
const finalStatus = new sfn.Pass(stack, 'final step'); | ||
|
||
const stateJson = { | ||
Type: 'Task', | ||
Resource: 'arn:aws:states:::dynamodb:putItem', | ||
Parameters: { | ||
TableName: 'my-cool-table', | ||
Item: { | ||
id: { | ||
S: 'my-entry', | ||
}, | ||
}, | ||
}, | ||
ResultPath: null, | ||
}; | ||
|
||
const custom = new sfn.CustomState(stack, 'my custom task', { | ||
stateJson, | ||
}); | ||
|
||
const chain = sfn.Chain.start(custom).next(finalStatus); | ||
|
||
const sm = new sfn.StateMachine(stack, 'StateMachine', { | ||
definition: chain, | ||
timeout: cdk.Duration.seconds(30), | ||
}); | ||
|
||
new cdk.CfnOutput(stack, 'StateMachineARN', { | ||
value: sm.stateMachineArn, | ||
}); | ||
|
||
app.synth(); |