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.ts
in 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
provider
section 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
requestNothing
lambda by aspreadVirus
lambda that sends 3 events of typeVIRUS_CREATION_REQUESTED
in thedojo-serverless
Event Bus - Rename the
doNothing
Step Function to have 3 steps:- A
ChooseWaitTime
state ofTask
type, which triggers a lambda returning a number of seconds to wait (random between 1 and 60) - A
WaitXSeconds
state ofWait
type, that waits for the given number of seconds before triggering the next step (use theSecondsPath
property) - A final
CreateVirus
state ofTask
type that calls thecreateVirus
lambda
- 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!