Example open source event-driven application that generates a new bed time story for your children every night using Lambda, EventBridge, DynamoDB, App Runner, ChatGPT and DALL-E.
Every day at a configured time an EventBridge Schedule is trigger which triggers a Lambda function.
The create-story
lambda function takes characters and scenes from the Amazon DynamoDB tables and uses ChatGPT (OpenAI API) to create the story. The story is stored with a 2 day TTL in DynamoDB.
An Amazon EventBridge Pipe is configured to listen to all New items created inside the table using streams and triggers an Amazon EventBridge event (StoryCreated).
EventBridge routes the StoryCreated
event to three targets:
The frontend application is running on AWS App Runner and is hosting a NextJS SRR application. When the user goes to the URL in the Email (through SNS topic), the story is loaded and displayed.
This application was designed as a proof of concept, and if you want to take extract patterns there might be some design considerations to understand before you do.
This application is designed for single use, every day it will email a single person a URL to a new story, if you wanted to scale this out to many users you would have to change the architecture to support that.
The frontend application is built with NextJS and hosted in App Runner, the App Runner container has the permission to talk to the DynamoDB table to get stories. The stories have a TTL of 2 days and will not be available after that duration (removed from the table).
Once the story is created, EventBridge will raise an event to many consumers (audio processing, image creation, and SNS), this can lead to race conditions. There could be a chance that the audio or image is not ready when the user views the story on the screen. The application will check for audio and images, and fallback to render just the story if this information is not available yet (due to async processing). For a simple use case this might be fine but if you need to wait you may want to look at patterns like the aggregator or step function workflows that may help with this processing of state.
The application is fairly simple, and three tables seemed to be a pattern that worked well here. The characters and scenes table is not updated very often and the stories hold the generated stories. If you wanted to support many users you will need to consider your access patterns and table design.
First you will need an OpenAI API key, if you donβt have an account you will need to set one. You can go here to get started: https://platform.openai.com/overview
Once you have your key, you will need to add it to Secret Manager the secret name needs to be open-api-key
.
Clone the repository
Change the config.json file (add your email address and cron job)
Run npm run install:all
Run npm run deploy
Populate your DynamoDB databases (Scenes and Characters)
/backend/data/
, change these to what you want.npm run populate-db
to populate these tables.Once done, your application is ready.
EventBridge scheduler will trigger your Lambda function to generate a story at the configured time set in your config.json file (default 7:15pm).
You can also manually trigger the function (
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.