Build a Web App to Deliver Calming and Empowering Affirmations Using AWS Lambda and Amazon DynamoDB
May is Mental Health Month, so why not take a little time to relax and build yourself a mindfulness app?
This app was inspired by the instructions given in this helpful tutorial in the Amazon API Gateway docs.


Before starting, make sure your local computer is set up to usenpm
,git
, andnode
.
npm create astro@latest
in your terminal or command line. A wizard will launch and help you create a folder for your project. Ask the wizard to Include sample files
and be sure to install dependencies. You don't need to use TypeScript in this app, so you can say 'no' to this. Initialize your new Astro folder as a git repository. The terminal will display a message that you've successfully created your app.
cd
to enter the new folder Astro created (you can use ls -al
to view all the folders, and it should match what you entered earlier for the project name) and make sure all dependencies are installed by typing npm i
in the terminal. Type npm run dev
to start your local server. At the moment, your web app looks like this:
tip: typecode .
in your terminal to open the folder in VS Code. If you were already using the VS Code terminal to run the previous commands, click on the+
to create another terminal instance as you want to leave the app running in the background.
src/pages/index.astro
file, replace all the code with the following:AffirmationsComponent.jsx
is missing. Let's fix that.What's going on with the above code? Astro offers an interesting syntax allowing you to combine front matter with style, script, and layout tags. You can progressively enhance your app to include more and more functionality by building up your components' capabilities. By usingclient:load
you are allowing a child component to load fresh data from a database, which is our use case.
Preact is a lightweight version of React, "a fast 3kB alternative to React with the same modern API."
npx astro add preact
in the terminal of your app. Allow Astro to configure your app to suit Preact's requirements (a configuration change is needed). You will be asked a number of times to accept changes to files, so please answer "yes" to all of them.Tip: use the built-in terminal in VS Code to keep your environment neat by selecting terminal > new terminal.
/src/components folder
, remove the Card.astro file and add a file called components/AffirmationComponent.jsx
, a file type understood by Preact.AffirmationComponent.jsx
file, add the following code:
Create table
and name it Affirmations
. For its partition key, enter Id
, and leave the dropdown next to it on the default value of String
. Now click on Create Table
.
Affirmations
to view it. Let's add a few helpful phrases to our table by navigating to Tables
-> Explore Items
in the left-hand navigation bar, and confirming that the Affirmations
table is selected. Now we can add items by using the Create Item
button. Enter a numeric number next to Id
, and then click on Add new attribute
, enter Text
under the Attribute name
column, and then the affirmation as text in the Value
column. Each item should have an Id
and a Text
, like this:
Here's a helpful list of nice affirmations.
Create function
> Author from Scratch
. You can call it get-affirmation-function
and use Node 18 for the runtime. Now you can create your function.Configuration
tab in the middle of the screen. Your function has been assigned a basic 'role' with a few permissions. Click Add Permissions > Attach policies
in the dropdown. Search for DynamoDB
and select the AmazonDynamoDBReadOnlyAccess
access to let your new function interact with DynamoDB. Add this permission. Now you can write your function's code.index.mjs
file in the Lambda code editor. Add the following code and select Deploy
:Note, you are selecting a random affirmation by its Id - so edit themin
andmax
variables to reflect how many affirmations you entered into your database.
Create API
> HTTP API
> Build
. Name your new API http-affirmations-api
. Click the Next
button and create an integration with the Lambda function that you just created. Then add one route, which is the pathway that the API will use to perform various functions between the Lambda function and your database.GET
to select elements from the database. Add /items
as the route's name, then Next
and Create
:
/items
to the end of the URL; you should see your database items listed!Note, you might need to tweak CORS in your API Gateway if you find that the data stops refreshing and there are errors in your browser console related to CORS. Check the CORS settings in API Gateway for your API - add*
in the allowed headers to allow the data to get to your app.
AffirmationComponent.jsx
file. Add these lines to the top:<h3>
tag includes just the one affirmation:New Workflow
button and search for Astro. Select the Astro Workflow and click configure
. This process creates a new folder called .github/workflows
with a .yml
file outlining the steps needed to build your app. Commit this structure to your repo by selecting commit changes
.
Settings
> Pages
to ensure that your app has GitHub Pages configured and that your new Action is building them. GitHub should provide you with an URL. A demo of this whole app is available on this GitHub page.Affirmations
database table in the AWS Console by searching for DynamoDB and choosing the table to delete. Do the same in API Gateway for your http-affirmations-api
asset, and in Lambda, delete the get-affirmations
function. Remove the role that you created in IAM as well. This will save you from any costs that fail to spark joy. Marie Kondo would be proud of you.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.