visit
Being a developer is amazing. Writing code, solving problems and thinking of ingenious solutions for complicated algorithms is what we live for. But, the grass is not always so green on this side of the fence. Sooner or later you need to get your hands dirty and deploy the app you worked so hard on. Deployments are not always easy. To be blunt, they can be incredibly hard and time-consuming. That’s what we’ll solve in this tutorial.
Hold up a sec. We need to mention a crucial fact. All lambda functions are stateless, meaning they cannot store persistent data. And, if I just said Function as a Service, how do we deploy a whole Node.js application to AWS Lambda?
$ npm install -g serverless
Note: Prefix the command with _sudo_
if you're running this command on Linux.
$ sls config credentials --provider aws --key PUBLIC_KEY --secret SECRET_KEY
Make sure to add your IAM User’s public and secret key instead of the placeholders I specified above. If you skipped this part above, .
$ mkdir serverless-nodejs-app && cd serverless-nodejs-app
Awesome, now what’s left is to run the create
command to generate some starter code for us. This is called a serverless service.
$ sls create -t aws-nodejs -n serverless-nodejs-app
Only one more step before opening a code editor.
$ npm init -y $ npm install --save express serverless-http
That’s it! Let’s open it up in a code editor and do some real coding.
Once you open up the code editor, you’ll see three files. Ignoring the .gitignore
file, let me explain what the handler.js
is first, then I'll move on to the serverless.yml
. The handler will hold all your app logic, all the code. While the serverless.yml
is the configuration file for the resources you'll be creating on AWS.
Go ahead and rename the handler.js
to app.js
, just to make it simpler for us to figure out what goes where.
Delete all the starter code and paste this code snippet into the app.js
file.
// app.js
const express = require('express') const sls = require('serverless-http') const app = express() app.get('/', async (req, res, next) => { res.status(200).send('Hello World!') }) module.exports.server = sls(app)
Seven lines of code 😎. Looks familiar right? Just like you’re used to. That’s it. Believe it or not, there’s nothing more to it. Let’s move on to the serverless.yml
.
# serverless.yml
service: serverless-nodejs-app
provider: name: aws runtime: nodejs8.10 stage: dev region: eu-central-1 functions: app: handler: app.server # reference the file and exported method events: # events trigger lambda functions - http: # this is an API Gateway HTTP event trigger path: / method: ANY cors: true - http: # all routes get proxied to the Express router path: /{proxy+} method: ANY cors: true
Done! All that’s left is to deploy it.
The Serverless Framework will now wrap everything up into a nice bundle, create a CloudFormation file from the serverless.yml
and shoot it off to AWS S3. Once the resources are created and the code is deployed, you'll see an endpoint get sent back to you in the terminal.
Opening up the provided URL in a browser you’ll see Hello World!
get sent back to you.
For now, let’s just add the NODE_ENV
in the secrets.json
.
{ "NODE_ENV": "production" }
As simple as it was to add the secrets file, it’s even easier to just reference the file in the serverless.yml
.
service: serverless-nodejs-app
custom: # add these two lines secrets: ${file(secrets.json)} # reference the secrets.json file
provider: name: aws runtime: nodejs8.10 stage: production # make sure to change this to production region: eu-central-1 environment: # add environment property NODE_ENV: ${self:custom.secrets.NODE_ENV} # reference the NODE_ENV from the secrets.json file
functions: app: handler: app.server events: - http: path: / method: ANY cors: true - http: path: /{proxy+} method: ANY cors: true
Amazing, that’s it! Delete the node_modules
and .serverless
folders from the service and run npm install
once again, but this time with the --production
flag.
$ npm install --production
Great! All that’s left is to re-deploy the service and you’re set.
$ sls deploy
And this is what we end up with.
I guess we’re done? Not really. Having an app running in production just because you installed npm modules with --production
doesn't really cut it. To be able to sleep well at night, I need a bit more. Here's where proper system insight and monitoring tools come to play. Let me show you.
Hope you guys and girls enjoyed reading this as much as I enjoyed writing it. Do you think this tutorial will be of help to someone? Do not hesitate to share. If you liked it, smash the clap below so other people will see this here on Medium.
Originally published at .