Skip to content

Commit f30885b

Browse files
authored
Merge pull request #1565 from aligent/feature/MI-276_migrate-step-function-from-file-construct
Feature/mi 276 migrate step function from file construct
2 parents 3cdbb90 + 1acf1eb commit f30885b

17 files changed

+435
-0
lines changed

.changeset/huge-bananas-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aligent/cdk-step-function-from-file": patch
3+
---
4+
5+
Migrating the step function from file from the MS node template to the cdk-constructs repo for use in MS projects

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These are all written for CDK v2. See the offical AWS guide for how to migrate f
1818
| [rabbitmq](packages/rabbitmq) | Create a RabbitMQ cluster within CDK |
1919
| [shared-vpc](packages/shared-vpc) | Creates a single VPC with static IP for micro-services with DNS management |
2020
| [static-hosting](packages/static-hosting) | Construct to deploy infrastructure for static webpage hosting |
21+
| [step-function-from-file](packages/step-function-from-file) | Create a Step Function state machine definition from a given JSON or YAML file |
2122
| [waf](packages/waf) | Configurable WAF infrastructure |
2223

2324
## Contributing
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# TypeScript source files
2+
*.ts
3+
!*.d.ts
4+
5+
# TypeScript configuration
6+
tsconfig*.json
7+
8+
# Jest configuration
9+
jest.config.ts
10+
11+
# Nx project configuration
12+
project.json
13+
14+
# Test files
15+
test/
16+
**/*.test.ts
17+
**/*.test.js
18+
**/*.spec.ts
19+
**/*.spec.js
20+
21+
# Development dependencies
22+
node_modules/
23+
24+
# Keep these files in published package:
25+
# *.js (compiled JavaScript)
26+
# *.d.ts (TypeScript declarations)
27+
# README.md
28+
# CHANGELOG.md
29+
# package.json
30+
# docs/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @aligent/cdk-step-function-from-file
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Aligent AWS Step Function From File
2+
3+
A Step Function construct that loads its definition from a YAML or JSON file.
4+
5+
```typescript
6+
import { StepFunctionFromFile } from "@libs/cdk-utils/infra";
7+
8+
// Basic usage
9+
new StepFunctionFromFile(this, "MyStateMachine", {
10+
filepath: "src/step-functions/workflow.yml",
11+
});
12+
13+
// With Lambda function substitutions
14+
new StepFunctionFromFile(this, "WorkflowWithLambdas", {
15+
filepath: "src/step-functions/workflow.yml",
16+
lambdaFunctions: [myLambda1, myLambda2],
17+
definitionSubstitutions: {
18+
MyCustomParam: "CustomValue",
19+
},
20+
});
21+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
StepFunctionFromFile,
3+
StepFunctionFromFileProps,
4+
} from "./lib/step-function-from-file-construct";
5+
6+
export { StepFunctionFromFile, StepFunctionFromFileProps };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: "step-function-from-file",
4+
preset: "../../jest.preset.js",
5+
testEnvironment: "node",
6+
transform: {
7+
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
8+
},
9+
moduleFileExtensions: ["ts", "js", "html"],
10+
coverageDirectory: "../../coverage/packages/step-function-from-file",
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Comment: A simple minimal example of a Step Functions state machine
2+
StartAt: Hello
3+
States:
4+
Hello:
5+
Type: Pass
6+
Result: Hello
7+
Next: World
8+
World:
9+
Type: Pass
10+
Result: World
11+
End: true
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`StepFunctionFromFile creates a state machine from a file: Rerun tests with the -u flag to update snapshots if changes are expected 1`] = `
4+
{
5+
"MyStateMachine6C968CA5": {
6+
"DeletionPolicy": "Delete",
7+
"DependsOn": [
8+
"MyStateMachineRoleD59FFEBC",
9+
],
10+
"Properties": {
11+
"DefinitionS3Location": {
12+
"Bucket": {
13+
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
14+
},
15+
"Key": "74802786d8a56529517e696861ce369bcaad20d207e2f9614118bd6ccf267e7d.yaml",
16+
},
17+
"RoleArn": {
18+
"Fn::GetAtt": [
19+
"MyStateMachineRoleD59FFEBC",
20+
"Arn",
21+
],
22+
},
23+
},
24+
"Type": "AWS::StepFunctions::StateMachine",
25+
"UpdateReplacePolicy": "Delete",
26+
},
27+
}
28+
`;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Stack } from "aws-cdk-lib";
2+
import { Match, Template } from "aws-cdk-lib/assertions";
3+
import { Code, Runtime } from "aws-cdk-lib/aws-lambda";
4+
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
5+
import { join as pathJoin } from "path";
6+
import { StepFunctionFromFile } from "./step-function-from-file-construct";
7+
8+
const snapshotMessage =
9+
"Rerun tests with the -u flag to update snapshots if changes are expected";
10+
11+
describe("StepFunctionFromFile", () => {
12+
let stack: Stack;
13+
14+
beforeEach(() => {
15+
stack = new Stack();
16+
new StepFunctionFromFile(stack, "MyStateMachine", {
17+
filepath: pathJoin(__dirname, "__data__", "test-machine.asl.yaml"),
18+
});
19+
});
20+
21+
test("creates a state machine from a file", () => {
22+
const template = Template.fromStack(stack);
23+
24+
const stateMachines = template.findResources(
25+
"AWS::StepFunctions::StateMachine"
26+
);
27+
expect(stateMachines).toMatchSnapshot(snapshotMessage);
28+
});
29+
30+
test("creates a state machine with lambda functions", () => {
31+
// Create test lambda functions
32+
const lambda = new NodejsFunction(stack, "TestFunction", {
33+
runtime: Runtime.NODEJS_22_X,
34+
handler: "index.handler",
35+
code: Code.fromInline(
36+
"exports.handler = async () => ({ statusCode: 200 });"
37+
),
38+
});
39+
40+
const otherLambda = new NodejsFunction(stack, "OtherTestFunction", {
41+
runtime: Runtime.NODEJS_22_X,
42+
handler: "index.handler",
43+
code: Code.fromInline(
44+
"exports.handler = async () => ({ statusCode: 200 });"
45+
),
46+
});
47+
48+
new StepFunctionFromFile(stack, "LambdaStateMachine", {
49+
filepath: pathJoin(__dirname, "__data__", "test-machine.asl.yaml"),
50+
lambdaFunctions: [lambda, otherLambda],
51+
definitionSubstitutions: {
52+
ExtraParam: "ExtraValue",
53+
},
54+
});
55+
56+
const template = Template.fromStack(stack);
57+
58+
template.hasResourceProperties("AWS::StepFunctions::StateMachine", {
59+
DefinitionSubstitutions: {
60+
ExtraParam: "ExtraValue",
61+
TestFunction: {
62+
"Fn::GetAtt": [Match.stringLikeRegexp("TestFunction.*"), "Arn"],
63+
},
64+
OtherTestFunction: {
65+
"Fn::GetAtt": [Match.stringLikeRegexp("OtherTestFunction.*"), "Arn"],
66+
},
67+
},
68+
});
69+
70+
// Check that IAM policies grant invoke permissions (there should be 2 policies, one for each lambda)
71+
template.hasResourceProperties("AWS::IAM::Policy", {
72+
PolicyDocument: {
73+
Statement: Match.arrayWith([
74+
Match.objectLike({
75+
Action: "lambda:InvokeFunction",
76+
Effect: "Allow",
77+
}),
78+
]),
79+
},
80+
});
81+
});
82+
});

0 commit comments

Comments
 (0)