Serverless Architecture for IoT on AWS

Markus Tacker

Markus Tacker

Senior R&D Engineer, Nordic Semiconductor, Trondheim

Software crafter. Code donor. Mentor. Speaker. Conference & Community builder. Camper.

@coderbyheart

Pronouns: he/him

My work at Nordic Semiconductor

2017

First full time cloud engineer working on nrfcloud.com building Software-as-a-service (Saas) offering.

2019

Application Group to work on on open-source end-to-end examples of real world IoT products.

Agenda

  • What is IoT?
    • Protocols for the Internet of Things.
  • What is serverless?
    • Why I like serverless
    • Downsides of serverless
  • AWS Lambda explained.
  • IoT <3 serverless
    • Architecture Example:
      store and retrieve temperature data
  • Learning resources

What is IoT?

  • Computing devices (things) connected to the internet
  • either through a gateway (PC, phone)
  • or directly, using cellular IoT (wireless radio)

Benefits of IoT

Enable new aproaches to conserve and protect resources and make smarter, real-time data-driven decisions.

RAM1

Power transmission tower which detects failures and allows to react immediately and prevent for example wildfires cause by electrical fire.

WakeCap

Hard hat sensor locates wearer across large sites and reports activity/incidents.

Protocols for the Internet of Things

  • ultra shortrange: cm (NFC, …)
  • shortrange: m (Bluetooth, Zigbee, WiFi, …)
  • long range: km (LoRa, LTE, …)

Wireless radio protocols

Wireless radio protocols

LTE-M

  • 375 kbps downlink, 300 kbps uplink
  • ~100 kbps application throughput running IP
  • supports roaming (same as LTE)
  • typically uses frequency bands above 2 Ghz
  • ms-latency

NB-IoT

  • 60 kbps downlink, 30 kbps uplink
  • typically uses frequency bands below 2 Ghz
  • no roaming support (some Telcos do offer custom solution)
  • good indoor/underground penetration characteristics
  • long range

Comparison

LTE-M

for medium throughput applications requiring low power, low latency and/or mobility

  • asset tracking
  • wearables
  • medical
  • POS
  • home security

NB-IoT

for static, low throughput applications requiring low power and long range

  • smart metering
  • smart agriculture
  • smart city

Application data

Typical IoT Data Protocol Configuration

Typical IoT Data Protocol Configuration

The four kinds of data

  1. Device State
  2. Device Configuration
  3. Past Data
  4. Firmware Updates

1. Device State

  • sensor readings (like position, temperature)
  • information about its health (like battery level)

Because the latest state should be immediately visible: buffer data in a Digital Twin.

Typical IoT Data Protocol Configuration

2. Device Configuration

  • change behaviour of device in real time (e.g. sensor sensititiy, timeouts)
  • configure physical state (e.g. locked state of a door lock)
  • cloud side owns device state (because the device may lose power and state)
Typical IoT Data Protocol Configuration

3. Past Data

Cellular IoT devices need to send data about past events: they will be offline most of the time.

Coverage map

4. Firmware Updates

  • 2-3 magnitudes larger than a control message (~250 KB)
  • notification via control channel (MQTT)
  • download via data channel (HTTP): less overhead, supports resume

Data protocols

  • JSON
  • Alternatives to JSON
    • Protocol buffers
    • Flatbuffers
    • CBOR

JSON

{
  "v": {
    "lng": 10.414394,
    "lat": 63.430588,
    "acc": 17.127758,
    "alt": 221.639832,
    "spd": 0.320966,
    "hdg": 0
  },
  "ts": 1566042672382
}

“default” data protocol for IoT (AWS, Azure, Google Cloud)

👍 human readable
👍 schema-less (self-describing)

👎 overhead

Possible Optimizations

GPS location message

{
  "v": {
    "lng": 10.414394,
    "lat": 63.430588,
    "acc": 17.127758,
    "alt": 221.639832,
    "spd": 0.320966,
    "hdg": 0
  },
  "ts": 1566042672382
}


02 36 01 37 51 4b 73 2b
d4 24 40 09 68 06 f1 81
1d b7 4f 40 11 68 cd 8f
bf b4 20 31 40 19 e6 5d
f5 80 79 b4 6b 40 21 1a
30 48 fa b4 8a d4 3f 29
00 00 00 00 00 00 00 00
09 00 e0 cf ac f6 c9 76
42

JSON
114 bytes
without newlines

Protocol Buffers
65 bytes (-42%)
source

Protocol buffers

developers.google.com/protocol-buffers

  • send structured data in efficient binary format without overhead
  • message format can be changed (ammended) without breaking existing clients
  • small implementation for embedded devices exists: nanopb

Flatbuffers

google.github.io/flatbuffers

CBOR

cbor.io

  • maps JSON to binary structures
  • zero configuration needed between exchanging parties
  • support for embedded devices: tinycbor

CBOR: example

GPS location message

{
  "v": {
    "lng": 10.414394,
    "lat": 63.430588,
    "acc": 17.127758,
    "alt": 221.639832,
    "spd": 0.320966,
    "hdg": 0
  },
  "ts": 1566042672382
}


A2 61 76 A6 63 6C 6E 67
FB 40 24 D4 2B 73 4B 51
37 63 6C 61 74 FB 40 4F
B7 1D 81 F1 06 68 63 61
63 63 FB 40 31 20 B4 BF
8F CD 68 63 61 6C 74 FB
40 6B B4 79 80 F5 5D E6
63 73 70 64 FB 3F D4 8A
B4 FA 48 30 1A 63 68 64
67 00 62 74 73 1B 00 00
01 6C 9F 6A CC FE

JSON
114 bytes
without newlines

CBOR
86 bytes (-24%)
source

Summary: Data protocols

Look into denser data protocols!
JSON is for Humans.

  • devices always™ send the same structure:
    no need to transmit it
  • less data to send
    • less money spent on data (grows linear with № of devices)
    • less energy consumed = longer device lifetime
    • lower chance of failed transmit

Transport protocols

  • MQTT+TLS
  • MQTT-SN+(D)TLS
  • CoAP/LWM2M+(D)TLS

MQTT+TLS

common protocol for “ecommerce” cloud vendors
(AWS, Azure, Google Cloud)

  • great fit for asynchronous, event oriented communication: MQTT is bidirectional pub/sub model
  • overhead:
    • topic name in every MQTT package
      № of topics per device: ~3
    • TLS handshake with AWS IoT broker: ~10 KB

MQTT-SN+(D)TLS

MQTT-SN 1.2 Specification

  • optimized version designed specifically IoT
  • supports UDP
  • use numeric IDs instead of strings for topic names
  • better offline support
  • not supported by cloud vendors: needs a (stateful) Gateway

CoAP/LWM2M+(D)TLS

  • common protocol in Telco clouds (Verizon’s Thingspace, AT&T’s IoT Platform)
  • typically used for device management (carrier library)
  • not supported by cloud vendors: needs a (stateful) Gateway.
    Proof-of-concept AWS IoT-LwM2M Gateway: github.com/coderbyheart/leshan-aws

IoT connectivity summary

  • no silver bullet - multiple conflicting dimensions need to be considered
  • highly depends on use case scenario
  • ultra-low power relevant in all scenarios:
    • data = money
    • power consumption = money

What is serverless?

Serverless [architectures] are application designs that […] run in managed, ephemeral containers on a “Functions as a Service” (FaaS) platform.

Benefits

By using these ideas, […] such architectures remove much of the need for a traditional always-on server component.

[They] may benefit from significantly reduced operational cost, complexity, and engineering lead time, at a cost of increased reliance on vendor dependencies and comparatively immature supporting services.

Source: martinfowler.com/articles/serverless.html

Why I like serverless

Serverless computing allows me to focus on implementing the solution, and not spend time on maintaining the infrastructure needed to execute it.

or more practical

  • build globally scalable solutions with a small team
  • suprises are rare, allows me to go on vacation

Horizontal Scalablity

  • Serverless implementations depend on stateless, event-driven implementations.
  • This allows to process as many requests as needed, in parallel.
  • However, you have to deal with eventual consistency.

Updating individual components

Instead of restarting the instance,
I can update an individual function.

Downsides of serverless

Testability

  • Testing cloud-native solutions really is a challenge.

Infrastructure is part of the solution

  • More ceremony involved, defining infrastructure as code is now your job.

Serverless is usually closed source

  • Impossible to run it locally.

Mindset shift

  • Huge mindset shift from the classical application server model (LAMP, Tomcat, Rails), to a serverless, stateless, eventual consistent application development model.

AWS Lambda explained

With Lambda, you can run code virtually with zero administration of the underlying infrastructure. You are responsible only for the code that you provide Lambda, and the configuration of how Lambda runs that code on your behalf.

AWS Lambda Hello World!

const AWS = require("aws-sdk");
const s3 = new AWS.S3();

exports.handler = async (event) => s3.listBuckets().promise();

AWS Lambda execution

const AWS = require("aws-sdk"); // Dependencies included in runtime
const s3 = new AWS.S3(); // Credentials provided by environment (based on role)

// event populated per request
exports.handler = async (event) => s3.listBuckets().promise();
// response is returned to invoker by runtime

Supported runtimes

Built-in

Node.js, Python, Ruby, Java, Go, .NET Core

Custom runtimes

Anything that runs in Amazon Linux 2.

When should I use Lambda?

Lambda is best suited for shorter, event-driven workloads, since Lambda functions run for up to 15 minutes per invocation.

Other limits

  • RAM: max 10 GB
  • payload (event): max 6 MB
  • source code size: max 25 MB / 10 GB container image
  • open files: 1,024
  • /tmp size: max 512 MB

Scaling

  • first invokation (event) creates a function instance
  • stays active after response and waits to process additional events
  • more instances are created if multiple events need processing at the same time
  • instances are discared if number of events decreases
Autoscaling with Provisioned Concurrency

Lambda execution environments

Lambda provides function version level isolation:

  • reserved for a specific function version
    • no reuse across function versions, functions, or AWS accounts
  • only one concurrent invocation at a time

Execution environment

A diagram showing the isolation model for AWS Lambda Workers.

Execution environments are isolated from one another using several container-like technologies. Amazon open-sourced their virtualization technology, called Firecracker.

Execution environment lifecycle

The execution environment lifecycle
  1. download the code for the function from S3 or ECR
  2. create environment with the memory, runtime, and configuration specified
  3. run any initialization code outside of the event handler
  4. run the handler code.

Hitting the cold start zone

Lambda cold starts

Lambda service retains the execution environment for a non-deterministic period of time.

Cold start scenarios

6 simultaneous requests

6 simultaneous requests

6 asynchronous inviations with reserved concurrency 1

6 asynchronous inviations with reserved concurrency 1

6 requests with staggered arrival

S3 bucket events
6 requests with staggered arrival

Cold start times

Cold start times

  • typically <1% of invocations in production deployments

Cold start mitigation

  • reserved concurrency (keep n instances alive all the time)
  • reduce cold start time:
    • reduce deployment artifact size
    • chose leaner execution environment
    • give more resources to Lambda (RAM)
    • reduce start up time (e.g. DB connections)
    • reduce processing run time

IoT <3 serverless

No world

  • Many, many devices, that don’t share global state.

18+ billion IoT devices this year

  • Needs to scale to billions of devices, connections, message per day.

Data for thousands of use cases

  • Many different operations with different business needs (processing) on small datasets, suits serverless microservice model very well.

Architecture Example: store and retrieve temperature data

Historical Data

PDF version - Miro Board

AWS implementation

AWS implementation

Learning resources

Book recommendations

Cellular IoT development

Thank you & happy connecting!

Please share your feedback!

Markus.Tacker@NordicSemi.no
@coderbyheart

Latest version:
bit.ly/awsiotarch

We are hiring!
nordicsemi.com/jobs
Trondheim · Oslo · 20+ more locations