Amazon MQTT via the AWS IoT service for communication
How you could use MQTT via the AWS IoT service for communication between two .NET Core microservices. For the purpose of this…
Implementing Intercommunication between .NET Core Microservices using MQTT and AWS IoT

In this blog post, we’ll walk through an example of how MQTT can be used through the AWS IoT service for communication between two .NET Core microservices. Specifically, we’ll explore how to set up a “temperature sensor” service that sends data to a “temperature alert” service.
But before we dive in, make sure your AWS credentials are correctly configured on your machine. You can do this via the AWS CLI by running the command aws configure.
Step 1: Creating and Configuring Your IoT Thing in the AWS Console
- Navigate to the AWS IoT Core console.
- Follow the path: “Manage” > “Things” > “Create” > “Single Thing”.
- Name your IoT Thing as “TemperatureSensor” and click “Next”.
- Optional: Create a type and a group, then click “Next”.
- Choose “Create certificate” in the “Add your device to a certificate” section.
- Download all the presented certificates and click “Activate”.
- Click “Attach a policy” and create a new policy.
- Allow
iot:*on resource*for simplicity in this example. - Bear in mind that in a production setting, this policy should be much more restrictive.
Step 2: Implementing the Temperature Sensor Service
Next, we’ll create a simple console application that sends temperature data over MQTT to the AWS IoT Core service.
Here’s the code for that:
using System;
using System.Security.Cryptography.X509Certificates;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
class Program
{
static async Task Main(string[] args)
{
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithClientId(“TemperatureSensor”)
.WithTcpServer(“<Your-AWS-IoT-Endpoint>”, 8883) // Secure MQTT
.WithCredentials(“<Your-AWS-IoT-Thing-Name>”)
.WithCleanSession()
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = new List<byte[]>
{
File.ReadAllBytes(“<Your-Certificate>.crt”),
File.ReadAllBytes(“<Your-PrivateKey>.key”)
},
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false,
AllowUntrustedCertificates = false
})
.Build();
await mqttClient.ConnectAsync(options, CancellationToken.None);
while (true)
{
var temperature = new Random().Next(15, 35); // Simulate temperature data
var message = new MqttApplicationMessageBuilder()
.WithTopic(“temperature”)
.WithPayload(temperature.ToString())
.WithExactlyOnceQoS()
.Build();
await mqttClient.PublishAsync(message, CancellationToken.None);
Console.WriteLine($”Sent temperature: {temperature}”);
await Task.Delay(TimeSpan.FromSeconds(10)); // Send every 10 seconds
}
}
}
Step 3: Implementing the Temperature Alert Service
Finally, let’s set up a service that listens to the temperature topic and sends alerts when the temperature is too high.
Here’s the necessary code:
using System;
using System.Security.Cryptography.X509Certificates;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
class Program
{
static async Task Main(string[] args)
{
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithClientId(“TemperatureAlert”)
.WithTcpServer(“<Your-AWS-IoT-Endpoint>”, 8883) // Secure MQTT
.WithCredentials(“<Your-AWS-IoT-Thing-Name>”)
.WithCleanSession()
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = new List<byte[]>
{
File.ReadAllBytes(“<Your-Certificate>.crt”),
File.ReadAllBytes(“<Your-PrivateKey>.key”)
},
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false,
AllowUntrustedCertificates = false
})
.Build();
await mqttClient.ConnectAsync(options, CancellationToken.None);
await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(“temperature”).Build());
mqttClient.UseApplicationMessageReceivedHandler(e =>
{
var temperature = int.Parse(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
if (temperature > 30)
{
Console.WriteLine($”High temperature alert: {temperature}”);
}
});
Console.ReadLine(); // Keep the application alive
}
}
In the provided examples, replace ”<Your-AWS-IoT-Endpoint>”, ”<Your-AWS-IoT-Thing-Name>”, ”<Your-Certificate>.crt”, and ”<Your-PrivateKey>.key” with your actual values. You can find the endpoint in the settings of your AWS IoT Thing.
Remember that this is a simplified example. In a real-world scenario, you would need to handle potential exceptions and edge cases, and craft a more complex policy. You’d also want to make sure your application is secure and adheres to best practices.
The MQTT client used in this example is MQTTnet, which you can add to your project via NuGet. Note that the MQTTnet.Client.Options namespace might differ based on the version you're using, and you may need to adjust the code accordingly.
메타데이터
- post_id
- cb02e97e83bb
- slug
- amazon-mqtt-via-the-aws-iot-service-for-communication-cb02e97e83bb
- url
- https://medium.com/@engrabdullahabdullah/amazon-mqtt-via-the-aws-iot-service-for-communication-cb02e97e83bb
- canonical_url
- https://medium.com/@engrabdullahabdullah/amazon-mqtt-via-the-aws-iot-service-for-communication-cb02e97e83bb
- author_url
- https://medium.com/@engrabdullahabdullah
- status
- ok
- fetched_at
- 2026-08-18 01:11:02