Rohith Gilla
06/14/2022, 5:21 AMjustindra
06/14/2022, 5:46 AMScott
06/14/2022, 7:18 AMexport function StateMachineFunctionsStack({ stack }: StackContext) {
const { database } = use(DatabaseStack);
const exampleFunction = new Function(stack, 'example-task', {
handler: 'src/service/statemachine/example-task.main',
environment: {
DATABASE: DATABASE,
CLUSTER_ARN: database.clusterArn,
SECRET_ARN: database.secretArn,
},
});
exampleFunction.attachPermissions([database]);
return { exampleFunction };
}
export function StateMachineStack({ stack }: StackContext) {
const { exampleFunction } = use(StateMachineFunctionsStack);
// Define each state
const sWait = new sfn.Wait(stack, 'Wait', {
time: sfn.WaitTime.duration(
cdk.Duration.seconds(Number(process.env.TIME_DELAY) ?? 0)
),
});
const sExampleTask= new tasks.LambdaInvoke(stack, 'ExampleTask', {
lambdaFunction: exampleFunction,
// Lambda's result is in the attribute `Payload`
inputPath: '$',
});
const definition = sWait.next(sExampleTask);
const stateMachine = new sfn.StateMachine(
stack,
'StateMachine',
{
definition,
stateMachineName: 'StateMachine',
}
);
return { stateMachine };
}
import {
SFNClient,
SFNClientConfig,
StartExecutionCommand,
StartExecutionCommandInput,
} from '@aws-sdk/client-sfn';
export const startStateMachine = async (stateMachineARN: string, data: any) => {
const config: SFNClientConfig = { region: 'ap-southeast-2' };
const params: StartExecutionCommandInput = {
stateMachineArn: stateMachineARN,
input: JSON.stringify(data),
};
const client = new SFNClient(config);
console.log('State Machine|Initiating...');
try {
const command = new StartExecutionCommand(params);
await client.send(command);
console.log('State Machine|Initiated');
} catch (err) {
console.log('State Machine|Failed to Initiate', err);
}
};
Rohith Gilla
06/14/2022, 7:19 AMScott
06/14/2022, 7:20 AMRohith Gilla
06/14/2022, 7:22 AMScott
06/14/2022, 7:24 AMScott
06/14/2022, 7:25 AMRohith Gilla
06/14/2022, 7:27 AMRohith Gilla
06/14/2022, 7:28 AMstep
functions so I think I would prolly need to read more about them.justindra
06/14/2022, 3:35 PM