As far as I’m concerned as a largely front-end developer, I have found the Serverless paradigm to be extremely useful to call third-party APIs, such as MailChimp, that
does not support client-side implementation of their API using CORS requests due to the potential security risk of exposing account API keys.
This article is about consolidating all information on configuring the Serverless framework in order to let you go on building your business idea!
Pre-requisites
- Node.js
v6.5.0
or later. npm install -g serverless
- An AWS account. Free tier. 1 million free request per month for AWS Lambda. Always.
Make sure you put in a credit card or you would run into this error
AWS Access Key Id needs a subscription for the service
4. Set up your Provider credentials in AWS ‘ so that it [Serverless Framework] can create and manage resources on your behalf.’ See next section.
Set up your ‘Provider credentials’
1. Create or login to your Amazon Web Services Account and go to the Identity & Access Management (IAM) page.
2. Click on Users and then Add user. Enter a name in the first field to remind you this User is the Framework, like
serverless-admin
. Enable Programmatic access by clicking the checkbox. Click Next to go through to the Permissions page. Click on Attach existing policies directly. Search for and select AdministratorAccess then click Next: Review. Check everything looks good and click Create user. Later, you can create different IAM Users for different apps and different stages of those apps. That is, if you don't use separate AWS accounts for stages/apps, which is most common.3. View and copy the API Key & Secret to a temporary place. You’ll need it in the next step.
Those copied API Key & Secret
Well, actually you can just do this:
serverless config credentials --provider aws --key AKIAIOSFODNN7EXAMPLE --secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
and be on your way.
However, my preferred way is to install the aws-cli
because it potentially allows you to switching different credential profiles that each of them would be for a different application under your one AWS account. So let’s do that.
Install Python & pip
We are going to use the Python package manager called pip
to install aws-cli
. Follow the guide below for your platform:
Window:
macOS:
Plug in the key & secret
Once the aws-cli
is installed, in your terminal, type aws configure
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER
And now you should be all set up! Do sls start
to start your Serverless development 🚀
Dev & Debugging Practices
Although you would always need to do sls deploy
when you edited the serverless.yml
file, it’s super slow in terms of feedback loop if you are making lots of changes and testing on a specific function, in which case, consider using sls deploy function -f myFuncName
💪
Equipped with that, now consider open a separate terminal and do serverless logs -f myFuncName -t
that provides live updates where you can inspect any exceptions thrown or your console.log
output as you are pinging your functions.
Having learned all that, now remember, always catch any exceptions! Otherwise it’s very hard to find out where went wrong in the Serverless land.
For example, if you have a promise in one of your functions, don’t forget the catch
method:
Promise
.then(res => ...)
.catch(error => console.log(error))
And the last trick is to develop it offline in your localHost
by emulating AWS lambda and API Gateway locally:
npm install serverless-offline --save-dev
- Then add the
plugins
key inserverless.yml
plugins:
- serverless-offline
3. Finally, sls offline start
in your terminal to start developing in your localHost
!
OK that should be all for now. 🎈