npx create-sst@latest --template=base/monorepo expo-app
cd expo-app
npm install
By default, our app will be deployed to the us-east-1 AWS region. This can be changed in the sst.config.ts in your project root.
import { SSTConfig } from "sst";
import { Api } from "sst/constructs";
export default {
config(_input) {
return {
name: "expo-app",
region: "us-east-1",
};
},
} satisfies SSTConfig;
Project Layout
An SST app is made up of a couple of parts.
stacks/ — App Infrastructure
The code that describes the infrastructure of your serverless app is placed in the stacks/ directory of your project. SST uses AWS CDK, to create the infrastructure.
packages/ — App Code
The code that’s run when your API is invoked is placed in the packages/ directory of your project.
frontend/ — Expo App
The code for our frontend Expo app.
Create our infrastructure
Our app is made up of a simple API and a Expo app. The API will be talking to a database to store the number of clicks. We’ll start by creating the database.
Adding the table
We’ll be using Amazon DynamoDB; a reliable and highly-performant NoSQL database that can be configured as a true serverless database. Meaning that it’ll scale up and down automatically. And you won’t get charged if you are not using it.
Replace the stacks/MyStack.ts with the following.
import { StackContext, Table, Api } from "@serverless-stack/resources";
export function MyStack({ stack }: StackContext) {
// Create the table
const table = new Table(stack, "Counter", {
fields: {
counter: "string",
},
primaryIndex: { partitionKey: "counter" },
});
}
This creates a serverless DynamoDB table using the SST Table construct. It has a primary key called counter. Our table is going to look something like this:
Creating our API
Add this below the Table definition in stacks/MyStack.ts.
// Create the HTTP API
const api = new Api(stack, "Api", {
defaults: {
function: {
// Bind the table name to our API
bind: [table],
},
},
routes: {
"POST /": "functions/lambda.handler",
},
});
// Show the URLs in the output
stack.addOutputs({
ApiEndpoint: api.url,
});
We are using the SST Api construct to create our API. It simply has one endpoint (the root). When we make a POST request to this endpoint the Lambda function called handler in packages/functions/src/lambda.ts will get invoked.
We’ll also bind our table to our API. It allows our API to access (read and write) the table we just created.
Reading from our table
Our API is powered by a Lambda function. In the function we’ll read from our DynamoDB table.
Replace packages/functions/src/lambda.ts with the following.
import { DynamoDB } from "aws-sdk";
import { Table } from "@serverless-stack/node/table";
const dynamoDb = new DynamoDB.DocumentClient();
export async function handler() {
const getParams = {
// Get the table name from the environment variable
TableName: Table.Counter.tableName,
// Get the row where the counter is called "clicks"
Key: {
counter: "clicks",
},
};
const results = await dynamoDb.get(getParams).promise();
// If there is a row, then get the value of the
// column called "tally"
let count = results.Item ? results.Item.tally : 0;
return {
statusCode: 200,
body: count,
};
}
We make a get call to our DynamoDB table and get the value of a row where the counter column has the value clicks. Since we haven’t written to this column yet, we are going to just return 0.
Let’s install the aws-sdk package in the packages/ folder.
$ npm install aws-sdk
And let’s test what we have so far.
Setting your dev environment
SST features a Live Lambda Development environment that allows you to work on your serverless apps live.
$ npm start
The first time you run this command it’ll take a couple of minutes to deploy your app and a debug stack to power the Live Lambda Development environment.
Let’s test our endpoint with the SST Console. The SST Console is a web based dashboard to manage your SST apps. Learn more about it in our docs.
Go to the API tab and click Send button to send a POST request.
Note, The API explorer lets you make HTTP requests to any of the routes in your Api construct. Set the headers, query params, request body, and view the function logs with the response.
You should see a 0 in the response body.
Setting up our Expo app
We are now ready to use the API we just created. Let’s use Expo CLI to setup our Expo app.
Run the following in the project root and create a blank project.
Create a .env file inside frontend/ and create two variables to hold dev and prod API endpoints and replace DEV_API_URL with the deployed URL from the steps above.
Now if you head over to your emulator, your Expo app should look something like this.
Of course if you click on the button multiple times, the count doesn’t change. That’s because we are not updating the count in our API. We’ll do that next.
Making changes
Let’s update our table with the clicks.
Add this above the return statement in packages/functions/src/lambda.js.
Here we are updating the clicks row’s tally column with the increased count.
And if you head over to your emulator and click the button again, you should see the count increase!
Also let’s go to the DynamoDB tab in the SST Console and check that the value has been updated in the table.
Note, The DynamoDB explorer allows you to query the DynamoDB tables in the Table constructs in your app. You can scan the table, query specific keys, create and edit items.
Deploying to prod
To wrap things up we’ll deploy our app to prod.
$ npx sst deploy --stage prod
This allows us to separate our environments, so when we are working locally it doesn’t break the app for our users.
Once deployed, you should see something like this.
Run the below command to open the SST Console in prod stage to test the production endpoint.
npx sst console --stage prod
Go to the API tab and click Send button to send a POST request.
Now we are ready to ship our app!
Cleaning up
Finally, you can remove the resources created in this example using the following commands.
$ npx sst remove
$ npx sst remove --stage prod
Conclusion
And that’s it! We’ve got a completely serverless click counter Expo app. A local development environment, to test and make changes. And it’s deployed to production as well, so you can share it with your users. Check out the repo below for the code we used in this example. And leave a comment if you have any questions!
Setting up SST Expo app Tesla WebApp SmartCar
Import example app
By default, our app will be deployed to the us-east-1 AWS region. This can be changed in the sst.config.ts in your project root.
Project Layout
An SST app is made up of a couple of parts.
stacks/
— App InfrastructureThe code that describes the infrastructure of your serverless app is placed in the stacks/ directory of your project. SST uses AWS CDK, to create the infrastructure.
packages/
— App CodeThe code that’s run when your API is invoked is placed in the packages/ directory of your project.
frontend/
— Expo AppThe code for our frontend Expo app.
Create our infrastructure
Our app is made up of a simple API and a Expo app. The API will be talking to a database to store the number of clicks. We’ll start by creating the database.
Adding the table
We’ll be using Amazon DynamoDB; a reliable and highly-performant NoSQL database that can be configured as a true serverless database. Meaning that it’ll scale up and down automatically. And you won’t get charged if you are not using it.
Replace the
stacks/MyStack.ts
with the following.This creates a serverless DynamoDB table using the SST
Table
construct. It has a primary key calledcounter
. Our table is going to look something like this:Creating our API
Add this below the
Table
definition instacks/MyStack.ts
.We are using the SST
Api
construct to create our API. It simply has one endpoint (the root). When we make aPOST
request to this endpoint the Lambda function calledhandler
inpackages/functions/src/lambda.ts
will get invoked.We’ll also bind our table to our API. It allows our API to access (read and write) the table we just created.
Reading from our table
Our API is powered by a Lambda function. In the function we’ll read from our DynamoDB table.
Replace
packages/functions/src/lambda.ts
with the following.We make a
get
call to our DynamoDB table and get the value of a row where the counter column has the valueclicks
. Since we haven’t written to this column yet, we are going to just return0
.Let’s install the
aws-sdk
package in thepackages/
folder.$ npm install aws-sdk
And let’s test what we have so far.
Setting your dev environment
SST features a Live Lambda Development environment that allows you to work on your serverless apps live.
$ npm start
The first time you run this command it’ll take a couple of minutes to deploy your app and a debug stack to power the Live Lambda Development environment.
The
ApiEndpoint
is the API we just created.Let’s test our endpoint with the SST Console. The SST Console is a web based dashboard to manage your SST apps. Learn more about it in our docs.
Go to the API tab and click Send button to send a
POST
request.Note, The API explorer lets you make HTTP requests to any of the routes in your
Api
construct. Set the headers, query params, request body, and view the function logs with the response.You should see a
0
in the response body.Setting up our Expo app
We are now ready to use the API we just created. Let’s use Expo CLI to setup our Expo app.
Run the following in the project root and create a blank project.
This sets up our Expo app in the
frontend/
directory.We also need to load the environment variables from our SST app. To do this, we’ll be using the
babel-plugin-inline-dotenv
package.Install the
babel-plugin-inline-dotenv
package by running the following in thefrontend/
directory.$ npm install babel-plugin-inline-dotenv
We need to update our script to use this package in
babel.config.js
.Update your
babel.config.js
like below.Create a .
env
file insidefrontend/
and create two variables to hold dev and prod API endpoints and replaceDEV_API_URL
with the deployed URL from the steps above.We’ll add the
PROD_API_URL
later in this example.Let’s start our Expo development environment.
In the
frontend/
directory run the following for the iOS emulator.$ expo start --ios
Or run this for the Android emulator.$ expo start --android
This will open up an emulator and load your app.Add the click button
We are now ready to add the UI for our app and connect it to our serverless API.
Replace
frontend/App.js
with.Here we are adding a simple button that when clicked, makes a request to our API. We are getting the API endpoint from the environment variable.
The response from our API is then stored in our app’s state. We use it to display the count of the number of times the button has been clicked.
Let’s add some styles.
Add a
StyleSheet
in yourApp.js
.Now if you head over to your emulator, your Expo app should look something like this.
Of course if you click on the button multiple times, the count doesn’t change. That’s because we are not updating the count in our API. We’ll do that next.
Making changes
Let’s update our table with the clicks.
Add this above the return statement in
packages/functions/src/lambda.js
.Here we are updating the
clicks
row’stally
column with the increased count.And if you head over to your emulator and click the button again, you should see the count increase!
Also let’s go to the DynamoDB tab in the SST Console and check that the value has been updated in the table.
Note, The DynamoDB explorer allows you to query the DynamoDB tables in the
Table
constructs in your app. You can scan the table, query specific keys, create and edit items.Deploying to prod
To wrap things up we’ll deploy our app to prod.
$ npx sst deploy --stage prod
This allows us to separate our environments, so when we are working locally it doesn’t break the app for our users.
Once deployed, you should see something like this.
Add the above endpoint to the
.env
file infrontend/.env
as the production API endpointRun the below command to open the SST Console in prod stage to test the production endpoint.
npx sst console --stage prod
Go to the API tab and click Send button to send a
POST
request.Now we are ready to ship our app!
Cleaning up
Finally, you can remove the resources created in this example using the following commands.
Conclusion
And that’s it! We’ve got a completely serverless click counter Expo app. A local development environment, to test and make changes. And it’s deployed to production as well, so you can share it with your users. Check out the repo below for the code we used in this example. And leave a comment if you have any questions!