Povery is a framework for building things on AWS Lambda with Typescript
This is the CLI for Povery.
You can easily serve locally many lambdas as an http server, or you can invoke them locally from the terminal.
Unlike other things like this, it does not want to create lambdas or any kind of infrastructure on AWS for you (just deploy them, if you wish).
We firmly believe on separation of concerns between code that handles application logic and code for infra. Mixing those things, it always gets messy at some point...
- Every lambda has a named folder under
lambda
folder. - The entrypoint of the lambda MUST BE
index.ts
file. - API Gateway MUST USE proxy integration to respond to api request.
- Lambdas that serve API Gateway SHOULD BE prefixed with
API_
(e.g.API_Something
) and start with a capital letter. - Lambdas that serve ant other events SHOULD BE prefixed with
EVENT_
(e.g.EVENT_Something
) and start with a capital letter.
As rules are clear, this cli needs a defined structure to work properly. The structure is as follows:
/<project_root>
lambda/
API_Something/
index.ts
EVENT_Something/
index.ts
event.json
povery.json
If you want to install it globally, you can run:
npm i -g povery-cli
A better choice is to install it locally on your project as a dev dependency, in which case you can run:
npm i -D povery-cli
In this case, you can add a script to your package.json
file to run it:
{
"scripts": {
"povery-cli": "povery-cli"
}
}
And call it like this:
npm run povery-cli
Just remeber that in this way, specific options to povery-cli
must be passed after --
npm run povery-cli function deploy API_Something -- --stage dev
povery-cli --help
To start a local web server for testing, configure povery.json
file with the routes you need.
// povery.json
{
"lambdas": {
"API_Something": [
{
"method": "ANY",
"path": "/{proxy+}"
}
]
}
}
Then run:
povery-cli start
It runs your ts code with the json file event.json
in the lambda folder.
povery-cli function invoke EVENT_Something
Working with shared code beteen your lambdas, you will want to add some kind of "common" folders outside of your lambda
folder.
Importing file into index.ts code with relative imports will break transpilations
// WRONG LAMBDA CODE on index.ts
// THIS BREAKS STUFF
import { Something } from '.../.../common/something.ts';
You should instead use aliases and import external deps with them
import { Something } from '@common/something.ts'
and configure your tsconfig.json
properly like this
"compilerOptions": {
...
"baseUrl": "./",
"paths": {
"@common/*": "common/*",
"povery": "node_modules/povery"
}
...
}
Note the path to povery
needed to avoid misinterpretation of povery import from esbuild ts transpilation.
There are more options you can specify on your povery.json
file:
- `` (empty string): It will deploy your lambdas without any prefix or alias. Example:
API_Something
STAGE_PREFIX
: It will deploy your lambdas with the stage name as prefix. Example:dev_API_Something
STAGE_ALIAS
: It will deploy your lambdas with the stage name as alias. Example:API_Something:dev
You can specify a script to run instead of the default npm install
when building your lambdas.
You can give specific configurations for esbuild stage, like this
{
"esbuild": {
"external": ["pg"]
}
}
This is particularly useful for excluding from bundling particular libreries that you want to put on a Lambda Layer, for example libraries that gives compilation errors or libraries that have dynamic imports) o big libraries that makes your index file huge and slows down cold starts.
You can deploy any lambda or all of them at once.
To deploy a single lambda, you can follow the wizard once you enter
povery-cli function
Or, if you know exaclty the name of the lambda you need to deploy, you can run:
povery-cli function deploy <lambda_name>
To deploy all of your lambdas, you can run:
povery-cli deploy
This is particularly useful for CI/CD pipelines.
By default, povery.cli will minify your code and will produce a single file and a source map. This is for performance reasons, because it will be order of magnitudes farter to execute the code, in particular during cold starts. The stack traces may result unreadable, but you can actually enable the source maps generated by enabling them via env variable. To do so, add this env variable to your lambda:
NODE_OPTIONS=--enable-source-maps
Note that this can cause a performance hit, so it's not very recommended to use it in production. We suggest to use an error tracking software like Sentry and to upload source maps there.
Please add this to your .gitignore
file:
.serverless.*
There should be a .envrc
file in the root of your project, defining the env variables for the project. Every exported variable in this file will be available to the execution of your local lambdas.
Feel free to open issues and PRs. We will be happy to review them.
MIT