For the moment, we scheduled the creation of one virus every minute. It is slow, but the rate and cron scheduling expressions does not allow shorter frequencies! And even if it did, it would be nice to have some randomization...
We can do that thanks to EventBridge and Step Functions! Our goal is the make the scheduled lambda send 3 EventBridge messages every minute. Each message will launch a Step Function that will wait a random amount of time between 0 and 60s before creating a virus.
Checkout session-4 branch
git checkout session-4-v2- Notice the new
event-bridge.tsin the ressource folder - Add it to the ressources in the
serverless.ts(end of file) - Give your lambdas the right to dispatch messages on this event bus by uncommenting the correct blocks in the
providersection of theserverless.ts. - Run yarn and deploy your stack.
cd backend
yarn
sls deploy- Open AWS EventBridge to check out your custom event bus.
- Open AWS StepFunctions to check the newly created state machine that does nothing (for the moment)
- Replace the
requestNothinglambda by aspreadViruslambda that sends 3 events of typeVIRUS_CREATION_REQUESTEDin thedojo-serverlessEvent Bus - Rename the
doNothingStep Function to have 3 steps:- A
ChooseWaitTimestate ofTasktype, which triggers a lambda returning a number of seconds to wait (random between 1 and 60) - A
WaitXSecondsstate ofWaittype, that waits for the given number of seconds before triggering the next step (use theSecondsPathproperty) - A final
CreateVirusstate ofTasktype that calls thecreateViruslambda
- A
Done ? Nice work ! Don't forget to kill your stack by running serverless remove in the backend folder ! You just have created an app which trigger a lambda every minute! Don't forget it or it could be expensive ;)
To see final result, checkout master branch!