API on AWS with SST
Create and deploy a serverless API in AWS with SST.
We are going to build a serverless API, add an S3 Bucket for file uploads, and deploy it to AWS using SST.
Before you get started, make sure to configure your AWS credentials.
1. Create a project
Let’s start by creating our app.
Init SST
Now let’s initialize SST in our app.
Select the defaults and pick AWS. This’ll create a sst.config.ts
file in your project root.
2. Add an API
Let’s add an API using Amazon API Gateway. Update your sst.config.ts
.
We are adding two routes, one for uploading an image and another for getting the latest uploaded image.
Start dev mode
Start your app in dev mode. This runs your functions Live.
This will give you the URL of your API.
3. Add an S3 Bucket
Let’s add an S3 Bucket for file uploads. Update your sst.config.ts
.
Add this above the Api
component.
Link the bucket
Now, link the bucket to the API routes.
4. Upload a file
We want the /
route of our API to generate a pre-signed URL to upload a file to our S3 Bucket. Create an index.ts
file and add the following.
Add the relevant imports.
And install the npm packages.
5. Download a file
We want the /latest
route of our API to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your index.ts
file.
Test your app
Let’s try uploading a file from your project root. Make sure to use your API URL.
Now head over to https://y9ink9nh96.execute-api.us-east-1.amazonaws.com/latest
in your browser and it’ll download the file you just uploaded.
6. Deploy your app
Now let’s deploy your app to production.
You can use any stage name here but it’s good to create a new stage for production.
Connect the console
As a next step, you can setup the SST Console to git push to deploy your app and monitor it for any issues.
You can create a free account and connect it to your AWS account.