IoT is not the future anymore. It is happening right here and right now. There are more and more applications for deploying tiny electronic devices and companies are starting to see the value in this approach. To meet the high demand for IoT solutions, Microsoft invested 5 BILLION dollars in their IoT services last year.
Developing and deploying IoT code using Azure services is easy. The hard part is supporting the large amount of data that comes with it. By using the classic approach for developing backend services, scalability, maintenance, deployment and deciding frameworks are the biggest nightmares any architect will face.
Serverless computing comes to solve these issues and allows us to focus on what matters most – the logic. In this session we will discuss the differences between the classic backend approach and the new serverless approach. We will go over the services that Azure provides us for IoT development and how we can connect them to other services on Azure to create a completely serverless system, which will save us development and maintenance time.
Separation of Lanthanides/ Lanthanides and Actinides
.NET Fest 2019. Alex Pshul. When IoT Meets Serverless
1. @AlexPshul
When IoT Meets Serverless
From Design to Production and Monitoring
Alex Pshul
Software Architect & Consultant
@AlexPshul
alex@pshul.com
http://pshul.com
http://codevalue.net
2. @AlexPshul
About Me
Alex Pshul
Architect, Consultant and lecturer
More than 9 years of hands on experience
Talk to me about:
Software Development
Hardware and Gadgets
Gaming
Animals
8. @AlexPshul
Time & Money
Pay per use
Don’t worry about server management
Quicker time to release
Faster to deploy new functionality
Don’t have to manage scaling and load balancing
Focus on business logic instead of servers and boilerplate.
Inherent Auto-Scalability
9. @AlexPshul
When to Serverless
Logic can be disassembled into small modules
Irregular Workloads
Hard to predict load peaks
Run closer to the user
Kyiv to USA
Kyiv to China
10. @AlexPshul
When not to Serverless
Latency is important
A consistently high and predictable workload
Long running tasks that can’t be split into sub-tasks or multiple cycles
Complex computing with high memory/CPU requirements.
12. @AlexPshul
Compute - FaaS
FaaS – Function as a Service
First mentioned by D. J. Wheeler in 1952- ‘The use of sub-routines in programmes’.
Event-Driven serverless compute
Examples:
Azure Functions
AWS Lambda
Google Cloud Functions
13. @AlexPshul
FaaS – Azure Functions
Trigger Oriented
Input & Output Binding
Dependency Injection
Tackle Cold-Start performance hits by leaving host loaded
Premium Plan
AppService Plan
Supports several frameworks and languages
C#, JavaScript, Java, Python, F#, PowerShell & TypeScript
18. @AlexPshul
Events
Process a high number of events per second
Decouple communication between components
Store and transform events
Integrate with other services
19. @AlexPshul
Events – Azure EventHub
Can receive and process millions of events per second
Support Apache Kafka clients
Integrate with other azure services
Provide SDKs for several frameworks
.Net, Node.js, Java, Python, Go, C, Apache Storm
Enable capturing and storing events
Partitioning
23. @AlexPshul
Communication – SignalR Service
Fully managed
Cross Platform
Easily integrated with other Azure resources
Such as Azure Functions
Provides abstractions
WebSockets, Long Polling or Server-sent events (SSE)
Send message to all or to a subset of users
27. @AlexPshul
Do Not Reinvent the Wheel
D2C
Messages
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
28. @AlexPshul
IoT Hub
One of Microsoft`s PaaS solutions for building IoT solutions
Provides the infrastructure for working with devices
Most of the work is defining the devices and coding
SDKs for various languages (.NET, Java, Node.js, Python, C, iOS)
Exposes various endpoints
Integration with other Azure services
29. @AlexPshul
IoT Hub - Tiers
Each tier has 3 paid editions
Each tier provides higher throughput
Makes the service more expensive
Basic tier
Limited features
Cheaper (compared with same standard tier edition)
Standard tier
All features are available
More expensive (compared with same basic tier edition)
Contains a free edition
Standard Free edition
1 free IoT Hub allowed per subscription
Encourages PoC projects
Same features as the Standard tier (Not same throughput)
30. @AlexPshul
Do Not Reinvent the Wheel
D2C
Messages
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
31. @AlexPshul
Device to Cloud Messages
Send device telemetry to the cloud
Using an SDK
Send a message directly using a protocol
MQTT (+ over WebSocket)
AMQP (+ over WebSocket)
HTTPS
Uses a connection string to identify the device in the IoT Hub
Stored by IoT Hub, up to 7 days
Up to 256-KB messages
Frequency depends on the selected IoT Hub edition
32. @AlexPshul
Device to Cloud Messages
static async Task Main(string[] args)
{
// Initialize the device client object
DeviceClient deviceClient =
DeviceClient.CreateFromConnectionString("Device_Connection_String");
// Create the message
var data = new { Temperature = 30, Humidity = 37 };
var messageString = JsonConvert.SerializeObject(data);
Message message = new Message(Encoding.ASCII.GetBytes(messageString));
// Send the message
await deviceClient.SendEventAsync(message);
}
33. @AlexPshul
Do Not Reinvent the Wheel
D2C
Messages
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
D2C
Messages
34. @AlexPshul
Cloud to Device Messages – Regular Messages
Not awaited
Stored in the device queue
If queue is full (>50) - results in an error
Can Reject or Abandon messages (unless MQTT)
Can set feedback for each message
36. @AlexPshul
Cloud to Device Messages
static async Task Main(string[] args)
{
// Initialize the service client object
ServiceClient serviceClient =
ServiceClient.CreateFromConnectionString("Service_Connection_String");
// Create the message
byte[] messageBytes = Encoding.ASCII.GetBytes("Cloud to device message.");
Message message = new Message(messageBytes);
// Send to a specific device
await serviceClient.SendAsync("myDeviceId", message);
}
Backend
37. @AlexPshul
Cloud to Device Messages – Direct Methods
Initiate an action on the device
Receive immediate response
Response contains
Status Code
Payload
39. @AlexPshul
Cloud to Device Messages
static async Task Main(string[] args)
{
// Initialize the service client object
ServiceClient serviceClient =
ServiceClient.CreateFromConnectionString("Service_Connection_String");
// Create method object
var methodInvocation = new CloudToDeviceMethod("GetData");
methodInvocation.SetPayloadJson("10");
// Invoke the direct method asynchronously and get the response from the simulated device.
CloudToDeviceMethodResult response =
await serviceClient.InvokeDeviceMethodAsync("MyDotnetDevice", methodInvocation);
Console.WriteLine($"Status: {response.Status}. Payload: {response.GetPayloadAsJson()}");
}
Backend
40. @AlexPshul
Do Not Reinvent the Wheel
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
D2C
Messages
C2D
Messages
41. @AlexPshul
Devices Management – Twin Properties
Devices can have states
No feedback, unless subscribing to IoT Hub messages
Desired Properties
C2D
Shouldn’t represent device state
Reported Properties
D2C
Should reflect the current device state
42. @AlexPshul
Devices Management – Query Devices
Devices can be queried
Example: Get only the devices that were installed today
Supports queries by twin properties as well
Built in functions that allow more complex scenarios
Simple example
SELECT * FROM devices
Returns all devices and their data
43. @AlexPshul
Devices Management – Device Provisioning Service
Zero-Touch Provisioning
Single IoT Hub
Multitenancy
Solution Isolation
Geo-Sharding
Much more scenarios…
48. @AlexPshul
Do Not Reinvent the Wheel
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
D2C
Messages
Security
49. @AlexPshul
Message Routing
Messages have a common format across protocols
Routes send messages to different endpoints based on a query
IoT Hub handles routing duplication
Supports various endpoint types
Built-in endpoint
Azure Blob Storage
Service Bus Queues and Service Bus Topics
Event Hubs
50. @AlexPshul
Message Routing – Built-in endpoint & Event Hubs
The Build-in endpoint is just like any other Event Hub endpoint
Monitor build-in endpoint messages using Azure IoT Hub Toolkit extension
for VS/VS Code
Stops receiving messages when another route is created
Unless a route to the default endpoint is created explicitly
Can add other Event Hubs for different routes
51. @AlexPshul
Message Routing – Azure Blob Storage
Writes batches of data to the blob storage
When size is reached
When a certain time windows has passed
Supports AVRO format only
JSON format available as a preview
(Not supported in East US, West US and West Europe)
A file is created for each batch of data
52. @AlexPshul
Message Routing – Service Bus Queues and Topics
Session and Duplicate Detection must be disabled
Endpoint will appear as unreachable if above is not met
53. @AlexPshul
Do Not Reinvent the Wheel
C2D
Messages
Devices
Management
Security
Message
Routing
Deployment
D2C
Messages
Message
Routing
56. @AlexPshul
The Problem
It is hard to debug remote resources
Applications are built of small little modules
Resources can be created and disposed of according to scale
A monitoring approach is easier to achieve
And is needed in any case
57. @AlexPshul
Monitor – Azure Functions
Logging is your friend
An ILogger object can be injected to your function
Use Application Insights to view logs
63. @AlexPshul
Summary
Exists on all major cloud providers
Serverless is not always the option
But very useful
Saves money and time
IoT solutions are available as PaaS
Don’t reinvent the wheel
Debugging is not easy, but it is possible
Monitoring is your friend