Quick-starting an AWS Serverless Framework Project
victoroliveirab
Posted on December 8, 2020
Step -1: Download Node.JS to your computer
Step 0: Download Serverless Framework CLI via npm
:
npm i -g serverless
Step 1: Setup AWS Credentials to your Serverless CLI:
sls config credentials --provider aws --key YOURKEY --secret YOURSECRETKEY
In order to access your key and your secret key, you must log in to AWS console and navigate to IAM Management and click on Users. Create a user, allow programmatic access, attach an existing policy called
AdministratorAccess
to it, and then click on your recently created User. Navigate to Security Credentials, click on Create access key, and take note of the two values, especially the secret key as you won't be able to visualize it again.
Step 2: Create a project setting a template and a path. A list of templates (along with many of the information I am providing here) can be found here:
sls create --template aws-nodejs-typescript --path project-name
This will generate a NodeJS project with a
package.json
file,handler.ts
, which contains a sample lambda function, andserverless.yml
, which contains configuration to the AWS services we might want to set.
Step 3: Navigate to your newly created project and install the libraries listed on package.json
:
cd project-name && npm i
Step 4: Install some developing tools to improve your experience. I recommend the following:
npm i -D prettier serverless-offline serverless-dotenv-plugin
Prettier is a very well known opinative code formatted; Serverless Offline is a package that emulates AWS API Gateway and Lambda on your local machine, which facilitates a lot the development cycle, testing, and debugging; Serverless DotENV allows us to store environment variables in a
.env
file and access its values in our code andserverless.yml
file.
Step 4.1: In case you opted to install at least one of the serverless plugins I mentioned before, add them to your serverless.yml
plugins list:
plugins:
- serverless-webpack # This one should be already there for the template I used here
- serverless-offline
- serverless-dotenv-plugin
Step 5: Make sure everything is working correctly by launching serverless offline
in your localhost
:
sls offline
And, in another shell session:
curl http://localhost:3000/dev/hello
If this command prints a JSON response to your console, you're good to start developing 😄
Posted on December 8, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.