The Serverless Modern Enterprise I
A guide to leaving provisioned servers for cloud-hosted managed services on AWS.
This article will be split into three segments.
Part 1: Installation & Deployment |
Part 2: Cognito
Part 3: In-Cognito
Part I Installation & Configuration of Node.js, Serverless, and the AWS CLI
AWS Lambda. What is it and should your enterprise be using it?
Now, there are cases when provisioned servers are more cost-effeciant. These cases occur mostly when you cross a certain threshold where serverless computing begins to cost more than using Lamda. If you reach this point you probably won’t care about the extra cash since it means your application is doing quite well.
If that’s not an issue, then AWS Lambda is great. It lets you run code without provisioning or managing servers.Less managing means less headaches.
- You pay only for the compute time you consume — there is no charge when your code is not running.
- With Lambda, you can run code for virtually any type of application or backend service — all with zero administration.
Going Serverless
If your installing Node.js, you should be installing it with NVM (Node Version Manager).
Node Version manager allows you to easily switch between node environments as different applications may require different libraries and different versions of Node.js.
NVM Install script
To install or update nvm, you can use the install script using cURL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
or Wget:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
The script clones the nvm repository to ~/.nvm
and adds the source line to your profile (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Note: You can add --no-use
to the end of the above script (...nvm.sh --no-use
) to postpone using nvm
until you manually use
it.
You can customize the install source, directory, profile, and version using the NVM_SOURCE
, NVM_DIR
, PROFILE
, and NODE_VERSION
variables. Eg: curl ... | NVM_DIR="path/to/nvm"
. Ensure that the NVM_DIR
does not contain a trailing slash.
NB. The installer can use git
, curl
, or wget
to download nvm
, whatever is available.
Note: On Linux, after running the install script, if you get nvm: command not found
or see no feedback from your terminal after you type:
command -v nvm
simply close your current terminal, open a new terminal, and try verifying again.
Note: Since OS X 10.9, /usr/bin/git
has been preset by Xcode command line tools, which means we can't properly detect if Git is installed or not. You need to manually install the Xcode command line tools before running the install script, otherwise, it'll fail. (see #1782)
Note: On OS X, if you get nvm: command not found
after running the install script, one of the following might be the reason:-
- your system may not have a [
.bash_profile file
] where the command is set up. Simply create one withtouch ~/.bash_profile
and run the install script again - you might need to restart your terminal instance. Try opening a new tab/window in your terminal and retry.
If the above doesn’t fix the problem, open your .bash_profile
and add the following line of code:
source ~/.bashrc
- For more information about this issue and possible workarounds, please refer here
Verify installation of NVM
To verify that nvm has been installed, do:
command -v nvm
which should output ‘nvm’ if the installation was successful. Please note that which nvm
will not work, since nvm
is a sourced shell function, not an executable binary.
Using NVM to install Node.js
nvm install stable
nvm use stable
Part 1: Installation & Deployment |
AWS CLI Configuration
Exporting the Environment Variables. * Never push to github.
export AWS_ACCESS_KEY_ID=<your-key-here>export AWS_SECRET_ACCESS_KEY=<your-secret-key-here># In Windows — use ‘set’ instead.
Installation of the Serverless Framework
Next Steps…
#global install of serverless
$ npm install serverless -g
Installation of the Starter Pack
https://github.com/AnomalyInnovations/serverless-nodejs-starter
$ serverless install --url https://github.com/yellow11/package-serverless-node --name my-project
Enter the new directory
$ cd my-project
Install the Node.js packages
$ npm install
Usage
To run unit tests on your local
$ npm test
To run a function on your local
$ serverless invoke local --function hello
To simulate API Gateway locally using serverless-offline
$ serverless offline start
Run your tests
$ npm test
I use Jest to run tests. You can read more about setting up your tests here.
Deploy your project
$ serverless deploy
Deploy a single function
$ serverless deploy function --function hello
To add another function as a new file to your project, simply add the new file and add the reference to serverless.yml
. The webpack.config.js
automatically handles functions in different files.
To add environment variables to your project
- Rename
env.example
toenv.yml
. - Add environment variables for the various stages to
env.yml
. - Uncomment
environment: ${file(env.yml):${self:provider.stage}}
in theserverless.yml
. - Make sure to not commit your
env.yml
.