Knowledge Bank
  • My GitBook
  • Miscellaneous
  • Project
    • Rider and Intellij
    • Code
    • Frontend
      • Condition
      • AddConditionModalDialog
    • Backend
    • e2e
      • fragments
  • JAVASCRIPT
    • Promise
    • Destructuring
    • Spread Syntax and Rest Parameters
    • Typescript
      • Examples of Types
      • React Typescript
    • This
    • Dot Notation vs Bracket Notation
    • Shallow vs Deep Clone
    • New ES Edition
  • C#
    • Project Note
    • Basic
    • Shortcut and Debugging
  • Programming Paradigms
    • SOLID Principles
    • Object Oriented Programming (OOP)
      • Evolution of OOP (Procedural to OOP)
      • Instantiation
      • 4 Pillars of OOP
      • Extra
    • Functional Programming (FP)
      • Idempotent
      • Imperative vs Declarative
      • Immutability
      • High Order Function and Closure
      • Currying
      • Partial Application
      • Memoization and Caching
      • Compose and Pipe
      • Extra
      • Example of FP
    • OOP vs FP
      • Composition vs Inheritance
  • DATA STRUCTURE
    • Big O
    • Data Structure
    • Array
    • Hash Table
    • Linked List
    • Queue and Stack
    • Tree
      • Binary Heap
      • Trie
    • Graph
      • Example of Graph
  • React-Redux
    • MobX
    • Best Practices
  • Algorithms
    • Recursion
      • Examples of Recursion
    • Sorting
    • Searching and Traversal
    • Dynamic Programming
  • REFACTORING
    • Clean Code
      • Formatting
      • Error Handling
      • Concurrency
      • Testing
      • SOLID Principles
      • Classes
      • Objects and Data Structures
      • Variables
      • Functions
    • Code Smells
      • Long Function
      • Duplicate Code
      • Loops
      • Double Negative
      • Christmas Tree Code
      • Complex Condition
      • Primitive Obsession
      • Speculative Generality
      • God Class
      • Long Parameter List
  • Junior to Senior
    • AWS
      • Lambda
    • Session + Authentication
    • Redis
    • Kubernetes
      • Networking
      • Services
      • Deployment
      • Replica Set
      • YAML
      • pod-definition.yml
      • Kubectl
      • Pods
      • Fundamentals
    • Docker
      • Operating System - Extra
      • Dockerfile - Docker Image
      • Docker Storage
      • Docker Network
      • Docker Registry
      • Docker Command
      • Docker Compose
      • Docker Compose - Postgres
    • Security
      • Logging
      • HTTPS, Cross-Site-Scripting (XSS) and Cross-Site-Request-Forgery (CSRF)
      • 3rd Party Library
      • Injection
      • Code Secret, Secure Header, Access Control, Data Management, Authentication
    • CI/CD
    • SPA vs Server-Side Rendering
    • Performance
      • Optimized Code
      • Critical Render Path
      • Backend Optimization
      • Minimized Files and Images
      • Minimized Delivery
  • SECURITY
    • Encryption
    • SSH
  • Command
  • Cheatsheet
    • NPM
    • GIT
  • Writing Template
    • Guide
    • API
    • ChangeLog
    • FAQ
  • Linux
Powered by GitBook
On this page

Was this helpful?

  1. Junior to Senior
  2. AWS

Lambda

In the past, we've built and deployed web applications where we control everything. We need to be responsible for:

  • Deployment and making sure all resources are set (provisioning and managing resources)

  • Problem with this is we will get charged for keeping the server up even though we are not using or serving out requests

  • Maintenance and upgrade and security

And this is the idea of Serveless come in (Serveless does not mean not using server). But, it simply means we build applications where we hand in to the cloud provided (like AWS). The benefits:

  • We only get charged for what we use. (because cloud provider can reallocate unused server to other company)

  • Scalability, maintenance, and security are taken care

Lambda

When we give a function to AWS Lambda, it is going to run it for us. Underneath the hood, the cloud provider creates a container (like Docker) and it runs the function inside of it. So if there are five users requesting, they are going to create five separate container for each user and give response.

All they do is simply store the function that we give it in their database and whenever it's being called it will grab it from the database and actually run it. Now you may see a problem with this and this is actually called the cold start problem. When the first time it is being called, they grab the function from database, it will takes a bit of time (which is still fast)

Serverless

NPM package that helps us deploy AWS lambda functions from our command line.

// create a boiler template
sls create -t aws-nodejs

In order for our serverless to connect with AWS, we have to use a service called IAM (Identity and Access Management)

// connect serverless with AWS
sls config credentials --provider aws --key ${yourKey} --secret ${yourSecretKey}

Lambda is stateless and that is it's not going to remember anything for us. Remember these things are created because the function is stored in a database and when it's ready to be invoked it's going to be put into a container by Amazon and it's going to get run and after it's done running and there's no more requests it's going to get put back into the database or that container is going to go down and there's no way to remember anything.

So you want to keep your functions stateless and if you ever want to store information or data you can use Dynamo DB or S3.

// to deploy
sls deploy

// to invoke
sls invoke --function rank
OR
sls invoke local --function rank 
// This is done in local, so it is not going to be counted as charge
// Downside is there is no connection with other resources that Amazon have like
// S3 and others. So your code cannot have that dependency.

Lambda functions are great if you have a task that you don't want your servers running and you want somebody else (AWS) to manage it for you. This can be because of many reasons but the two main ones:

1. More scalable and can make sure that if you have really high requests at certain time of day, it can be handled.

2. Lower cost compared to managing your own servers.

PreviousAWSNextSession + Authentication

Last updated 4 years ago

Was this helpful?