Skip to content

Commit a25bb9e

Browse files
pedramhakaiz-io
andauthored
add stepfunctions with external asl definition file (#1087)
* add stepfunctions with external asl definition file * Delete typescript/stepfunction-external-definition/test/stepfunction-external-definition.test.ts Does not implement test * Update package.json Does not implement test * Delete typescript/stepfunction-external-definition/jest.config.js Does not implement test * change permission setting --------- Co-authored-by: Michael Kaiser <[email protected]> Co-authored-by: Michael Kaiser <[email protected]>
1 parent 63d0cf7 commit a25bb9e

File tree

9 files changed

+302
-0
lines changed

9 files changed

+302
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Welcome to your CDK TypeScript project
2+
3+
This is a Tyepscript CDK project which enables implementing stepfunctions with external asl(Amazon States Language) file. This can be useful in usecases where it is desired to leverage local development flow of the stepfunction statemachines,e.g. AWS toolkit for VScode, and directly import the asl file into your project without rewriting it in CDK.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
## Useful commands
8+
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npm run test` perform the jest unit tests
12+
* `npx cdk deploy` deploy this stack to your default AWS account/region
13+
* `npx cdk diff` compare deployed stack with current state
14+
* `npx cdk synth` emits the synthesized CloudFormation template
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { StepfunctionExternalDefinitionStack } from '../lib/stepfunction-external-definition-stack';
5+
6+
const app = new cdk.App();
7+
new StepfunctionExternalDefinitionStack(app, 'StepfunctionExternalDefinitionStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/stepfunction-external-definition.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
21+
"@aws-cdk/core:checkSecretUsage": true,
22+
"@aws-cdk/core:target-partitions": [
23+
"aws",
24+
"aws-cn"
25+
],
26+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
27+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
28+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
29+
"@aws-cdk/aws-iam:minimizePolicies": true,
30+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
31+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
32+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
33+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
34+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
35+
"@aws-cdk/core:enablePartitionLiterals": true,
36+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
37+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
38+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
39+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
40+
"@aws-cdk/aws-route53-patters:useCertificate": true,
41+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
42+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
43+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
44+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
45+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
46+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
47+
"@aws-cdk/aws-redshift:columnId": true,
48+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
49+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
50+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
51+
"@aws-cdk/aws-kms:aliasNameRef": true,
52+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
53+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
54+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
55+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
56+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
57+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
58+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
59+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
60+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
61+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
62+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
63+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
64+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
65+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
66+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
67+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
68+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
69+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
70+
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false,
71+
"@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true,
72+
"@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true,
73+
"@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true,
74+
"@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true,
75+
"@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true,
76+
"@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true
77+
}
78+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
3+
import * as apigateway from "aws-cdk-lib/aws-apigateway";
4+
import * as stepfunctions from "aws-cdk-lib/aws-stepfunctions";
5+
import * as logs from 'aws-cdk-lib/aws-logs';
6+
import * as iam from "aws-cdk-lib/aws-iam";
7+
import * as fs from "fs";
8+
9+
export class StepfunctionExternalDefinitionStack extends cdk.Stack {
10+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
11+
super(scope, id, props);
12+
13+
const logGroup = new logs.LogGroup(this, 'MyStateMachineLogGroup', {
14+
logGroupName: '/aws/vendedlogs/states/MyStateMachine',
15+
retention: logs.RetentionDays.ONE_WEEK,
16+
});
17+
18+
const workflow = fs.readFileSync('./workflow/stepfunction.json.asl', 'utf8');
19+
const stateMachine= new stepfunctions.StateMachine(this, 'MyStateMachine', {
20+
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
21+
definitionBody: stepfunctions.DefinitionBody.fromString(workflow.toString()),
22+
logs: {
23+
destination: logGroup,
24+
level: stepfunctions.LogLevel.ALL,
25+
includeExecutionData: true,
26+
},
27+
});
28+
logGroup.grantWrite(stateMachine.role);
29+
30+
const api = new apigateway.RestApi(this, "StepFuncApi", {
31+
restApiName: "StepFuncApi",
32+
description: "StepFuncApi",
33+
endpointTypes: [apigateway.EndpointType.REGIONAL]
34+
});
35+
const resource = api.root.addResource("orders");
36+
resource.addMethod("GET", apigateway.StepFunctionsIntegration.startExecution(stateMachine));
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "stepfunction-external-definition",
3+
"version": "0.1.0",
4+
"bin": {
5+
"stepfunction-external-definition": "bin/stepfunction-external-definition.js"
6+
},
7+
"scripts": {
8+
"build": "tsc",
9+
"watch": "tsc -w",
10+
"cdk": "cdk"
11+
},
12+
"devDependencies": {
13+
"@types/node": "22.5.4",
14+
"aws-cdk": "2.162.1",
15+
"ts-node": "^10.9.2",
16+
"typescript": "~5.6.2"
17+
},
18+
"dependencies": {
19+
"aws-cdk-lib": "2.162.1",
20+
"constructs": "^10.0.0",
21+
"source-map-support": "^0.5.21"
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2020",
7+
"dom"
8+
],
9+
"declaration": true,
10+
"strict": true,
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"noImplicitThis": true,
14+
"alwaysStrict": true,
15+
"noUnusedLocals": false,
16+
"noUnusedParameters": false,
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": false,
19+
"inlineSourceMap": true,
20+
"inlineSources": true,
21+
"experimentalDecorators": true,
22+
"strictPropertyInitialization": false,
23+
"typeRoots": [
24+
"./node_modules/@types"
25+
]
26+
},
27+
"exclude": [
28+
"node_modules",
29+
"cdk.out"
30+
]
31+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"Comment": "smiple workflow for the car company",
3+
"StartAt": "Order Received",
4+
"States": {
5+
"Order Received": {
6+
"Comment": "Order revieved.",
7+
"Type": "Pass",
8+
"Next": "RiskyCustomer"
9+
},
10+
"RiskyCustomer": {
11+
"Comment": "No risky business!",
12+
"Type": "Choice",
13+
"Choices": [
14+
{
15+
"Variable": "$.risk",
16+
"BooleanEquals": true,
17+
"Next": "Yes"
18+
},
19+
{
20+
"Variable": "$.risk",
21+
"BooleanEquals": false,
22+
"Next": "No"
23+
}
24+
],
25+
"Default": "Yes"
26+
},
27+
"Yes": {
28+
"Type": "Pass",
29+
"Next": "organize pickup"
30+
},
31+
"No": {
32+
"Type": "Fail",
33+
"Cause": "Not Hello World"
34+
},
35+
"organize pickup": {
36+
"Comment": "wait for client to pickup",
37+
"Type": "Wait",
38+
"Seconds": 3,
39+
"Next": "wait for 6 months"
40+
},
41+
"wait for 6 months": {
42+
"Comment": "wait for 6 months",
43+
"Type": "Wait",
44+
"Seconds": 3,
45+
"Next": "organize return"
46+
},
47+
"organize return": {
48+
"Comment": "wait for client to return",
49+
"Type": "Wait",
50+
"Seconds": 3,
51+
"Next": "Parallel State"
52+
},
53+
"Parallel State": {
54+
"Comment": "A Parallel state can be used to create parallel branches of execution in your state machine.",
55+
"Type": "Parallel",
56+
"Next": "return",
57+
"Branches": [
58+
{
59+
"StartAt": "book",
60+
"States": {
61+
"book": {
62+
"Type": "Pass",
63+
"End": true
64+
}
65+
}
66+
},
67+
{
68+
"StartAt": "calculate damage",
69+
"States": {
70+
"calculate damage": {
71+
"Type": "Pass",
72+
"End": true
73+
}
74+
}
75+
}
76+
]
77+
},
78+
"return": {
79+
"Type": "Pass",
80+
"End": true
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)