Skip to content

Commit

Permalink
Beginnings of Callback flow
Browse files Browse the repository at this point in the history
  • Loading branch information
FyreByrd committed Sep 4, 2024
1 parent 6f62b70 commit 01dcdbe
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
7 changes: 4 additions & 3 deletions source/SIL.AppBuilder.Portal.AWS.Step/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Testing AWS Step Functions Locally

1. If you don't already have the AWS Local Step Functions Docker Image, `docker pull amazon/aws-stepfunctions-local`
1. Create a `.env` file with these contents.
```
AWS_DEFAULT_REGION=dummy
AWS_ACCESS_KEY_ID=dummy
AWS_SECRET_ACCESS_KEY=dummy
```
2. `npm i`
2. `npm start`
2. Use Postman to interact with the REST API
3. `npm i`
3. `npm start`
3. Use Postman to interact with the REST API
72 changes: 66 additions & 6 deletions source/SIL.AppBuilder.Portal.AWS.Step/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const fs = require('node:fs');

const { SFNClient, ListStateMachinesCommand } = require("@aws-sdk/client-sfn"); // CommonJS import
const {
SFNClient,
CreateStateMachineCommand,
DescribeStateMachineCommand,
ListStateMachinesCommand,
StartExecutionCommand
} = require("@aws-sdk/client-sfn"); // CommonJS import

const client = new SFNClient({
endpoint: `http://localhost:8083`,
Expand All @@ -13,6 +20,8 @@ const client = new SFNClient({
}
});

const machines = {};

const app = express();

const port = 3000;
Expand All @@ -31,15 +40,66 @@ app.post('/echo', (req, res) => {
console.log(req.body);
});

app.get('/machines', async (req, res) => {
async function listMachines() {
try {
const response = await client.send(new ListStateMachinesCommand());
res.send(response);
return await client.send(new ListStateMachinesCommand());
}
catch (e) {
console.log(e);
res.send({error: e});
return {error: e};
}
})
}

app.get('/machines', async (req, res) => {
res.send(await listMachines());
});

app.get('/machines/:name', async (req, res) => {
const name = req.params.name;

if (machines[name]) {
res.send(await client.send(new DescribeStateMachineCommand({stateMachineArn: machines[name]})));
return;
}

// Sending 404 when not found something is a good practice
res.status(404).send('State Machine not found');
});

app.get('/flow/start/:name', async (req, res) => {
const name = req.params.name;

if (!machines[name]) {
res.status(404).send('State Machine not found');
return;
}

res.send(await client.send(new StartExecutionCommand({stateMachineArn: machines[name]})));
});

app.post('/token', (req, res) => {
console.log(req.body);
});

const creation = setInterval(async () => {
const res = await listMachines();
if (!res.error) {
console.log("Creating State Machine");
fs.readFile("flow.asl.json", (err, data) => {
client.send(new CreateStateMachineCommand({
name: "workflow",
definition: data.toString('utf8'),
roleArn: "arn:aws:iam::128716708097:role/StepFunctionsLambdaRole"
})).then((r) => {
const key = r.stateMachineArn.split(":").pop();
machines[key] = r.stateMachineArn;
console.log(machines);
}).catch((e) => {
console.log(e);
})
});
clearInterval(creation);
}
}, 5000);

app.listen(port, () => console.log(`Hello world app listening on port ${port}!`));
45 changes: 45 additions & 0 deletions source/SIL.AppBuilder.Portal.AWS.Step/flow.asl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"Comment": "An example of the Amazon States Language using a choice state.",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:states:::http:invoke.waitForTaskToken",
"Parameters": {
"ApiEndpoint": "http://localhost:3000",
"Authentication": {
"ConnectionArn": "arn:aws:events:dummy:123456789012:connection"
},
"Method": "POST",
"Headers": {
"Content-Type": "application/json"
},
"RequestBody": {
"token.$": "$$.Task.Token"
}
},
"Next": "ChoiceState"
},
"ChoiceState": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.name",
"StringEquals": "test",
"Next": "SuccessState"
}
],
"Default": "DefaultState"
},

"DefaultState": {
"Type": "Fail",
"Error": "DefaultStateError",
"Cause": "No Matches!"
},

"SuccessState": {
"Type": "Succeed"
}
}
}

0 comments on commit 01dcdbe

Please sign in to comment.