If TDD is Zen, then TDD + Serverless is Nirvana

Brick Pop
Brick Pop
Jul 19, 2018 · 11 min read

Start me up!

npm i -g serverless       (Windows)
sudo npm i -g serverless (Linux and MacOS)

Cloud accounts

Image for post
Image for post
Amazon AWS IAM Console
Image for post
Image for post
IAM user creation
Image for post
Image for post
AWS IAM user just created
serverless config credentials --provider aws --key <the-access-key-id> --secret <the-secret-access-key>

Image for post
Image for post
MongoDB Atlas
Image for post
Image for post
MongoDB cluster just created
Image for post
Image for post
DB user creation
Image for post
Image for post
Connection settings
Image for post
Image for post
Connection URL

Let’s code!

serverless create --template aws-nodejs --path my-api
cd my-api
serverless invoke local -f hello(output)
{
"statusCode": 200,
"body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"\"}"
}
npm init -y
npm i mongodb
npm i -D serverless-offline serverless-mocha-plugin
serverless offline start
Image for post
Image for post
Serverless offline listening on port 3000
Image for post
Image for post
mkdir handlers test
rm handler.js
touch handlers/users.js
GET /users
GET /users/<id>
POST /users
PUT /users/<id>
DELETE /users/<id>

Time to TDD!

serverless create test -f listUsers
serverless create test -f getUser
serverless create test -f addUser
serverless create test -f updateUser
serverless create test -f removeUser
serverless invoke test
Image for post
Image for post
Test execution

Secret management

const uri = "mongodb+srv://lambda:lambda@myapp...."
const uri = process.env.MONGODB_URL
provider:
name: aws
runtime: nodejs8.10
stage: prod
region: eu-central-1
environment:
MONGODB_URL: ${ssm:MY_API_MONGODB_URL~true}
pip install awscli    # install the AWS CLI if necessary
aws configure # confirm the key/secret, define your region
aws ssm put-parameter --name MY_API_MONGODB_URL --type SecureString --value "mongodb+srv://lambda:xxxxxx@myapp-.....mongodb.net/my-app?retryWrites=true"

Deploying

serverless deploy
Image for post
Image for post
Image for post
Image for post

Production

Cleaning


Wrap up


Bonus track #1: Staging

aws ssm delete-parameter --name MY_API_MONGODB_URL
# PROD
aws ssm put-parameter --name MY_API_MONGODB_URL_prod --type SecureString --value "mongodb+srv://my-app-prod:xxxxxx@myapp-.....mongodb.net/my-app-prod?retryWrites=true"
# STAGING
aws ssm put-parameter --name MY_API_MONGODB_URL_staging --type SecureString --value "mongodb+srv://my-app-staging:xxxxxx@myapp-.....mongodb.net/my-app-staging?retryWrites=true"
Image for post
Image for post
AWS Systems Manager
serverless deploy --stage prod
Image for post
Image for post
Deployment to staging
Image for post
Image for post

Bonus track #2: Automated tasks

...
"scripts": {
"deploy": "npm test && sls deploy --stage staging",
"deploy:prod": "npm test && sls deploy --stage prod",
"start": "serverless offline start",
"test": "serverless invoke test"
},

Stack Me Up

Full stack, Blockchain, Mobile.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store