Install S:
npm i -g @eahefnawy/s
Start building your serverless library by creating an index.js
file that exports your functions, and an optional config object:
const createUser = async (args) => {
// create user here...
return user
}
const updateUser = async (args) => {
// update user here...
return user
}
const deleteUser = async (args) => {
// delete user here...
return user
}
const config = {
name: 'users', // defaults to parent dir name
region: 'us-east-2', // defaults to us-east-1
description: 'library of user functions', // defaults to "S: Build serverless libraries on AWS"
memory: 512, // defaults to 128
timeout: 300, // defaults to 10
env: {
SOME_TOKEN: 'abc'
},
origins: ['example.com'] // origin domains authorized to access your library. defaults to *
}
module.exports = { createUser, updateUser, deleteUser, config }
Sync your serverless library. Notice the returned SID:
$ s
✔ SID: a-qwertyuiop (copied to clipboard)
$
Now you can use your new serverless library by passing the SID and importing the functions you'd like to use:
const S = require('@eahefnawy/s')
const config = {
SID: 'a-qwertyuiop',
import: ['createUser', 'updateUser', 'deleteUser']
}
const { createUser, updateUser, deleteUser } = S(config)
const createdUser = await createUser(args)
const updatedUser = await updateUser(args)
const deletedUser = await deleteUser(args)
Check the logs of your serverless library:
$ s logs
Today at 12:02:14 PM Function Called: createUser
Today at 12:02:14 PM Created User eahefnawy
Today at 12:25:59 PM Function Called: updateUser
Today at 12:25:59 PM Updated User eahefnawy
Today at 12:34:12 PM Function Called: deleteUser
Today at 12:34:12 PM Deleted User eahefnawy
Pro Tip: Changing the name or the region results in a new SID. You can make use of environment variables to create different stages based on the library name:
const config = {
name: `users-${process.env.S_STAGE}`
}
module.exports = { config }