39
loading...
This website collects cookies to deliver better user experience
$ npx create-serverless-stack@latest rest-api
$ cd rest-api
dev
and the us-east-1
AWS region. This can be changed in the sst.json
in your project root.{
"name": "rest-api",
"stage": "dev",
"region": "us-east-1"
}
stacks/
— App Infrastructurestacks/
directory of your project. SST uses AWS CDK, to create the infrastructure.src/
— App Codesrc/
directory of your project.stacks/MyStack.js
with the following.import * as sst from "@serverless-stack/resources";
export default class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
// Create the HTTP API
const api = new sst.Api(this, "Api", {
routes: {
"GET /notes": "src/list.main",
"GET /notes/{id}": "src/get.main",
"PUT /notes/{id}": "src/update.main",
},
});
// Show the API endpoint in the output
this.addOutputs({
ApiEndpoint: api.url,
});
}
}
sst.Api
construct. And we are adding three routes to it.GET /notes
GET /notes/{id}
PUT /notes/{id}
src/notes.js
.export default {
id1: {
noteId: "id1",
userId: "user1",
createdAt: Date.now(),
content: "Hello World!",
},
id2: {
noteId: "id2",
userId: "user2",
createdAt: Date.now() - 10000,
content: "Hello Old World! Old note.",
},
};
src/list.js
.import notes from "./notes";
export async function main() {
return {
statusCode: 200,
body: JSON.stringify(notes),
};
}
async
to be invoked by AWS Lambda. Even though, in this case we are doing everything synchronously.src/get.js
.import notes from "./notes";
export async function main(event) {
const note = notes[event.pathParameters.id];
return note
? {
statusCode: 200,
body: JSON.stringify(note),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
};
}
src/update.js
.import notes from "./notes";
export async function main(event) {
const note = notes[event.pathParameters.id];
if (!note) {
return {
statusCode: 404,
body: JSON.stringify({ error: true }),
};
}
const data = JSON.parse(event.body);
note.content = data.content;
return {
statusCode: 200,
body: JSON.stringify(note),
};
}
$ npx sst start
src/
directory with ones that connect to your local client.===============
Deploying app
===============
Preparing your SST app
Transpiling source
Linting source
Deploying stacks
dev-rest-api-my-stack: deploying...
✅ dev-rest-api-my-stack
Stack dev-rest-api-my-stack
Status: deployed
Outputs:
ApiEndpoint: https://2q0mwp6r8d.execute-api.us-east-1.amazonaws.com
ApiEndpoint
is the API we just created. Now let's get our list of notes. Head over to the following in your browser. Make sure to replace the URL with your API.https://2q0mwp6r8d.execute-api.us-east-1.amazonaws.com/notes
https://2q0mwp6r8d.execute-api.us-east-1.amazonaws.com/notes/id1
PUT
request. Our browser cannot make this type of request. So use the following command in your terminal.curl -X PUT \
-H 'Content-Type: application/json' \
-d '{"content":"Updating my note"}' \
https://2q0mwp6r8d.execute-api.us-east-1.amazonaws.com/notes/id1
src/list.js
with the following.import notes from "./notes";
export async function main() {
return {
statusCode: 200,
body: JSON.stringify(notes, null, " "),
};
}
/notes
endpoint.https://2q0mwp6r8d.execute-api.us-east-1.amazonaws.com/notes
dev
environment, the one specified in your sst.json
.prod
. This allows us to separate our environments, so when we are working in dev
, it doesn't break the API for our users.$ npx sst deploy --stage prod
stage
names. It prefixes the resources with the stage names to ensure that they don't thrash.$ npx sst remove
$ npx sst remove --stage prod