Running Containerized Node.js Services
on AWS EB
zupzup.org
...or “why are there so many buzzwords in my title?”
Running ServerlessContainerized Node.js MicroServices
on AWS EB and AWS Lambda, Cross-compiled from Rust
zupzup.org
It could have been worse...
zupzup.orgWhy so many Buzzwords?● Back to simple JavaScript Testing
● An inside look into DOM-diffing algorithms
● Learning JavaScript beyond the Fancy Frameworks
● Running Containerized NodeJS Services on AWS EB
● Ponies
zupzup.orgWhy so many Buzzwords?● Back to simple JavaScript Testing
● An inside look into DOM-diffing algorithms
● Learning JavaScript beyond the Fancy Frameworks
● Running Containerized NodeJS Services on AWS EB
● Ponies
zupzup.orgWhy so many Buzzwords?● Back to simple JavaScript Testing
● An inside look into DOM-diffing algorithms
● Learning JavaScript beyond the Fancy Frameworks
● Running Containerized NodeJS Services on AWS EB
● Ponies
zupzup.orgWhy so many Buzzwords?● Back to simple JavaScript Testing
● An inside look into DOM-diffing algorithms
● Learning JavaScript beyond the Fancy Frameworks
● Running Containerized NodeJS Services on AWS EB
● Ponies
zupzup.org
● Back to simple JavaScript Testing
● An inside look into DOM-diffing algorithms
● Learning JavaScript beyond the Fancy Frameworks
● Running Containerized NodeJS Services on AWS EB
● Ponies
Why so many Buzzwords?
zupzup.orgThis Talk● ~ 30 minutes
● NOT a demo-talk
● NOT a tutorial
● Concepts, Trade-offs & Impulse
● Q&A afterwards (Open Space)
zupzup.orgOverview● ~3 min intro to Docker (Containers)● ~3 min intro to AWS & AWS EB● Steps needed to “Run Containe…on AWS EB”● Trade-offs● Node.js for “serious applications”● Things (Libs etc.) I like for node backend development
○ Finally something useful!
● Conclusion
zupzup.orgContainers (Docker)● Containerization
○ “Putting into an isolated environment, which can run anywhere”
● Initially based on LXC (linux containers - 2008)○ Awkward to use, not widely spread back then
● Idea of a “portable environment”○ For the whole development cycle (dev - testing - staging - prod)○ You can do this with VMs (vagrant etc.)
zupzup.orgDocker● No new ideas? Around since 2008? Why the hype?
○ Docker made it mainstream with usability (tooling) and marketing
● A Linux utility that can create, run and ship containers○ By now a lot more - huge ecosystem
● Lightweight, self-contained Linux process○ Runs in isolation using cgroups, namespaces etc.○ Shares kernel of host system○ Only 1 process per container
zupzup.orgDocker (Useful Properties)● Lightweight, Low Overhead
● Fast Startup
● Stateless
● Declarative & Layered
● Ecosystem (tooling, integrations) for development cycle
zupzup.orgDocker● ./my-app/Dockerfile
○ Description how to build an image (i.e.: template for a container)
● docker build -t mzupzup/my-app .● docker run mzupzup/my-app● docker push mzupzup/my-app
○ https://hub.docker.com/ ○ Or any other registry
● docker pull mzupzup/my-app
● ...
zupzup.orgAWS● Amazon Web Services
● Cloud Computing Provider
● On-Demand Model○ Pay per use
● 16 Geographical Regions○ Endless Scaling
● Tons of services (~70)
zupzup.orgAWS
Don’t try to read this, it’s small on purpose!
zupzup.orgAWS● Widely used
● Good Documentation (mostly)
● Lots of Tooling
● 2017 estimate: ~13 billion in revenue
zupzup.orgAWS Elastic Beanstalk● Orchestration Service
● Application consists of multiple “services”○ Computation & Storage○ DB & Cache○ Loadbalancing○ Deployment○ ...
● That’s what EB is here to help you with
● No extra costs, just underlying infrastructure
zupzup.orgAWS Elastic Beanstalk (start)
zupzup.orgAWS Elastic Beanstalk (~10 m later)
zupzup.orgAWS Elastic Beanstalk (Config)● Logs● Monitoring● Auto-Scaling● Adding a Database● Deployment Strategies● …● Basically, everything you need to ship & scale an
Application
zupzup.org
Running Containerized node.js Services on AWS EB
zupzup.orgMonolith Approach (1 Application, 1 Deployment)
Loadbalancer
API Service (nodeJS)
DB
SPA (Re-emb-gular.js)
CacheSync Service (nodeJS)
zupzup.orgMicroServices approach (multiple Applications)
Loadbalancer
Sync Service (nodeJS) API Service (nodeJS) DB
Cache
SPA (Re-emb-gular.js)
Loadbalancer
DB
Loadbalancer
Edge Service (nginx)
zupzup.orgExample nodeJS Dockerfile
zupzup.orgExample SPA Dockerfile
zupzup.orgAdding a DB
zupzup.orgEnv Variables for Secrets
REALLY_SECRET_PW 1234
const conn = new DB({ host: process.ENV.DB_HOST,pw: process.ENV.REALLY_SECRET_PW,
});
zupzup.orgAWS EB CLI (for use in CI for example)eb init / eb create
eb deploy
-> Dockerrun.aws.json
eb status / eb health / eb events / eb logs / eb config
eb terminate
zupzup.orgExample Dockerrun.aws.json
zupzup.org
Running Containerized nodeJS Services on AWS EB
zupzup.orgTrade-offs, Setup Time● Very Quick to get something Running
○ e.g.: dev/testing/staging environments
● Don’t need much expertise initially○ In Production, this can change very quickly
● Likely to be less work/cost than setting up your own cluster (root server etc.)
● Advanced configuration gets complex very quickly
zupzup.orgTrade-offs, Cost (Pay Per Use vs. Monthly)● Very low use: Yes
○ Testing / staging / CI○ low traffic systems in general
■ my blog :(
● Avg. & fairly constant traffic: No○ Monthly billing (e.g.: root-servers) often much cheaper
● Veeery High Use: Yes○ Probably can’t run your own Datacenter○ Automatic Scaling kicks in○ Especially on a global scale (multiple regions)
zupzup.orgGood Practices● Containers (not necessarily Docker)
○ “Portable environment”
● Clean CI / CD pipeline
● Well designed architecture○ Not necessarily Microservices○ Clear Service Boundaries
zupzup.orgnode.js for “serious applications”● “Serious Application”
○ ~ Relevant, productive, (distributed/moderately complex) system which generates value
● Lots of successful examples○ Linkedin○ Walmart○ Uber○ Paypal○ … you’ll find those for ANY language / technology…
zupzup.org
● Event-driven, Non-blocking I/O○ Which is what most web-apps do - they don’t “think” much
● Fast Setup & Iteration Speed○ Prototyping & Getting sh*t “done”!
● Huge Ecosystem & Community○ Finding Developers○ Has a dark side!
● Low Footprint○ Nice for distributed systems○ “Build for Obsolescence”
What node seems to be good at
zupzup.orgWhat node seems to be less good at● CPU-intensive tasks
● Hype / Maturity
● Maintenance and Robustness○ Dynamic vs. Static○ Error handling○ Testing
● Async model not intuitive for many○ Callback Hell○ Promise Hell○ Reactive Hell
zupzup.orgGood Practices● Handle all errors
○ Even (especially!) in {asyncPrimitive}-hell
● Write lots(!) of tests○ Even if asynchronous integration tests are no fun
● “Soft” type checking where it counts○ Service Boundaries
● Document your code & processes
zupzup.orgLibs I like (right now?) for node.js Backend dev● hapi.js (web framework)
○ joi (request/response validation)○ boom (http friendly errors)○ good (monitoring)
● knex (multi-dialect query builder)● winston (logging)● node-config (app configuration)● mocha/chai (testing)
● the classics○ moment / async / lodash / bluebird / request
zupzup.orgThings I try to avoid● Dependencies (i.e. complexity)● Unnecessary build-steps (transpiling etc.)● Mocks
○ If you mock a promise, chaining a mocked promise … what are you really testing?
○ Isolate the actual logic and test it in isolation
● Really anything that makes testing, monitoring & error handling harder
○ Hard enough to get to a “robust” application as it is
zupzup.org
● Containerization has some cool properties
● Node.js is great for iteration speed and starting out
● AWS EB gets you started quickly & scales well later on
● Depending on your trade-offs & experience, it actually might be a good idea to … … “Run containerized node.js services on AWS EB” … … but it also might not in many cases…
Conclusion
zupzup.org
Focus more on
Good Practices
and less on Fancy Technology
Conclusion
zupzup.orgResources● http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_ecs.html● http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html● https://www.docker.com/● https://nodejs.org/en/● https://hapijs.com/● http://momentjs.com/● http://bluebirdjs.com/docs/getting-started.html● http://knexjs.org/● https://github.com/winstonjs/winston● https://github.com/request/request● https://mochajs.org/● http://chaijs.com/● https://lodash.com/● https://github.com/caolan/async● https://github.com/lorenwest/node-config ● http://www.slideshare.net/AmazonWebServices/aws-elastic-beanstalk-running-microservices-and-docker
●
zupzup.orgThank you!Mario Zupan
Freelance & Open Source Software Developer
(starting March 1st)
You can hire me!
https://zupzup.org/
https://github.com/zupzup/
https://twitter.com/mzupzup
meee!!