Skip to content

Commit 271aed4

Browse files
committed
Add initial files
1 parent 81ae486 commit 271aed4

File tree

8 files changed

+158
-11
lines changed

8 files changed

+158
-11
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
node_modules
3+
cdk.out
4+
package-lock.json

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
## My Project
1+
# State Machine with sub-minute Loop
22

3-
TODO: Fill this README out!
3+
This CDK creates a state machine for a 3 seconds loop to trigger a lambda function.
44

5-
Be sure to:
5+
![alt text](assets/state-machine.png)
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
## Get Started
98

10-
## Security
9+
```sh
10+
git clone https://github.com/aws-samples/aws-cdk-state-machine-with-sub-minute-loop.git
11+
cd aws-cdk-state-machine-with-sub-minute-loop
12+
npm install
13+
```
1114

12-
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
13-
14-
## License
15-
16-
This library is licensed under the MIT-0 License. See the LICENSE file.
15+
## Deployment
1716

17+
```sh
18+
npx cdk bootstrap
19+
npx cdk deploy
20+
```

assets/state-machine.png

21.1 KB
Loading

bin/cdk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
import * as cdk from "aws-cdk-lib";
3+
import { CdkStack } from "../lib/cdk-stack";
4+
5+
const app = new cdk.App();
6+
new CdkStack(app, "CdkStack");

cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "npx ts-node bin/cdk.ts"
3+
}

lib/cdk-stack.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as cdk from "aws-cdk-lib";
2+
import * as lambda from "aws-cdk-lib/aws-lambda";
3+
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
4+
import * as tasks from "aws-cdk-lib/aws-stepfunctions-tasks";
5+
6+
export class CdkStack extends cdk.Stack {
7+
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
8+
super(scope, id, props);
9+
10+
// Set hit interval in seconds
11+
const hitIntervalInSeconds = 3;
12+
const hitDurationInSeconds = 1 * 60; // 1 minute * 60 seconds
13+
14+
// The lambda function
15+
const playLambda = new lambda.Function(this, "playLambda", {
16+
description: "Lambda function that pulls will be triggered",
17+
handler: "playLambda.handler",
18+
runtime: lambda.Runtime.NODEJS_14_X,
19+
code: lambda.Code.fromInline(`
20+
exports.handler = async (event, context, callback) => {
21+
console.log('event: ', event);
22+
23+
let index = event.iterator.index;
24+
let step = event.iterator.step;
25+
let count = event.iterator.count;
26+
27+
// do your business
28+
29+
callback(null, {
30+
index,
31+
step,
32+
count,
33+
continue: index < count ? "CONTINUE" : "END",
34+
});
35+
};
36+
`),
37+
});
38+
39+
const ConfigureCount = new sfn.Pass(this, "ConfigureCount", {
40+
result: {
41+
value: {
42+
count: Math.round(hitDurationInSeconds / hitIntervalInSeconds),
43+
index: 0,
44+
step: 1,
45+
},
46+
},
47+
resultPath: "$.iterator",
48+
});
49+
50+
const Iterator = new tasks.LambdaInvoke(this, "PlayTask", {
51+
lambdaFunction: playLambda,
52+
payloadResponseOnly: true,
53+
retryOnServiceExceptions: false,
54+
resultPath: "$.iterator",
55+
});
56+
57+
const Wait = new sfn.Wait(this, "Wait", {
58+
time: sfn.WaitTime.duration(cdk.Duration.seconds(hitIntervalInSeconds)),
59+
}).next(Iterator);
60+
61+
const Done = new sfn.Succeed(this, "Done");
62+
63+
const IsCountReached = new sfn.Choice(this, "IsCountReached", {
64+
comment: "If the count is reached then end the process",
65+
})
66+
.when(sfn.Condition.stringEquals("$.iterator.continue", "CONTINUE"), Wait)
67+
.otherwise(Done);
68+
69+
new sfn.StateMachine(this, "PlayStateMachine", {
70+
stateMachineName: "PlayStateMachine",
71+
definition: ConfigureCount.next(Iterator).next(IsCountReached),
72+
});
73+
}
74+
}

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "aws-cdk-state-machine-with-sub-minute-loop",
3+
"license": "MIT-0",
4+
"version": "0.0.1",
5+
"bin": {
6+
"cdk": "bin/cdk.js"
7+
},
8+
"scripts": {
9+
"build": "tsc",
10+
"watch": "tsc -w",
11+
"test": "jest",
12+
"cdk": "cdk"
13+
},
14+
"devDependencies": {
15+
"aws-cdk": "2.10.0",
16+
"@types/jest": "^26.0.10",
17+
"@types/node": "10.17.27",
18+
"jest": "^26.4.2",
19+
"ts-jest": "^26.2.0",
20+
"ts-node": "^9.0.0",
21+
"typescript": "~3.9.7"
22+
},
23+
"dependencies": {
24+
"aws-cdk-lib": "2.10.0",
25+
"constructs": "^10.0.0"
26+
}
27+
}

tsconfig.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2018",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2018",
7+
],
8+
"declaration": true,
9+
"strict": true,
10+
"noImplicitAny": true,
11+
"strictNullChecks": true,
12+
"noImplicitThis": true,
13+
"alwaysStrict": true,
14+
"noUnusedLocals": false,
15+
"noUnusedParameters": false,
16+
"noImplicitReturns": true,
17+
"noFallthroughCasesInSwitch": false,
18+
"inlineSourceMap": true,
19+
"inlineSources": true,
20+
"experimentalDecorators": true,
21+
"strictPropertyInitialization": false,
22+
"typeRoots": [
23+
"./node_modules/@types"
24+
]
25+
},
26+
"exclude": [
27+
"node_modules",
28+
"cdk.out"
29+
]
30+
}

0 commit comments

Comments
 (0)