There are more AWS SDK examples available in the AWS Doc SDK Examples
Learn the basics of AWS IoT with an AWS SDK
The following code examples show how to:
Create an AWS IoT Thing.
Generate a device certificate.
Update an AWS IoT Thing with Attributes.
Return a unique endpoint.
List your AWS IoT certificates.
Update an AWS IoT shadow.
Write out state information.
Creates a rule.
List your rules.
Search things using the Thing name.
Delete an AWS IoT Thing.
- .NET
-
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Run an interactive scenario demonstrating AWS IoT features.
/// <summary> /// Scenario class for AWS IoT basics. /// </summary> public class IoTBasics { public static bool IsInteractive = true; public static IoTWrapper? Wrapper = null; public static IAmazonCloudFormation? CloudFormationClient = null; public static ILogger<IoTBasics> logger = null!; private static IoTWrapper _iotWrapper = null!; private static IAmazonCloudFormation _amazonCloudFormation = null!; private static ILogger<IoTBasics> _logger = null!; private static string _stackName = "IoTBasicsStack"; private static string _stackResourcePath = "../../../../../../scenarios/basics/iot/iot_usecase/resources/cfn_template.yaml"; /// <summary> /// Main method for the IoT Basics scenario. /// </summary> /// <param name="args">Command line arguments.</param> /// <returns>A Task object.</returns> public static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonIoT>(new AWSOptions() { Region = RegionEndpoint.USEast1 }) .AddAWSService<IAmazonCloudFormation>() .AddTransient<IoTWrapper>() .AddLogging(builder => builder.AddConsole()) .AddSingleton<IAmazonIotData>(sp => { var iotService = sp.GetRequiredService<IAmazonIoT>(); var request = new DescribeEndpointRequest { EndpointType = "iot:Data-ATS" }; var response = iotService.DescribeEndpointAsync(request).Result; return new AmazonIotDataClient($"https://{response.EndpointAddress}/"); }) ) .Build(); logger = LoggerFactory.Create(builder => builder.AddConsole()) .CreateLogger<IoTBasics>(); Wrapper = host.Services.GetRequiredService<IoTWrapper>(); CloudFormationClient = host.Services.GetRequiredService<IAmazonCloudFormation>(); // Set the private fields for backwards compatibility _logger = logger; _iotWrapper = Wrapper; _amazonCloudFormation = CloudFormationClient; Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the AWS IoT example scenario."); Console.WriteLine("This example program demonstrates various interactions with the AWS Internet of Things (IoT) Core service."); Console.WriteLine(); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } Console.WriteLine(new string('-', 80)); try { await RunScenarioAsync(); } catch (Exception ex) { _logger.LogError(ex, "There was a problem running the scenario."); Console.WriteLine($"\nAn error occurred: {ex.Message}"); } Console.WriteLine(new string('-', 80)); Console.WriteLine("The AWS IoT scenario has successfully completed."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Run the IoT Basics scenario. /// </summary> /// <returns>A Task object.</returns> public static async Task RunScenarioAsync() { // Use static properties if available, otherwise use private fields var iotWrapper = Wrapper ?? _iotWrapper; var cloudFormationClient = CloudFormationClient ?? _amazonCloudFormation; var scenarioLogger = logger ?? _logger; await RunScenarioInternalAsync(iotWrapper, cloudFormationClient, scenarioLogger); } /// <summary> /// Internal method to run the IoT Basics scenario with injected dependencies. /// </summary> /// <param name="iotWrapper">The IoT wrapper instance.</param> /// <param name="cloudFormationClient">The CloudFormation client instance.</param> /// <param name="scenarioLogger">The logger instance.</param> /// <returns>A Task object.</returns> private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { string thingName = $"iot-thing-{Guid.NewGuid():N}"; string certificateArn = ""; string certificateId = ""; string ruleName = $"iotruledefault"; string snsTopicArn = ""; try { // Step 1: Create an AWS IoT Thing Console.WriteLine(new string('-', 80)); Console.WriteLine("1. Create an AWS IoT Thing."); Console.WriteLine("An AWS IoT Thing represents a virtual entity in the AWS IoT service that can be associated with a physical device."); Console.WriteLine(); if (IsInteractive) { Console.Write("Enter Thing name: "); var userInput = Console.ReadLine(); if (!string.IsNullOrEmpty(userInput)) thingName = userInput; } else { Console.WriteLine($"Using default Thing name: {thingName}"); } var thingArn = await iotWrapper.CreateThingAsync(thingName); Console.WriteLine($"{thingName} was successfully created. The ARN value is {thingArn}"); Console.WriteLine(new string('-', 80)); // Step 1.1: List AWS IoT Things Console.WriteLine(new string('-', 80)); Console.WriteLine("2. List AWS IoT Things."); Console.WriteLine("Now let's list the IoT Things to see the Thing we just created."); Console.WriteLine(); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var things = await iotWrapper.ListThingsAsync(); Console.WriteLine($"Found {things.Count} IoT Things:"); foreach (var thing in things.Take(10)) // Show first 10 things { Console.WriteLine($"Thing Name: {thing.ThingName}"); Console.WriteLine($"Thing ARN: {thing.ThingArn}"); if (thing.Attributes != null && thing.Attributes.Any()) { Console.WriteLine("Attributes:"); foreach (var attr in thing.Attributes) { Console.WriteLine($" {attr.Key}: {attr.Value}"); } } Console.WriteLine("--------------"); } Console.WriteLine(); Console.WriteLine(new string('-', 80)); // Step 2: Generate a Device Certificate Console.WriteLine(new string('-', 80)); Console.WriteLine("3. Generate a device certificate."); Console.WriteLine("A device certificate performs a role in securing the communication between devices (Things) and the AWS IoT platform."); Console.WriteLine(); var createCert = "y"; if (IsInteractive) { Console.Write($"Do you want to create a certificate for {thingName}? (y/n)"); createCert = Console.ReadLine(); } else { Console.WriteLine($"Creating certificate for {thingName}..."); } if (createCert?.ToLower() == "y") { var certificateResult = await iotWrapper.CreateKeysAndCertificateAsync(); if (certificateResult.HasValue) { var (certArn, certPem, certId) = certificateResult.Value; certificateArn = certArn; certificateId = certId; Console.WriteLine($"\nCertificate:"); // Show only first few lines of certificate for brevity var lines = certPem.Split('\n'); for (int i = 0; i < Math.Min(lines.Length, 5); i++) { Console.WriteLine(lines[i]); } if (lines.Length > 5) { Console.WriteLine("..."); } Console.WriteLine($"\nCertificate ARN:"); Console.WriteLine(certificateArn); // Step 3: Attach the Certificate to the AWS IoT Thing Console.WriteLine("Attach the certificate to the AWS IoT Thing."); var attachResult = await iotWrapper.AttachThingPrincipalAsync(thingName, certificateArn); if (attachResult) { Console.WriteLine("Certificate attached to Thing successfully."); } else { Console.WriteLine("Failed to attach certificate to Thing."); } Console.WriteLine("Thing Details:"); Console.WriteLine($"Thing Name: {thingName}"); Console.WriteLine($"Thing ARN: {thingArn}"); } else { Console.WriteLine("Failed to create certificate."); } } Console.WriteLine(new string('-', 80)); // Step 4: Update an AWS IoT Thing with Attributes Console.WriteLine(new string('-', 80)); Console.WriteLine("4. Update an AWS IoT Thing with Attributes."); Console.WriteLine("IoT Thing attributes, represented as key-value pairs, offer a pivotal advantage in facilitating efficient data"); Console.WriteLine("management and retrieval within the AWS IoT ecosystem."); Console.WriteLine(); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var attributes = new Dictionary<string, string> { { "Location", "Seattle" }, { "DeviceType", "Sensor" }, { "Firmware", "1.2.3" } }; await iotWrapper.UpdateThingAsync(thingName, attributes); Console.WriteLine("Thing attributes updated successfully."); Console.WriteLine(new string('-', 80)); // Step 5: Return a unique endpoint specific to the Amazon Web Services account Console.WriteLine(new string('-', 80)); Console.WriteLine("5. Return a unique endpoint specific to the Amazon Web Services account."); Console.WriteLine(); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var endpoint = await iotWrapper.DescribeEndpointAsync(); if (endpoint != null) { var subdomain = endpoint.Split('.')[0]; Console.WriteLine($"Extracted subdomain: {subdomain}"); Console.WriteLine($"Full Endpoint URL: https://{endpoint}"); } else { Console.WriteLine("Failed to retrieve endpoint."); } Console.WriteLine(new string('-', 80)); // Step 6: List your AWS IoT certificates Console.WriteLine(new string('-', 80)); Console.WriteLine("6. List your AWS IoT certificates"); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var certificates = await iotWrapper.ListCertificatesAsync(); foreach (var cert in certificates.Take(5)) // Show first 5 certificates { Console.WriteLine($"Cert id: {cert.CertificateId}"); Console.WriteLine($"Cert Arn: {cert.CertificateArn}"); } Console.WriteLine(); Console.WriteLine(new string('-', 80)); // Step 7: Create an IoT shadow Console.WriteLine(new string('-', 80)); Console.WriteLine("7. Update an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device"); Console.WriteLine(); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var shadowPayload = JsonSerializer.Serialize(new { state = new { desired = new { temperature = 25, humidity = 50 } } }); await iotWrapper.UpdateThingShadowAsync(thingName, shadowPayload); Console.WriteLine("Thing Shadow updated successfully."); Console.WriteLine(new string('-', 80)); // Step 8: Write out the state information, in JSON format Console.WriteLine(new string('-', 80)); Console.WriteLine("8. Write out the state information, in JSON format."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var shadowData = await iotWrapper.GetThingShadowAsync(thingName); Console.WriteLine($"Received Shadow Data: {shadowData}"); Console.WriteLine(new string('-', 80)); // Step 9: Set up resources (SNS topic and IAM role) and create a rule Console.WriteLine(new string('-', 80)); Console.WriteLine("9. Set up resources and create a rule"); Console.WriteLine(); // Deploy CloudFormation stack to create SNS topic and IAM role Console.WriteLine("Deploying CloudFormation stack to create SNS topic and IAM role..."); var deployStack = !IsInteractive || GetYesNoResponse("Would you like to deploy the CloudFormation stack? (y/n) "); if (deployStack) { if (IsInteractive) { Console.Write( $"Enter stack resource file path (or press Enter for default '{_stackResourcePath}'): "); var userResourcePath = Console.ReadLine(); if (!string.IsNullOrEmpty(userResourcePath)) _stackResourcePath = userResourcePath; } _stackName = PromptUserForStackName(); var deploySuccess = await DeployCloudFormationStack(_stackName, cloudFormationClient, scenarioLogger); if (deploySuccess) { // Get stack outputs var stackOutputs = await GetStackOutputs(_stackName, cloudFormationClient, scenarioLogger); if (stackOutputs != null) { snsTopicArn = stackOutputs["SNSTopicArn"]; string roleArn = stackOutputs["RoleArn"]; Console.WriteLine($"Successfully deployed stack. SNS topic: {snsTopicArn}"); Console.WriteLine($"Successfully deployed stack. IAM role: {roleArn}"); if (IsInteractive) { Console.Write($"Enter Rule name (press Enter for default '{ruleName}'): "); var userRuleName = Console.ReadLine(); if (!string.IsNullOrEmpty(userRuleName)) ruleName = userRuleName; } else { Console.WriteLine($"Using default rule name: {ruleName}"); } // Now create the IoT rule with the CloudFormation outputs var ruleResult = await iotWrapper.CreateTopicRuleAsync(ruleName, snsTopicArn, roleArn); if (ruleResult) { Console.WriteLine("IoT Rule created successfully."); } else { Console.WriteLine("Failed to create IoT rule."); } } else { Console.WriteLine("Failed to get stack outputs. Skipping rule creation."); } } else { Console.WriteLine("Failed to deploy CloudFormation stack. Skipping rule creation."); } } else { Console.WriteLine("Skipping CloudFormation stack deployment and rule creation."); } Console.WriteLine(new string('-', 80)); // Step 10: List your rules Console.WriteLine(new string('-', 80)); Console.WriteLine("10. List your rules."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var rules = await iotWrapper.ListTopicRulesAsync(); Console.WriteLine("List of IoT Rules:"); foreach (var rule in rules.Take(5)) // Show first 5 rules { Console.WriteLine($"Rule Name: {rule.RuleName}"); Console.WriteLine($"Rule ARN: {rule.RuleArn}"); Console.WriteLine("--------------"); } Console.WriteLine(); Console.WriteLine(new string('-', 80)); // Step 11: Search things using the Thing name Console.WriteLine(new string('-', 80)); Console.WriteLine("11. Search things using the Thing name."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } var searchResults = await iotWrapper.SearchIndexAsync($"thingName:{thingName}"); if (searchResults.Any()) { Console.WriteLine($"Thing id found using search is {searchResults.First().ThingId}"); } else { Console.WriteLine($"No search results found for Thing: {thingName}"); } Console.WriteLine(new string('-', 80)); // Step 12: Cleanup - Detach and delete certificate if (!string.IsNullOrEmpty(certificateArn)) { Console.WriteLine(new string('-', 80)); var deleteCert = "y"; if (IsInteractive) { Console.Write($"Do you want to detach and delete the certificate for {thingName}? (y/n)"); deleteCert = Console.ReadLine(); } else { Console.WriteLine($"Detaching and deleting certificate for {thingName}..."); } if (deleteCert?.ToLower() == "y") { Console.WriteLine("12. You selected to detach and delete the certificate."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } await iotWrapper.DetachThingPrincipalAsync(thingName, certificateArn); Console.WriteLine($"{certificateArn} was successfully removed from {thingName}"); await iotWrapper.DeleteCertificateAsync(certificateId); Console.WriteLine($"{certificateArn} was successfully deleted."); } Console.WriteLine(new string('-', 80)); } // Step 13: Delete the AWS IoT Thing Console.WriteLine(new string('-', 80)); Console.WriteLine("13. Delete the AWS IoT Thing."); var deleteThing = "y"; if (IsInteractive) { Console.Write($"Do you want to delete the IoT Thing? (y/n)"); deleteThing = Console.ReadLine(); } else { Console.WriteLine($"Deleting IoT Thing {thingName}..."); } if (deleteThing?.ToLower() == "y") { await iotWrapper.DeleteThingAsync(thingName); Console.WriteLine($"Deleted Thing {thingName}"); } Console.WriteLine(new string('-', 80)); // Step 14: Clean up CloudFormation stack if (!string.IsNullOrEmpty(snsTopicArn)) { Console.WriteLine(new string('-', 80)); Console.WriteLine("14. Clean up CloudFormation stack."); Console.WriteLine("Deleting the CloudFormation stack and all resources..."); var cleanup = !IsInteractive || GetYesNoResponse("Do you want to delete the CloudFormation stack and all resources? (y/n) "); if (cleanup) { var ruleCleanupSuccess = await iotWrapper.DeleteTopicRuleAsync(ruleName); var stackCleanupSuccess = await DeleteCloudFormationStack(_stackName, cloudFormationClient, scenarioLogger); if (ruleCleanupSuccess && stackCleanupSuccess) { Console.WriteLine("Successfully cleaned up CloudFormation stack and all resources."); } else { Console.WriteLine("Some cleanup operations failed. Check the logs for details."); } } else { Console.WriteLine($"Resources will remain. Stack name: {_stackName}"); } Console.WriteLine(new string('-', 80)); } } catch (Exception ex) { scenarioLogger.LogError(ex, "Error occurred during scenario execution."); // Cleanup on error if (!string.IsNullOrEmpty(certificateArn) && !string.IsNullOrEmpty(thingName)) { try { await iotWrapper.DetachThingPrincipalAsync(thingName, certificateArn); await iotWrapper.DeleteCertificateAsync(certificateId); } catch (Exception cleanupEx) { scenarioLogger.LogError(cleanupEx, "Error during cleanup."); } } if (!string.IsNullOrEmpty(thingName)) { try { await iotWrapper.DeleteThingAsync(thingName); } catch (Exception cleanupEx) { scenarioLogger.LogError(cleanupEx, "Error during Thing cleanup."); } } // Clean up CloudFormation stack on error if (!string.IsNullOrEmpty(snsTopicArn)) { try { await _iotWrapper.DeleteTopicRuleAsync(ruleName); await DeleteCloudFormationStack(_stackName, cloudFormationClient, scenarioLogger); } catch (Exception cleanupEx) { scenarioLogger.LogError(cleanupEx, "Error during CloudFormation stack cleanup."); } } throw; } } /// <summary> /// Deploys the CloudFormation stack with the necessary resources. /// </summary> /// <param name="stackName">The name of the CloudFormation stack.</param> /// <param name="cloudFormationClient">The CloudFormation client.</param> /// <param name="scenarioLogger">The logger.</param> /// <returns>True if the stack was deployed successfully.</returns> private static async Task<bool> DeployCloudFormationStack(string stackName, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { Console.WriteLine($"\nDeploying CloudFormation stack: {stackName}"); try { var request = new CreateStackRequest { StackName = stackName, TemplateBody = await File.ReadAllTextAsync(_stackResourcePath), Capabilities = new List<string> { Capability.CAPABILITY_NAMED_IAM } }; var response = await cloudFormationClient.CreateStackAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"CloudFormation stack creation started: {stackName}"); bool stackCreated = await WaitForStackCompletion(response.StackId, cloudFormationClient, scenarioLogger); if (stackCreated) { Console.WriteLine("CloudFormation stack created successfully."); return true; } else { scenarioLogger.LogError($"CloudFormation stack creation failed: {stackName}"); return false; } } else { scenarioLogger.LogError($"Failed to create CloudFormation stack: {stackName}"); return false; } } catch (AlreadyExistsException) { scenarioLogger.LogWarning($"CloudFormation stack '{stackName}' already exists. Please provide a unique name."); var newStackName = PromptUserForStackName(); return await DeployCloudFormationStack(newStackName, cloudFormationClient, scenarioLogger); } catch (Exception ex) { scenarioLogger.LogError(ex, $"An error occurred while deploying the CloudFormation stack: {stackName}"); return false; } } /// <summary> /// Waits for the CloudFormation stack to be in the CREATE_COMPLETE state. /// </summary> /// <param name="stackId">The ID of the CloudFormation stack.</param> /// <param name="cloudFormationClient">The CloudFormation client.</param> /// <param name="scenarioLogger">The logger.</param> /// <returns>True if the stack was created successfully.</returns> private static async Task<bool> WaitForStackCompletion(string stackId, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { int retryCount = 0; const int maxRetries = 30; const int retryDelay = 10000; while (retryCount < maxRetries) { var describeStacksRequest = new DescribeStacksRequest { StackName = stackId }; var describeStacksResponse = await cloudFormationClient.DescribeStacksAsync(describeStacksRequest); if (describeStacksResponse.Stacks.Count > 0) { if (describeStacksResponse.Stacks[0].StackStatus == StackStatus.CREATE_COMPLETE) { return true; } if (describeStacksResponse.Stacks[0].StackStatus == StackStatus.CREATE_FAILED || describeStacksResponse.Stacks[0].StackStatus == StackStatus.ROLLBACK_COMPLETE) { return false; } } Console.WriteLine("Waiting for CloudFormation stack creation to complete..."); await Task.Delay(retryDelay); retryCount++; } scenarioLogger.LogError("Timed out waiting for CloudFormation stack creation to complete."); return false; } /// <summary> /// Gets the outputs from the CloudFormation stack. /// </summary> /// <param name="stackName">The name of the CloudFormation stack.</param> /// <param name="cloudFormationClient">The CloudFormation client.</param> /// <param name="scenarioLogger">The logger.</param> /// <returns>A dictionary of stack outputs.</returns> private static async Task<Dictionary<string, string>?> GetStackOutputs(string stackName, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { try { var describeStacksRequest = new DescribeStacksRequest { StackName = stackName }; var response = await cloudFormationClient.DescribeStacksAsync(describeStacksRequest); if (response.Stacks.Count > 0) { var outputs = new Dictionary<string, string>(); foreach (var output in response.Stacks[0].Outputs) { outputs[output.OutputKey] = output.OutputValue; } return outputs; } return null; } catch (Exception ex) { scenarioLogger.LogError(ex, $"Failed to get stack outputs for {stackName}"); return null; } } /// <summary> /// Deletes the CloudFormation stack and waits for confirmation. /// </summary> /// <param name="stackName">The name of the CloudFormation stack.</param> /// <param name="cloudFormationClient">The CloudFormation client.</param> /// <param name="scenarioLogger">The logger.</param> /// <returns>True if the stack was deleted successfully.</returns> private static async Task<bool> DeleteCloudFormationStack(string stackName, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { try { var request = new DeleteStackRequest { StackName = stackName }; await cloudFormationClient.DeleteStackAsync(request); Console.WriteLine($"CloudFormation stack '{stackName}' is being deleted. This may take a few minutes."); bool stackDeleted = await WaitForStackDeletion(stackName, cloudFormationClient, scenarioLogger); if (stackDeleted) { Console.WriteLine($"CloudFormation stack '{stackName}' has been deleted."); return true; } else { scenarioLogger.LogError($"Failed to delete CloudFormation stack '{stackName}'."); return false; } } catch (Exception ex) { scenarioLogger.LogError(ex, $"An error occurred while deleting the CloudFormation stack: {stackName}"); return false; } } /// <summary> /// Waits for the stack to be deleted. /// </summary> /// <param name="stackName">The name of the CloudFormation stack.</param> /// <param name="cloudFormationClient">The CloudFormation client.</param> /// <param name="scenarioLogger">The logger.</param> /// <returns>True if the stack was deleted successfully.</returns> private static async Task<bool> WaitForStackDeletion(string stackName, IAmazonCloudFormation cloudFormationClient, ILogger<IoTBasics> scenarioLogger) { int retryCount = 0; const int maxRetries = 30; const int retryDelay = 10000; while (retryCount < maxRetries) { var describeStacksRequest = new DescribeStacksRequest { StackName = stackName }; try { var describeStacksResponse = await cloudFormationClient.DescribeStacksAsync(describeStacksRequest); if (describeStacksResponse.Stacks.Count == 0 || describeStacksResponse.Stacks[0].StackStatus == StackStatus.DELETE_COMPLETE) { return true; } } catch (AmazonCloudFormationException ex) when (ex.ErrorCode == "ValidationError") { return true; } Console.WriteLine($"Waiting for CloudFormation stack '{stackName}' to be deleted..."); await Task.Delay(retryDelay); retryCount++; } scenarioLogger.LogError($"Timed out waiting for CloudFormation stack '{stackName}' to be deleted."); return false; } /// <summary> /// Helper method to get a yes or no response from the user. /// </summary> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; } /// <summary> /// Prompts the user for a stack name. /// </summary> private static string PromptUserForStackName() { if (IsInteractive) { Console.Write($"Enter a name for the CloudFormation stack (press Enter for default '{_stackName}'): "); string? input = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(input)) { var regex = new System.Text.RegularExpressions.Regex("[a-zA-Z][-a-zA-Z0-9]*"); if (!regex.IsMatch(input)) { Console.WriteLine($"Invalid stack name. Using default: {_stackName}"); return _stackName; } return input; } } return _stackName; } }A wrapper class for AWS IoT SDK methods.
/// <summary> /// Wrapper methods to use Amazon IoT Core with .NET. /// </summary> public class IoTWrapper { private readonly IAmazonIoT _amazonIoT; private readonly IAmazonIotData _amazonIotData; private readonly ILogger<IoTWrapper> _logger; /// <summary> /// Constructor for the IoT wrapper. /// </summary> /// <param name="amazonIoT">The injected IoT client.</param> /// <param name="amazonIotData">The injected IoT Data client.</param> /// <param name="logger">The injected logger.</param> public IoTWrapper(IAmazonIoT amazonIoT, IAmazonIotData amazonIotData, ILogger<IoTWrapper> logger) { _amazonIoT = amazonIoT; _amazonIotData = amazonIotData; _logger = logger; } /// <summary> /// Creates an AWS IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing to create.</param> /// <returns>The ARN of the Thing created, or null if creation failed.</returns> public async Task<string?> CreateThingAsync(string thingName) { try { var request = new CreateThingRequest { ThingName = thingName }; var response = await _amazonIoT.CreateThingAsync(request); _logger.LogInformation($"Created Thing {thingName} with ARN {response.ThingArn}"); return response.ThingArn; } catch (Amazon.IoT.Model.ResourceAlreadyExistsException ex) { _logger.LogWarning($"Thing {thingName} already exists: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't create Thing {thingName}. Here's why: {ex.Message}"); return null; } } /// <summary> /// Creates a device certificate for AWS IoT. /// </summary> /// <returns>The certificate details including ARN and certificate PEM, or null if creation failed.</returns> public async Task<(string CertificateArn, string CertificatePem, string CertificateId)?> CreateKeysAndCertificateAsync() { try { var request = new CreateKeysAndCertificateRequest { SetAsActive = true }; var response = await _amazonIoT.CreateKeysAndCertificateAsync(request); _logger.LogInformation($"Created certificate with ARN {response.CertificateArn}"); return (response.CertificateArn, response.CertificatePem, response.CertificateId); } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't create certificate. Here's why: {ex.Message}"); return null; } } /// <summary> /// Attaches a certificate to an IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="certificateArn">The ARN of the certificate to attach.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> AttachThingPrincipalAsync(string thingName, string certificateArn) { try { var request = new AttachThingPrincipalRequest { ThingName = thingName, Principal = certificateArn }; await _amazonIoT.AttachThingPrincipalAsync(request); _logger.LogInformation($"Attached certificate {certificateArn} to Thing {thingName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot attach certificate - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't attach certificate to Thing. Here's why: {ex.Message}"); return false; } } /// <summary> /// Updates an IoT Thing with attributes. /// </summary> /// <param name="thingName">The name of the Thing to update.</param> /// <param name="attributes">Dictionary of attributes to add.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingAsync(string thingName, Dictionary<string, string> attributes) { try { var request = new UpdateThingRequest { ThingName = thingName, AttributePayload = new AttributePayload { Attributes = attributes, Merge = true } }; await _amazonIoT.UpdateThingAsync(request); _logger.LogInformation($"Updated Thing {thingName} with attributes"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing attributes. Here's why: {ex.Message}"); return false; } } /// <summary> /// Gets the AWS IoT endpoint URL. /// </summary> /// <returns>The endpoint URL, or null if retrieval failed.</returns> public async Task<string?> DescribeEndpointAsync() { try { var request = new DescribeEndpointRequest { EndpointType = "iot:Data-ATS" }; var response = await _amazonIoT.DescribeEndpointAsync(request); _logger.LogInformation($"Retrieved endpoint: {response.EndpointAddress}"); return response.EndpointAddress; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't describe endpoint. Here's why: {ex.Message}"); return null; } } /// <summary> /// Lists all certificates associated with the account. /// </summary> /// <returns>List of certificate information, or empty list if listing failed.</returns> public async Task<List<Certificate>> ListCertificatesAsync() { try { var request = new ListCertificatesRequest(); var response = await _amazonIoT.ListCertificatesAsync(request); _logger.LogInformation($"Retrieved {response.Certificates.Count} certificates"); return response.Certificates; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<Certificate>(); } catch (Exception ex) { _logger.LogError($"Couldn't list certificates. Here's why: {ex.Message}"); return new List<Certificate>(); } } /// <summary> /// Updates the Thing's shadow with new state information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="shadowPayload">The shadow payload in JSON format.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingShadowAsync(string thingName, string shadowPayload) { try { var request = new UpdateThingShadowRequest { ThingName = thingName, Payload = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(shadowPayload)) }; await _amazonIotData.UpdateThingShadowAsync(request); _logger.LogInformation($"Updated shadow for Thing {thingName}"); return true; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing shadow - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing shadow. Here's why: {ex.Message}"); return false; } } /// <summary> /// Gets the Thing's shadow information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <returns>The shadow data as a string, or null if retrieval failed.</returns> public async Task<string?> GetThingShadowAsync(string thingName) { try { var request = new GetThingShadowRequest { ThingName = thingName }; var response = await _amazonIotData.GetThingShadowAsync(request); using var reader = new StreamReader(response.Payload); var shadowData = await reader.ReadToEndAsync(); _logger.LogInformation($"Retrieved shadow for Thing {thingName}"); return shadowData; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot get Thing shadow - resource not found: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't get Thing shadow. Here's why: {ex.Message}"); return null; } } /// <summary> /// Creates an IoT topic rule. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="snsTopicArn">The ARN of the SNS topic for the action.</param> /// <param name="roleArn">The ARN of the IAM role.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> CreateTopicRuleAsync(string ruleName, string snsTopicArn, string roleArn) { try { var request = new CreateTopicRuleRequest { RuleName = ruleName, TopicRulePayload = new TopicRulePayload { Sql = "SELECT * FROM 'topic/subtopic'", Description = $"Rule created by .NET example: {ruleName}", Actions = new List<Amazon.IoT.Model.Action> { new Amazon.IoT.Model.Action { Sns = new SnsAction { TargetArn = snsTopicArn, RoleArn = roleArn } } }, RuleDisabled = false } }; await _amazonIoT.CreateTopicRuleAsync(request); _logger.LogInformation($"Created IoT rule {ruleName}"); return true; } catch (Amazon.IoT.Model.ResourceAlreadyExistsException ex) { _logger.LogWarning($"Rule {ruleName} already exists: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't create topic rule. Here's why: {ex.Message}"); return false; } } /// <summary> /// Deletes an IoT topic rule. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DeleteTopicRuleAsync(string ruleName) { try { var request = new DeleteTopicRuleRequest { RuleName = ruleName, }; await _amazonIoT.DeleteTopicRuleAsync(request); _logger.LogInformation($"Deleted IoT rule {ruleName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogWarning($"Rule {ruleName} not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't delete topic rule. Here's why: {ex.Message}"); return false; } } /// <summary> /// Lists all IoT topic rules. /// </summary> /// <returns>List of topic rules, or empty list if listing failed.</returns> public async Task<List<TopicRuleListItem>> ListTopicRulesAsync() { try { var request = new ListTopicRulesRequest(); var response = await _amazonIoT.ListTopicRulesAsync(request); _logger.LogInformation($"Retrieved {response.Rules.Count} IoT rules"); return response.Rules; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<TopicRuleListItem>(); } catch (Exception ex) { _logger.LogError($"Couldn't list topic rules. Here's why: {ex.Message}"); return new List<TopicRuleListItem>(); } } /// <summary> /// Searches for IoT Things using the search index. /// </summary> /// <param name="queryString">The search query string.</param> /// <returns>List of Things that match the search criteria, or empty list if search failed.</returns> public async Task<List<ThingDocument>> SearchIndexAsync(string queryString) { try { // First, try to perform the search var request = new SearchIndexRequest { QueryString = queryString }; var response = await _amazonIoT.SearchIndexAsync(request); _logger.LogInformation($"Search found {response.Things.Count} Things"); return response.Things; } catch (Amazon.IoT.Model.IndexNotReadyException ex) { _logger.LogWarning($"Search index not ready, setting up indexing configuration: {ex.Message}"); return await SetupIndexAndRetrySearchAsync(queryString); } catch (Amazon.IoT.Model.ResourceNotFoundException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index")) { _logger.LogWarning($"Search index not configured, setting up indexing configuration: {ex.Message}"); return await SetupIndexAndRetrySearchAsync(queryString); } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<ThingDocument>(); } catch (Exception ex) { _logger.LogError($"Couldn't search index. Here's why: {ex.Message}"); return new List<ThingDocument>(); } } /// <summary> /// Sets up the indexing configuration and retries the search after waiting for the index to be ready. /// </summary> /// <param name="queryString">The search query string.</param> /// <returns>List of Things that match the search criteria, or empty list if setup/search failed.</returns> private async Task<List<ThingDocument>> SetupIndexAndRetrySearchAsync(string queryString) { try { // Update indexing configuration to REGISTRY mode _logger.LogInformation("Setting up IoT search indexing configuration..."); await _amazonIoT.UpdateIndexingConfigurationAsync( new UpdateIndexingConfigurationRequest() { ThingIndexingConfiguration = new ThingIndexingConfiguration() { ThingIndexingMode = ThingIndexingMode.REGISTRY } }); _logger.LogInformation("Indexing configuration updated. Waiting for index to be ready..."); // Wait for the index to be set up - this can take some time const int maxRetries = 10; const int retryDelaySeconds = 10; for (int attempt = 1; attempt <= maxRetries; attempt++) { try { _logger.LogInformation($"Waiting for index to be ready (attempt {attempt}/{maxRetries})..."); await Task.Delay(TimeSpan.FromSeconds(retryDelaySeconds)); // Try to get the current indexing configuration to see if it's ready var configResponse = await _amazonIoT.GetIndexingConfigurationAsync(new GetIndexingConfigurationRequest()); if (configResponse.ThingIndexingConfiguration?.ThingIndexingMode == ThingIndexingMode.REGISTRY) { // Try the search again var request = new SearchIndexRequest { QueryString = queryString }; var response = await _amazonIoT.SearchIndexAsync(request); _logger.LogInformation($"Search found {response.Things.Count} Things after index setup"); return response.Things; } } catch (Amazon.IoT.Model.IndexNotReadyException) { // Index still not ready, continue waiting _logger.LogInformation("Index still not ready, continuing to wait..."); continue; } catch (Amazon.IoT.Model.InvalidRequestException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index")) { // Index still not ready, continue waiting _logger.LogInformation("Index still not ready, continuing to wait..."); continue; } } _logger.LogWarning("Timeout waiting for search index to be ready after configuration update"); return new List<ThingDocument>(); } catch (Exception ex) { _logger.LogError($"Couldn't set up search index configuration. Here's why: {ex.Message}"); return new List<ThingDocument>(); } } /// <summary> /// Detaches a certificate from an IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="certificateArn">The ARN of the certificate to detach.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DetachThingPrincipalAsync(string thingName, string certificateArn) { try { var request = new DetachThingPrincipalRequest { ThingName = thingName, Principal = certificateArn }; await _amazonIoT.DetachThingPrincipalAsync(request); _logger.LogInformation($"Detached certificate {certificateArn} from Thing {thingName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot detach certificate - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't detach certificate from Thing. Here's why: {ex.Message}"); return false; } } /// <summary> /// Deletes an IoT certificate. /// </summary> /// <param name="certificateId">The ID of the certificate to delete.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DeleteCertificateAsync(string certificateId) { try { // First, update the certificate to inactive state var updateRequest = new UpdateCertificateRequest { CertificateId = certificateId, NewStatus = CertificateStatus.INACTIVE }; await _amazonIoT.UpdateCertificateAsync(updateRequest); // Then delete the certificate var deleteRequest = new DeleteCertificateRequest { CertificateId = certificateId }; await _amazonIoT.DeleteCertificateAsync(deleteRequest); _logger.LogInformation($"Deleted certificate {certificateId}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot delete certificate - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't delete certificate. Here's why: {ex.Message}"); return false; } } /// <summary> /// Deletes an IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing to delete.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DeleteThingAsync(string thingName) { try { var request = new DeleteThingRequest { ThingName = thingName }; await _amazonIoT.DeleteThingAsync(request); _logger.LogInformation($"Deleted Thing {thingName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot delete Thing - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't delete Thing. Here's why: {ex.Message}"); return false; } } /// <summary> /// Lists IoT Things with pagination support. /// </summary> /// <returns>List of Things, or empty list if listing failed.</returns> public async Task<List<ThingAttribute>> ListThingsAsync() { try { // Use pages of 10. var request = new ListThingsRequest() { MaxResults = 10 }; var response = await _amazonIoT.ListThingsAsync(request); // Since there is not a built-in paginator, use the NextMarker to paginate. bool hasMoreResults = true; var things = new List<ThingAttribute>(); while (hasMoreResults) { things.AddRange(response.Things); // If NextMarker is not null, there are more results. Get the next page of results. if (!String.IsNullOrEmpty(response.NextMarker)) { request.Marker = response.NextMarker; response = await _amazonIoT.ListThingsAsync(request); } else hasMoreResults = false; } _logger.LogInformation($"Retrieved {things.Count} Things"); return things; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<ThingAttribute>(); } catch (Exception ex) { _logger.LogError($"Couldn't list Things. Here's why: {ex.Message}"); return new List<ThingAttribute>(); } } }
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create an AWS IoT thing.
Aws::String thingName = askQuestion("Enter a thing name: "); if (!createThing(thingName, clientConfiguration)) { std::cerr << "Exiting because createThing failed." << std::endl; cleanup("", "", "", "", "", false, clientConfiguration); return false; }//! Create an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateThingRequest createThingRequest; createThingRequest.SetThingName(thingName); Aws::IoT::Model::CreateThingOutcome outcome = iotClient.CreateThing( createThingRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created thing " << thingName << std::endl; } else { std::cerr << "Failed to create thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }Generate and attach a device certificate.
Aws::String certificateARN; Aws::String certificateID; if (askYesNoQuestion("Would you like to create a certificate for your thing? (y/n) ")) { Aws::String outputFolder; if (askYesNoQuestion( "Would you like to save the certificate and keys to file? (y/n) ")) { outputFolder = std::filesystem::current_path(); outputFolder += "/device_keys_and_certificates"; std::filesystem::create_directories(outputFolder); std::cout << "The certificate and keys will be saved to the folder: " << outputFolder << std::endl; } if (!createKeysAndCertificate(outputFolder, certificateARN, certificateID, clientConfiguration)) { std::cerr << "Exiting because createKeysAndCertificate failed." << std::endl; cleanup(thingName, "", "", "", "", false, clientConfiguration); return false; } std::cout << "\nNext, the certificate will be attached to the thing.\n" << std::endl; if (!attachThingPrincipal(certificateARN, thingName, clientConfiguration)) { std::cerr << "Exiting because attachThingPrincipal failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } }//! Create keys and certificate for an Aws IoT device. //! This routine will save certificates and keys to an output folder, if provided. /*! \param outputFolder: Location for storing output in files, ignored when string is empty. \param certificateARNResult: A string to receive the ARN of the created certificate. \param certificateID: A string to receive the ID of the created certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createKeysAndCertificate(const Aws::String &outputFolder, Aws::String &certificateARNResult, Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::CreateKeysAndCertificateRequest createKeysAndCertificateRequest; Aws::IoT::Model::CreateKeysAndCertificateOutcome outcome = client.CreateKeysAndCertificate(createKeysAndCertificateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created a certificate and keys" << std::endl; certificateARNResult = outcome.GetResult().GetCertificateArn(); certificateID = outcome.GetResult().GetCertificateId(); std::cout << "Certificate ARN: " << certificateARNResult << ", certificate ID: " << certificateID << std::endl; if (!outputFolder.empty()) { std::cout << "Writing certificate and keys to the folder '" << outputFolder << "'." << std::endl; std::cout << "Be sure these files are stored securely." << std::endl; Aws::String certificateFilePath = outputFolder + "/certificate.pem.crt"; std::ofstream certificateFile(certificateFilePath); if (!certificateFile.is_open()) { std::cerr << "Error opening certificate file, '" << certificateFilePath << "'." << std::endl; return false; } certificateFile << outcome.GetResult().GetCertificatePem(); certificateFile.close(); const Aws::IoT::Model::KeyPair &keyPair = outcome.GetResult().GetKeyPair(); Aws::String privateKeyFilePath = outputFolder + "/private.pem.key"; std::ofstream privateKeyFile(privateKeyFilePath); if (!privateKeyFile.is_open()) { std::cerr << "Error opening private key file, '" << privateKeyFilePath << "'." << std::endl; return false; } privateKeyFile << keyPair.GetPrivateKey(); privateKeyFile.close(); Aws::String publicKeyFilePath = outputFolder + "/public.pem.key"; std::ofstream publicKeyFile(publicKeyFilePath); if (!publicKeyFile.is_open()) { std::cerr << "Error opening public key file, '" << publicKeyFilePath << "'." << std::endl; return false; } publicKeyFile << keyPair.GetPublicKey(); } } else { std::cerr << "Error creating keys and certificate: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Attach a principal to an AWS IoT thing. /*! \param principal: A principal to attach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::attachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::AttachThingPrincipalRequest request; request.SetPrincipal(principal); request.SetThingName(thingName); Aws::IoT::Model::AttachThingPrincipalOutcome outcome = client.AttachThingPrincipal( request); if (outcome.IsSuccess()) { std::cout << "Successfully attached principal to thing." << std::endl; } else { std::cerr << "Failed to attach principal to thing." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }Perform various operations on the AWS IoT thing.
if (!updateThing(thingName, { {"location", "Office"}, {"firmwareVersion", "v2.0"} }, clientConfiguration)) { std::cerr << "Exiting because updateThing failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now an endpoint will be retrieved for your account.\n" << std::endl; std::cout << "An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point\n" << "for communication between IoT devices and the AWS IoT service." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); Aws::String endpoint; if (!describeEndpoint(endpoint, clientConfiguration)) { std::cerr << "Exiting because getEndpoint failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } std::cout <<"Your endpoint is " << endpoint << "." << std::endl; printAsterisksLine(); std::cout << "Now the certificates in your account will be listed." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); if (!listCertificates(clientConfiguration)) { std::cerr << "Exiting because listCertificates failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now the shadow for the thing will be updated.\n" << std::endl; std::cout << "A thing shadow refers to a feature that enables you to create a virtual representation, or \"shadow,\"\n" << "of a physical device or thing. The thing shadow allows you to synchronize and control the state of a device between\n" << "the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a thing shadow." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); if (!updateThingShadow(thingName, R"({"state":{"reported":{"temperature":25,"humidity":50}}})", clientConfiguration)) { std::cerr << "Exiting because updateThingShadow failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now, the state information for the shadow will be retrieved.\n" << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); Aws::String shadowState; if (!getThingShadow(thingName, shadowState, clientConfiguration)) { std::cerr << "Exiting because getThingShadow failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } std::cout << "The retrieved shadow state is: " << shadowState << std::endl; printAsterisksLine(); std::cout << "A rule with now be added to to the thing.\n" << std::endl; std::cout << "Any user who has permission to create rules will be able to access data processed by the rule." << std::endl; std::cout << "In this case, the rule will use an Simple Notification Service (SNS) topic and an IAM rule." << std::endl; std::cout << "These resources will be created using a CloudFormation template." << std::endl; std::cout << "Stack creation may take a few minutes." << std::endl; askQuestion("Press Enter to continue: ", alwaysTrueTest); Aws::Map<Aws::String, Aws::String> outputs =createCloudFormationStack(STACK_NAME,clientConfiguration); if (outputs.empty()) { std::cerr << "Exiting because createCloudFormationStack failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } // Retrieve the topic ARN and role ARN from the CloudFormation stack outputs. auto topicArnIter = outputs.find(SNS_TOPIC_ARN_OUTPUT); auto roleArnIter = outputs.find(ROLE_ARN_OUTPUT); if ((topicArnIter == outputs.end()) || (roleArnIter == outputs.end())) { std::cerr << "Exiting because output '" << SNS_TOPIC_ARN_OUTPUT << "' or '" << ROLE_ARN_OUTPUT << "'not found in the CloudFormation stack." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, "", false, clientConfiguration); return false; } Aws::String topicArn = topicArnIter->second; Aws::String roleArn = roleArnIter->second; Aws::String sqlStatement = "SELECT * FROM '"; sqlStatement += MQTT_MESSAGE_TOPIC_FILTER; sqlStatement += "'"; printAsterisksLine(); std::cout << "Now a rule will be created.\n" << std::endl; std::cout << "Rules are an administrator-level action. Any user who has permission\n" << "to create rules will be able to access data processed by the rule." << std::endl; std::cout << "In this case, the rule will use an SNS topic" << std::endl; std::cout << "and the following SQL statement '" << sqlStatement << "'." << std::endl; std::cout << "For more information on IoT SQL, see https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-reference.html" << std::endl; Aws::String ruleName = askQuestion("Enter a rule name: "); if (!createTopicRule(ruleName, topicArn, sqlStatement, roleArn, clientConfiguration)) { std::cerr << "Exiting because createRule failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now your rules will be listed.\n" << std::endl; askQuestion("Press Enter to continue: ", alwaysTrueTest); if (!listTopicRules(clientConfiguration)) { std::cerr << "Exiting because listRules failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; } printAsterisksLine(); Aws::String queryString = "thingName:" + thingName; std::cout << "Now the AWS IoT fleet index will be queried with the query\n'" << queryString << "'.\n" << std::endl; std::cout << "For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html" << std::endl; std::cout << "For this query to work, thing indexing must be enabled in your account.\n" << "This can be done with the awscli command line by calling 'aws iot update-indexing-configuration'\n" << "or it can be done programmatically." << std::endl; std::cout << "For more information, see https://docs.aws.amazon.com/iot/latest/developerguide/managing-index.html" << std::endl; if (askYesNoQuestion("Do you want to enable thing indexing in your account? (y/n) ")) { Aws::IoT::Model::ThingIndexingConfiguration thingIndexingConfiguration; thingIndexingConfiguration.SetThingIndexingMode(Aws::IoT::Model::ThingIndexingMode::REGISTRY_AND_SHADOW); thingIndexingConfiguration.SetThingConnectivityIndexingMode(Aws::IoT::Model::ThingConnectivityIndexingMode::STATUS); // The ThingGroupIndexingConfiguration object is ignored if not set. Aws::IoT::Model::ThingGroupIndexingConfiguration thingGroupIndexingConfiguration; if (!updateIndexingConfiguration(thingIndexingConfiguration, thingGroupIndexingConfiguration, clientConfiguration)) { std::cerr << "Exiting because updateIndexingConfiguration failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; } } if (!searchIndex(queryString, clientConfiguration)) { std::cerr << "Exiting because searchIndex failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; }//! Update an AWS IoT thing with attributes. /*! \param thingName: The name for the thing. \param attributeMap: A map of key/value attributes/ \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThing(const Aws::String &thingName, const std::map<Aws::String, Aws::String> &attributeMap, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::UpdateThingRequest request; request.SetThingName(thingName); Aws::IoT::Model::AttributePayload attributePayload; for (const auto &attribute: attributeMap) { attributePayload.AddAttributes(attribute.first, attribute.second); } request.SetAttributePayload(attributePayload); Aws::IoT::Model::UpdateThingOutcome outcome = iotClient.UpdateThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing " << thingName << std::endl; } else { std::cerr << "Failed to update thing " << thingName << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Describe the endpoint specific to the AWS account making the call. /*! \param endpointResult: String to receive the endpoint result. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeEndpoint(Aws::String &endpointResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::String endpoint; Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeEndpointRequest describeEndpointRequest; describeEndpointRequest.SetEndpointType( "iot:Data-ATS"); // Recommended endpoint type. Aws::IoT::Model::DescribeEndpointOutcome outcome = iotClient.DescribeEndpoint( describeEndpointRequest); if (outcome.IsSuccess()) { std::cout << "Successfully described endpoint." << std::endl; endpointResult = outcome.GetResult().GetEndpointAddress(); } else { std::cerr << "Error describing endpoint" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! List certificates registered in the AWS account making the call. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listCertificates( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListCertificatesRequest request; Aws::Vector<Aws::IoT::Model::Certificate> allCertificates; Aws::String marker; // Used to paginate results. do { if (!marker.empty()) { request.SetMarker(marker); } Aws::IoT::Model::ListCertificatesOutcome outcome = iotClient.ListCertificates( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListCertificatesResult &result = outcome.GetResult(); marker = result.GetNextMarker(); allCertificates.insert(allCertificates.end(), result.GetCertificates().begin(), result.GetCertificates().end()); } else { std::cerr << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << allCertificates.size() << " certificate(s) found." << std::endl; for (auto &certificate: allCertificates) { std::cout << "Certificate ID: " << certificate.GetCertificateId() << std::endl; std::cout << "Certificate ARN: " << certificate.GetCertificateArn() << std::endl; std::cout << std::endl; } return true; } //! Update the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param document: The state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThingShadow(const Aws::String &thingName, const Aws::String &document, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotDataPlaneClient(clientConfiguration); Aws::IoTDataPlane::Model::UpdateThingShadowRequest updateThingShadowRequest; updateThingShadowRequest.SetThingName(thingName); std::shared_ptr<std::stringstream> streamBuf = std::make_shared<std::stringstream>( document); updateThingShadowRequest.SetBody(streamBuf); Aws::IoTDataPlane::Model::UpdateThingShadowOutcome outcome = iotDataPlaneClient.UpdateThingShadow( updateThingShadowRequest); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing shadow." << std::endl; } else { std::cerr << "Error while updating thing shadow." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Get the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param documentResult: String to receive the state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::getThingShadow(const Aws::String &thingName, Aws::String &documentResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotClient(clientConfiguration); Aws::IoTDataPlane::Model::GetThingShadowRequest request; request.SetThingName(thingName); auto outcome = iotClient.GetThingShadow(request); if (outcome.IsSuccess()) { std::stringstream ss; ss << outcome.GetResult().GetPayload().rdbuf(); documentResult = ss.str(); } else { std::cerr << "Error getting thing shadow: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Lists the AWS IoT topic rules. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listTopicRules( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListTopicRulesRequest request; Aws::Vector<Aws::IoT::Model::TopicRuleListItem> allRules; Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::IoT::Model::ListTopicRulesOutcome outcome = iotClient.ListTopicRules( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListTopicRulesResult &result = outcome.GetResult(); allRules.insert(allRules.end(), result.GetRules().cbegin(), result.GetRules().cend()); nextToken = result.GetNextToken(); } else { std::cerr << "ListTopicRules error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << "ListTopicRules: " << allRules.size() << " rule(s) found." << std::endl; for (auto &rule: allRules) { std::cout << " Rule name: " << rule.GetRuleName() << ", rule ARN: " << rule.GetRuleArn() << "." << std::endl; } return true; } //! Query the AWS IoT fleet index. //! For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html /*! \param: query: The query string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::searchIndex(const Aws::String &query, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::SearchIndexRequest request; request.SetQueryString(query); Aws::Vector<Aws::IoT::Model::ThingDocument> allThingDocuments; Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::IoT::Model::SearchIndexOutcome outcome = iotClient.SearchIndex(request); if (outcome.IsSuccess()) { const Aws::IoT::Model::SearchIndexResult &result = outcome.GetResult(); allThingDocuments.insert(allThingDocuments.end(), result.GetThings().cbegin(), result.GetThings().cend()); nextToken = result.GetNextToken(); } else { std::cerr << "Error in SearchIndex: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << allThingDocuments.size() << " thing document(s) found." << std::endl; for (const auto thingDocument: allThingDocuments) { std::cout << " Thing name: " << thingDocument.GetThingName() << "." << std::endl; } return true; }Clean up resources.
bool AwsDoc::IoT::cleanup(const Aws::String &thingName, const Aws::String &certificateARN, const Aws::String &certificateID, const Aws::String &stackName, const Aws::String &ruleName, bool askForConfirmation, const Aws::Client::ClientConfiguration &clientConfiguration) { bool result = true; if (!ruleName.empty() && (!askForConfirmation || askYesNoQuestion("Delete the rule '" + ruleName + "'? (y/n) "))) { result &= deleteTopicRule(ruleName, clientConfiguration); } Aws::CloudFormation::CloudFormationClient cloudFormationClient(clientConfiguration); if (!stackName.empty() && (!askForConfirmation || askYesNoQuestion( "Delete the CloudFormation stack '" + stackName + "'? (y/n) "))) { result &= deleteStack(stackName, clientConfiguration); } if (!certificateARN.empty() && (!askForConfirmation || askYesNoQuestion("Delete the certificate '" + certificateARN + "'? (y/n) "))) { result &= detachThingPrincipal(certificateARN, thingName, clientConfiguration); result &= deleteCertificate(certificateID, clientConfiguration); } if (!thingName.empty() && (!askForConfirmation || askYesNoQuestion("Delete the thing '" + thingName + "'? (y/n) "))) { result &= deleteThing(thingName, clientConfiguration); } return result; }//! Detach a principal from an AWS IoT thing. /*! \param principal: A principal to detach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::detachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DetachThingPrincipalRequest detachThingPrincipalRequest; detachThingPrincipalRequest.SetThingName(thingName); detachThingPrincipalRequest.SetPrincipal(principal); Aws::IoT::Model::DetachThingPrincipalOutcome outcome = iotClient.DetachThingPrincipal( detachThingPrincipalRequest); if (outcome.IsSuccess()) { std::cout << "Successfully detached principal " << principal << " from thing " << thingName << std::endl; } else { std::cerr << "Failed to detach principal " << principal << " from thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete a certificate. /*! \param certificateID: The ID of a certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteCertificate(const Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteCertificateRequest request; request.SetCertificateId(certificateID); Aws::IoT::Model::DeleteCertificateOutcome outcome = iotClient.DeleteCertificate( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted certificate " << certificateID << std::endl; } else { std::cerr << "Error deleting certificate " << certificateID << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete an AWS IoT rule. /*! \param ruleName: The name for the rule. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteTopicRule(const Aws::String &ruleName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::DeleteTopicRuleOutcome outcome = iotClient.DeleteTopicRule( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted rule " << ruleName << std::endl; } else { std::cerr << "Failed to delete rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteThingRequest request; request.SetThingName(thingName); const auto outcome = iotClient.DeleteThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted thing " << thingName << std::endl; } else { std::cerr << "Error deleting thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Run an interactive scenario demonstrating AWS IoT features.
import java.util.Scanner; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * This Java example performs these tasks: * * 1. Creates an AWS IoT Thing. * 2. Generate and attach a device certificate. * 3. Update an AWS IoT Thing with Attributes. * 4. Get an AWS IoT Endpoint. * 5. List your certificates. * 6. Updates the shadow for the specified thing.. * 7. Write out the state information, in JSON format * 8. Creates a rule * 9. List rules * 10. Search things * 11. Detach amd delete the certificate. * 12. Delete Thing. */ public class IotScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) { final String usage = """ Usage: <roleARN> <snsAction> Where: roleARN - The ARN of an IAM role that has permission to work with AWS IOT. snsAction - An ARN of an SNS topic. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } IotActions iotActions = new IotActions(); String thingName; String ruleName; String roleARN = args[0]; String snsAction = args[1]; Scanner scanner = new Scanner(System.in); System.out.println(DASHES); System.out.println("Welcome to the AWS IoT basics scenario."); System.out.println(""" This example program demonstrates various interactions with the AWS Internet of Things (IoT) Core service. The program guides you through a series of steps, including creating an IoT Thing, generating a device certificate, updating the Thing with attributes, and so on. It utilizes the AWS SDK for Java V2 and incorporates functionality for creating and managing IoT Things, certificates, rules, shadows, and performing searches. The program aims to showcase AWS IoT capabilities and provides a comprehensive example for developers working with AWS IoT in a Java environment. Let's get started... """); System.out.println(DASHES); System.out.println("1. Create an AWS IoT Thing."); System.out.println(""" An AWS IoT Thing represents a virtual entity in the AWS IoT service that can be associated with a physical device. """); // Prompt the user for input. System.out.print("Enter Thing name: "); thingName = scanner.nextLine(); iotActions.createIoTThing(thingName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. Generate a device certificate."); System.out.println(""" A device certificate performs a role in securing the communication between devices (Things) and the AWS IoT platform. """); System.out.print("Do you want to create a certificate for " +thingName +"? (y/n)"); String certAns = scanner.nextLine(); String certificateArn="" ; if (certAns != null && certAns.trim().equalsIgnoreCase("y")) { certificateArn = iotActions.createCertificate(); System.out.println("Attach the certificate to the AWS IoT Thing."); iotActions.attachCertificateToThing(thingName, certificateArn); } else { System.out.println("A device certificate was not created."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Update an AWS IoT Thing with Attributes."); System.out.println(""" IoT Thing attributes, represented as key-value pairs, offer a pivotal advantage in facilitating efficient data management and retrieval within the AWS IoT ecosystem. """); waitForInputToContinue(scanner); iotActions.updateShadowThing(thingName); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Return a unique endpoint specific to the Amazon Web Services account."); System.out.println(""" An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point for communication between IoT devices and the AWS IoT service. """); waitForInputToContinue(scanner); String endpointUrl = iotActions.describeEndpoint(); System.out.println("The endpoint is "+endpointUrl); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. List your AWS IoT certificates"); waitForInputToContinue(scanner); if (certificateArn.length() > 0) { iotActions.listCertificates(); } else { System.out.println("You did not create a certificates. Skipping this step."); } waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Create an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device"); System.out.println(""" A Thing Shadow refers to a feature that enables you to create a virtual representation, or "shadow," of a physical device or thing. The Thing Shadow allows you to synchronize and control the state of a device between the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a Thing Shadow. """); waitForInputToContinue(scanner); iotActions.updateShadowThing(thingName); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Write out the state information, in JSON format."); waitForInputToContinue(scanner); iotActions.getPayload(thingName); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("8. Creates a rule"); System.out.println(""" Creates a rule that is an administrator-level action. Any user who has permission to create rules will be able to access data processed by the rule. """); System.out.print("Enter Rule name: "); ruleName = scanner.nextLine(); iotActions.createIoTRule(roleARN, ruleName, snsAction); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("9. List your rules."); waitForInputToContinue(scanner); iotActions.listIoTRules(); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Search things using the Thing name."); waitForInputToContinue(scanner); String queryString = "thingName:"+thingName ; iotActions.searchThings(queryString); waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println(DASHES); if (certificateArn.length() > 0) { System.out.print("Do you want to detach and delete the certificate for " +thingName +"? (y/n)"); String delAns = scanner.nextLine(); if (delAns != null && delAns.trim().equalsIgnoreCase("y")) { System.out.println("11. You selected to detach amd delete the certificate."); waitForInputToContinue(scanner); iotActions.detachThingPrincipal(thingName, certificateArn); iotActions.deleteCertificate(certificateArn); waitForInputToContinue(scanner); } else { System.out.println("11. You selected not to delete the certificate."); } } else { System.out.println("11. You did not create a certificate so there is nothing to delete."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Delete the AWS IoT Thing."); System.out.print("Do you want to delete the IoT Thing? (y/n)"); String delAns = scanner.nextLine(); if (delAns != null && delAns.trim().equalsIgnoreCase("y")) { iotActions.deleteIoTThing(thingName); } else { System.out.println("The IoT Thing was not deleted."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("The AWS IoT workflow has successfully completed."); System.out.println(DASHES); } private static void waitForInputToContinue(Scanner scanner) { while (true) { System.out.println(""); System.out.println("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { System.out.println("Continuing with the program..."); System.out.println(""); break; } else { // Handle invalid input. System.out.println("Invalid input. Please try again."); } } } }A wrapper class for AWS IoT SDK methods.
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iot.IotAsyncClient; import software.amazon.awssdk.services.iot.model.Action; import software.amazon.awssdk.services.iot.model.AttachThingPrincipalRequest; import software.amazon.awssdk.services.iot.model.AttachThingPrincipalResponse; import software.amazon.awssdk.services.iot.model.Certificate; import software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse; import software.amazon.awssdk.services.iot.model.CreateThingRequest; import software.amazon.awssdk.services.iot.model.CreateThingResponse; import software.amazon.awssdk.services.iot.model.CreateTopicRuleRequest; import software.amazon.awssdk.services.iot.model.CreateTopicRuleResponse; import software.amazon.awssdk.services.iot.model.DeleteCertificateRequest; import software.amazon.awssdk.services.iot.model.DeleteCertificateResponse; import software.amazon.awssdk.services.iot.model.DeleteThingRequest; import software.amazon.awssdk.services.iot.model.DeleteThingResponse; import software.amazon.awssdk.services.iot.model.DescribeEndpointRequest; import software.amazon.awssdk.services.iot.model.DescribeEndpointResponse; import software.amazon.awssdk.services.iot.model.DescribeThingRequest; import software.amazon.awssdk.services.iot.model.DescribeThingResponse; import software.amazon.awssdk.services.iot.model.DetachThingPrincipalRequest; import software.amazon.awssdk.services.iot.model.DetachThingPrincipalResponse; import software.amazon.awssdk.services.iot.model.IotException; import software.amazon.awssdk.services.iot.model.ListCertificatesResponse; import software.amazon.awssdk.services.iot.model.ListTopicRulesRequest; import software.amazon.awssdk.services.iot.model.ListTopicRulesResponse; import software.amazon.awssdk.services.iot.model.SearchIndexRequest; import software.amazon.awssdk.services.iot.model.SearchIndexResponse; import software.amazon.awssdk.services.iot.model.TopicRuleListItem; import software.amazon.awssdk.services.iot.model.SnsAction; import software.amazon.awssdk.services.iot.model.TopicRulePayload; import software.amazon.awssdk.services.iotdataplane.IotDataPlaneAsyncClient; import software.amazon.awssdk.services.iotdataplane.model.GetThingShadowRequest; import software.amazon.awssdk.services.iotdataplane.model.GetThingShadowResponse; import software.amazon.awssdk.services.iotdataplane.model.UpdateThingShadowRequest; import software.amazon.awssdk.services.iotdataplane.model.UpdateThingShadowResponse; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class IotActions { private static IotAsyncClient iotAsyncClient; private static IotDataPlaneAsyncClient iotAsyncDataPlaneClient; private static final String TOPIC = "your-iot-topic"; private static IotDataPlaneAsyncClient getAsyncDataPlaneClient() { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryPolicy(RetryPolicy.builder() .numRetries(3) .build()) .build(); if (iotAsyncDataPlaneClient == null) { iotAsyncDataPlaneClient = IotDataPlaneAsyncClient.builder() .region(Region.US_EAST_1) .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return iotAsyncDataPlaneClient; } private static IotAsyncClient getAsyncClient() { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryPolicy(RetryPolicy.builder() .numRetries(3) .build()) .build(); if (iotAsyncClient == null) { iotAsyncClient = IotAsyncClient.builder() .region(Region.US_EAST_1) .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return iotAsyncClient; } /** * Creates an IoT certificate asynchronously. * * @return The ARN of the created certificate. * <p> * This method initiates an asynchronous request to create an IoT certificate. * If the request is successful, it prints the certificate details and returns the certificate ARN. * If an exception occurs, it prints the error message. */ public String createCertificate() { CompletableFuture<CreateKeysAndCertificateResponse> future = getAsyncClient().createKeysAndCertificate(); final String[] certificateArn = {null}; future.whenComplete((response, ex) -> { if (response != null) { String certificatePem = response.certificatePem(); certificateArn[0] = response.certificateArn(); // Print the details. System.out.println("\nCertificate:"); System.out.println(certificatePem); System.out.println("\nCertificate ARN:"); System.out.println(certificateArn[0]); } else { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); return certificateArn[0]; } /** * Creates an IoT Thing with the specified name asynchronously. * * @param thingName The name of the IoT Thing to create. * * This method initiates an asynchronous request to create an IoT Thing with the specified name. * If the request is successful, it prints the name of the thing and its ARN value. * If an exception occurs, it prints the error message. */ public void createIoTThing(String thingName) { CreateThingRequest createThingRequest = CreateThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<CreateThingResponse> future = getAsyncClient().createThing(createThingRequest); future.whenComplete((createThingResponse, ex) -> { if (createThingResponse != null) { System.out.println(thingName + " was successfully created. The ARN value is " + createThingResponse.thingArn()); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); } /** * Attaches a certificate to an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * @param certificateArn The ARN of the certificate to attach. * * This method initiates an asynchronous request to attach a certificate to an IoT Thing. * If the request is successful, it prints a confirmation message and additional information about the Thing. * If an exception occurs, it prints the error message. */ public void attachCertificateToThing(String thingName, String certificateArn) { AttachThingPrincipalRequest principalRequest = AttachThingPrincipalRequest.builder() .thingName(thingName) .principal(certificateArn) .build(); CompletableFuture<AttachThingPrincipalResponse> future = getAsyncClient().attachThingPrincipal(principalRequest); future.whenComplete((attachResponse, ex) -> { if (attachResponse != null && attachResponse.sdkHttpResponse().isSuccessful()) { System.out.println("Certificate attached to Thing successfully."); // Print additional information about the Thing. describeThing(thingName); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to attach certificate to Thing. HTTP Status Code: " + attachResponse.sdkHttpResponse().statusCode()); } } }); future.join(); } /** * Describes an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to describe an IoT Thing. * If the request is successful, it prints the Thing details. * If an exception occurs, it prints the error message. */ private void describeThing(String thingName) { DescribeThingRequest thingRequest = DescribeThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<DescribeThingResponse> future = getAsyncClient().describeThing(thingRequest); future.whenComplete((describeResponse, ex) -> { if (describeResponse != null) { System.out.println("Thing Details:"); System.out.println("Thing Name: " + describeResponse.thingName()); System.out.println("Thing ARN: " + describeResponse.thingArn()); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to describe Thing."); } } }); future.join(); } /** * Updates the shadow of an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to update the shadow of an IoT Thing. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void updateShadowThing(String thingName) { // Create Thing Shadow State Document. String stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}"; SdkBytes data = SdkBytes.fromString(stateDocument, StandardCharsets.UTF_8); UpdateThingShadowRequest updateThingShadowRequest = UpdateThingShadowRequest.builder() .thingName(thingName) .payload(data) .build(); CompletableFuture<UpdateThingShadowResponse> future = getAsyncDataPlaneClient().updateThingShadow(updateThingShadowRequest); future.whenComplete((updateResponse, ex) -> { if (updateResponse != null) { System.out.println("Thing Shadow updated successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to update Thing Shadow."); } } }); future.join(); } /** * Describes the endpoint of the IoT service asynchronously. * * @return A CompletableFuture containing the full endpoint URL. * * This method initiates an asynchronous request to describe the endpoint of the IoT service. * If the request is successful, it prints and returns the full endpoint URL. * If an exception occurs, it prints the error message. */ public String describeEndpoint() { CompletableFuture<DescribeEndpointResponse> future = getAsyncClient().describeEndpoint(DescribeEndpointRequest.builder().endpointType("iot:Data-ATS").build()); final String[] result = {null}; future.whenComplete((endpointResponse, ex) -> { if (endpointResponse != null) { String endpointUrl = endpointResponse.endpointAddress(); String exString = getValue(endpointUrl); String fullEndpoint = "https://" + exString + "-ats.iot.us-east-1.amazonaws.com"; System.out.println("Full Endpoint URL: " + fullEndpoint); result[0] = fullEndpoint; } else { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); return result[0]; } /** * Extracts a specific value from the endpoint URL. * * @param input The endpoint URL to process. * @return The extracted value from the endpoint URL. */ private static String getValue(String input) { // Define a regular expression pattern for extracting the subdomain. Pattern pattern = Pattern.compile("^(.*?)\\.iot\\.us-east-1\\.amazonaws\\.com"); // Match the pattern against the input string. Matcher matcher = pattern.matcher(input); // Check if a match is found. if (matcher.find()) { // Extract the subdomain from the first capturing group. String subdomain = matcher.group(1); System.out.println("Extracted subdomain: " + subdomain); return subdomain ; } else { System.out.println("No match found"); } return "" ; } /** * Lists all certificates asynchronously. * * This method initiates an asynchronous request to list all certificates. * If the request is successful, it prints the certificate IDs and ARNs. * If an exception occurs, it prints the error message. */ public void listCertificates() { CompletableFuture<ListCertificatesResponse> future = getAsyncClient().listCertificates(); future.whenComplete((response, ex) -> { if (response != null) { List<Certificate> certList = response.certificates(); for (Certificate cert : certList) { System.out.println("Cert id: " + cert.certificateId()); System.out.println("Cert Arn: " + cert.certificateArn()); } } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to list certificates."); } } }); future.join(); } /** * Retrieves the payload of a Thing's shadow asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to get the payload of a Thing's shadow. * If the request is successful, it prints the shadow data. * If an exception occurs, it prints the error message. */ public void getPayload(String thingName) { GetThingShadowRequest getThingShadowRequest = GetThingShadowRequest.builder() .thingName(thingName) .build(); CompletableFuture<GetThingShadowResponse> future = getAsyncDataPlaneClient().getThingShadow(getThingShadowRequest); future.whenComplete((getThingShadowResponse, ex) -> { if (getThingShadowResponse != null) { // Extracting payload from response. SdkBytes payload = getThingShadowResponse.payload(); String payloadString = payload.asUtf8String(); System.out.println("Received Shadow Data: " + payloadString); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to get Thing Shadow payload."); } } }); future.join(); } /** * Creates an IoT rule asynchronously. * * @param roleARN The ARN of the IAM role that grants access to the rule's actions. * @param ruleName The name of the IoT rule. * @param action The ARN of the action to perform when the rule is triggered. * * This method initiates an asynchronous request to create an IoT rule. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void createIoTRule(String roleARN, String ruleName, String action) { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); CompletableFuture<CreateTopicRuleResponse> future = getAsyncClient().createTopicRule(topicRuleRequest); future.whenComplete((response, ex) -> { if (response != null) { System.out.println("IoT Rule created successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to create IoT Rule."); } } }); future.join(); } /** * Lists IoT rules asynchronously. * * This method initiates an asynchronous request to list IoT rules. * If the request is successful, it prints the names and ARNs of the rules. * If an exception occurs, it prints the error message. */ public void listIoTRules() { ListTopicRulesRequest listTopicRulesRequest = ListTopicRulesRequest.builder().build(); CompletableFuture<ListTopicRulesResponse> future = getAsyncClient().listTopicRules(listTopicRulesRequest); future.whenComplete((listTopicRulesResponse, ex) -> { if (listTopicRulesResponse != null) { System.out.println("List of IoT Rules:"); List<TopicRuleListItem> ruleList = listTopicRulesResponse.rules(); for (TopicRuleListItem rule : ruleList) { System.out.println("Rule Name: " + rule.ruleName()); System.out.println("Rule ARN: " + rule.ruleArn()); System.out.println("--------------"); } } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to list IoT Rules."); } } }); future.join(); } /** * Searches for IoT Things asynchronously based on a query string. * * @param queryString The query string to search for Things. * * This method initiates an asynchronous request to search for IoT Things. * If the request is successful and Things are found, it prints their IDs. * If no Things are found, it prints a message indicating so. * If an exception occurs, it prints the error message. */ public void searchThings(String queryString) { SearchIndexRequest searchIndexRequest = SearchIndexRequest.builder() .queryString(queryString) .build(); CompletableFuture<SearchIndexResponse> future = getAsyncClient().searchIndex(searchIndexRequest); future.whenComplete((searchIndexResponse, ex) -> { if (searchIndexResponse != null) { // Process the result. if (searchIndexResponse.things().isEmpty()) { System.out.println("No things found."); } else { searchIndexResponse.things().forEach(thing -> System.out.println("Thing id found using search is " + thing.thingId())); } } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to search for IoT Things."); } } }); future.join(); } /** * Detaches a principal (certificate) from an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * @param certificateArn The ARN of the certificate to detach. * * This method initiates an asynchronous request to detach a certificate from an IoT Thing. * If the detachment is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void detachThingPrincipal(String thingName, String certificateArn) { DetachThingPrincipalRequest thingPrincipalRequest = DetachThingPrincipalRequest.builder() .principal(certificateArn) .thingName(thingName) .build(); CompletableFuture<DetachThingPrincipalResponse> future = getAsyncClient().detachThingPrincipal(thingPrincipalRequest); future.whenComplete((voidResult, ex) -> { if (ex == null) { System.out.println(certificateArn + " was successfully removed from " + thingName); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + ex.getMessage()); } } }); future.join(); } /** * Deletes a certificate asynchronously. * * @param certificateArn The ARN of the certificate to delete. * * This method initiates an asynchronous request to delete a certificate. * If the deletion is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void deleteCertificate(String certificateArn) { DeleteCertificateRequest certificateProviderRequest = DeleteCertificateRequest.builder() .certificateId(extractCertificateId(certificateArn)) .build(); CompletableFuture<DeleteCertificateResponse> future = getAsyncClient().deleteCertificate(certificateProviderRequest); future.whenComplete((voidResult, ex) -> { if (ex == null) { System.out.println(certificateArn + " was successfully deleted."); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + ex.getMessage()); } } }); future.join(); } /** * Deletes an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing to delete. * * This method initiates an asynchronous request to delete an IoT Thing. * If the deletion is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void deleteIoTThing(String thingName) { DeleteThingRequest deleteThingRequest = DeleteThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<DeleteThingResponse> future = getAsyncClient().deleteThing(deleteThingRequest); future.whenComplete((voidResult, ex) -> { if (ex == null) { System.out.println("Deleted Thing " + thingName); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + ex.getMessage()); } } }); future.join(); } // Get the cert Id from the Cert ARN value. private String extractCertificateId(String certificateArn) { // Example ARN: arn:aws:iot:region:account-id:cert/certificate-id. String[] arnParts = certificateArn.split(":"); String certificateIdPart = arnParts[arnParts.length - 1]; return certificateIdPart.substring(certificateIdPart.lastIndexOf("/") + 1); } }
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import aws.sdk.kotlin.services.iot.IotClient import aws.sdk.kotlin.services.iot.model.Action import aws.sdk.kotlin.services.iot.model.AttachThingPrincipalRequest import aws.sdk.kotlin.services.iot.model.AttributePayload import aws.sdk.kotlin.services.iot.model.CreateThingRequest import aws.sdk.kotlin.services.iot.model.CreateTopicRuleRequest import aws.sdk.kotlin.services.iot.model.DeleteCertificateRequest import aws.sdk.kotlin.services.iot.model.DeleteThingRequest import aws.sdk.kotlin.services.iot.model.DescribeEndpointRequest import aws.sdk.kotlin.services.iot.model.DescribeThingRequest import aws.sdk.kotlin.services.iot.model.DetachThingPrincipalRequest import aws.sdk.kotlin.services.iot.model.ListTopicRulesRequest import aws.sdk.kotlin.services.iot.model.SearchIndexRequest import aws.sdk.kotlin.services.iot.model.SnsAction import aws.sdk.kotlin.services.iot.model.TopicRulePayload import aws.sdk.kotlin.services.iot.model.UpdateThingRequest import aws.sdk.kotlin.services.iotdataplane.IotDataPlaneClient import aws.sdk.kotlin.services.iotdataplane.model.GetThingShadowRequest import aws.sdk.kotlin.services.iotdataplane.model.UpdateThingShadowRequest import aws.smithy.kotlin.runtime.content.ByteStream import aws.smithy.kotlin.runtime.content.toByteArray import java.util.Scanner import java.util.regex.Pattern import kotlin.system.exitProcess /** * Before running this Kotlin code example, ensure that your development environment * is set up, including configuring your credentials. * * For detailed instructions, refer to the following documentation topic: * [Setting Up Your Development Environment](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html) * * This code example requires an SNS topic and an IAM Role. * Follow the steps in the documentation to set up these resources: * * - [Creating an SNS Topic](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html#step-create-topic) * - [Creating an IAM Role](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html) */ val DASHES = String(CharArray(80)).replace("\u0000", "-") val TOPIC = "your-iot-topic" suspend fun main(args: Array<String>) { val usage = """ Usage: <roleARN> <snsAction> Where: roleARN - The ARN of an IAM role that has permission to work with AWS IOT. snsAction - An ARN of an SNS topic. """.trimIndent() if (args.size != 2) { println(usage) exitProcess(1) } var thingName: String val roleARN = args[0] val snsAction = args[1] val scanner = Scanner(System.`in`) println(DASHES) println("Welcome to the AWS IoT example scenario.") println( """ This example program demonstrates various interactions with the AWS Internet of Things (IoT) Core service. The program guides you through a series of steps, including creating an IoT thing, generating a device certificate, updating the thing with attributes, and so on. It utilizes the AWS SDK for Kotlin and incorporates functionality for creating and managing IoT things, certificates, rules, shadows, and performing searches. The program aims to showcase AWS IoT capabilities and provides a comprehensive example for developers working with AWS IoT in a Kotlin environment. """.trimIndent(), ) print("Press Enter to continue...") scanner.nextLine() println(DASHES) println(DASHES) println("1. Create an AWS IoT thing.") println( """ An AWS IoT thing represents a virtual entity in the AWS IoT service that can be associated with a physical device. """.trimIndent(), ) // Prompt the user for input. print("Enter thing name: ") thingName = scanner.nextLine() createIoTThing(thingName) describeThing(thingName) println(DASHES) println(DASHES) println("2. Generate a device certificate.") println( """ A device certificate performs a role in securing the communication between devices (things) and the AWS IoT platform. """.trimIndent(), ) print("Do you want to create a certificate for $thingName? (y/n)") val certAns = scanner.nextLine() var certificateArn: String? = "" if (certAns != null && certAns.trim { it <= ' ' }.equals("y", ignoreCase = true)) { certificateArn = createCertificate() println("Attach the certificate to the AWS IoT thing.") attachCertificateToThing(thingName, certificateArn) } else { println("A device certificate was not created.") } println(DASHES) println(DASHES) println("3. Update an AWS IoT thing with Attributes.") println( """ IoT thing attributes, represented as key-value pairs, offer a pivotal advantage in facilitating efficient data management and retrieval within the AWS IoT ecosystem. """.trimIndent(), ) print("Press Enter to continue...") scanner.nextLine() updateThing(thingName) println(DASHES) println(DASHES) println("4. Return a unique endpoint specific to the Amazon Web Services account.") println( """ An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point for communication between IoT devices and the AWS IoT service. """.trimIndent(), ) print("Press Enter to continue...") scanner.nextLine() val endpointUrl = describeEndpoint() println(DASHES) println(DASHES) println("5. List your AWS IoT certificates") print("Press Enter to continue...") scanner.nextLine() if (certificateArn!!.isNotEmpty()) { listCertificates() } else { println("You did not create a certificates. Skipping this step.") } println(DASHES) println(DASHES) println("6. Create an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device") println( """ A thing shadow refers to a feature that enables you to create a virtual representation, or "shadow," of a physical device or thing. The thing shadow allows you to synchronize and control the state of a device between the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a thing shadow. """.trimIndent(), ) print("Press Enter to continue...") scanner.nextLine() updateShawdowThing(thingName) println(DASHES) println(DASHES) println("7. Write out the state information, in JSON format.") print("Press Enter to continue...") scanner.nextLine() getPayload(thingName) println(DASHES) println(DASHES) println("8. Creates a rule") println( """ Creates a rule that is an administrator-level action. Any user who has permission to create rules will be able to access data processed by the rule. """.trimIndent(), ) print("Enter Rule name: ") val ruleName = scanner.nextLine() createIoTRule(roleARN, ruleName, snsAction) println(DASHES) println(DASHES) println("9. List your rules.") print("Press Enter to continue...") scanner.nextLine() listIoTRules() println(DASHES) println(DASHES) println("10. Search things using the name.") print("Press Enter to continue...") scanner.nextLine() val queryString = "thingName:$thingName" searchThings(queryString) println(DASHES) println(DASHES) if (certificateArn.length > 0) { print("Do you want to detach and delete the certificate for $thingName? (y/n)") val delAns = scanner.nextLine() if (delAns != null && delAns.trim { it <= ' ' }.equals("y", ignoreCase = true)) { println("11. You selected to detach amd delete the certificate.") print("Press Enter to continue...") scanner.nextLine() detachThingPrincipal(thingName, certificateArn) deleteCertificate(certificateArn) } else { println("11. You selected not to delete the certificate.") } } else { println("11. You did not create a certificate so there is nothing to delete.") } println(DASHES) println(DASHES) println("12. Delete the AWS IoT thing.") print("Do you want to delete the IoT thing? (y/n)") val delAns = scanner.nextLine() if (delAns != null && delAns.trim { it <= ' ' }.equals("y", ignoreCase = true)) { deleteIoTThing(thingName) } else { println("The IoT thing was not deleted.") } println(DASHES) println(DASHES) println("The AWS IoT workflow has successfully completed.") println(DASHES) } suspend fun deleteIoTThing(thingNameVal: String) { val deleteThingRequest = DeleteThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.deleteThing(deleteThingRequest) println("Deleted $thingNameVal") } } suspend fun deleteCertificate(certificateArn: String) { val certificateProviderRequest = DeleteCertificateRequest { certificateId = extractCertificateId(certificateArn) } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.deleteCertificate(certificateProviderRequest) println("$certificateArn was successfully deleted.") } } private fun extractCertificateId(certificateArn: String): String? { // Example ARN: arn:aws:iot:region:account-id:cert/certificate-id. val arnParts = certificateArn.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() val certificateIdPart = arnParts[arnParts.size - 1] return certificateIdPart.substring(certificateIdPart.lastIndexOf("/") + 1) } suspend fun detachThingPrincipal( thingNameVal: String, certificateArn: String, ) { val thingPrincipalRequest = DetachThingPrincipalRequest { principal = certificateArn thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.detachThingPrincipal(thingPrincipalRequest) println("$certificateArn was successfully removed from $thingNameVal") } } suspend fun searchThings(queryStringVal: String?) { val searchIndexRequest = SearchIndexRequest { queryString = queryStringVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val searchIndexResponse = iotClient.searchIndex(searchIndexRequest) if (searchIndexResponse.things?.isEmpty() == true) { println("No things found.") } else { searchIndexResponse.things ?.forEach { thing -> println("Thing id found using search is ${thing.thingId}") } } } } suspend fun listIoTRules() { val listTopicRulesRequest = ListTopicRulesRequest {} IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val listTopicRulesResponse = iotClient.listTopicRules(listTopicRulesRequest) println("List of IoT rules:") val ruleList = listTopicRulesResponse.rules ruleList?.forEach { rule -> println("Rule name: ${rule.ruleName}") println("Rule ARN: ${rule.ruleArn}") println("--------------") } } } suspend fun createIoTRule( roleARNVal: String?, ruleNameVal: String?, action: String?, ) { val sqlVal = "SELECT * FROM '$TOPIC '" val action1 = SnsAction { targetArn = action roleArn = roleARNVal } val myAction = Action { sns = action1 } val topicRulePayloadVal = TopicRulePayload { sql = sqlVal actions = listOf(myAction) } val topicRuleRequest = CreateTopicRuleRequest { ruleName = ruleNameVal topicRulePayload = topicRulePayloadVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createTopicRule(topicRuleRequest) println("IoT rule created successfully.") } } suspend fun getPayload(thingNameVal: String?) { val getThingShadowRequest = GetThingShadowRequest { thingName = thingNameVal } IotDataPlaneClient.fromEnvironment { region = "us-east-1" }.use { iotPlaneClient -> val getThingShadowResponse = iotPlaneClient.getThingShadow(getThingShadowRequest) val payload = getThingShadowResponse.payload val payloadString = payload?.let { java.lang.String(it, Charsets.UTF_8) } println("Received shadow data: $payloadString") } } suspend fun listCertificates() { IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val response = iotClient.listCertificates() val certList = response.certificates certList?.forEach { cert -> println("Cert id: ${cert.certificateId}") println("Cert Arn: ${cert.certificateArn}") } } } suspend fun describeEndpoint(): String? { val request = DescribeEndpointRequest {} IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val endpointResponse = iotClient.describeEndpoint(request) val endpointUrl: String? = endpointResponse.endpointAddress val exString: String = getValue(endpointUrl) val fullEndpoint = "https://$exString-ats.iot.us-east-1.amazonaws.com" println("Full endpoint URL: $fullEndpoint") return fullEndpoint } } private fun getValue(input: String?): String { // Define a regular expression pattern for extracting the subdomain. val pattern = Pattern.compile("^(.*?)\\.iot\\.us-east-1\\.amazonaws\\.com") // Match the pattern against the input string. val matcher = pattern.matcher(input) // Check if a match is found. if (matcher.find()) { val subdomain = matcher.group(1) println("Extracted subdomain: $subdomain") return subdomain } else { println("No match found") } return "" } suspend fun updateThing(thingNameVal: String?) { val newLocation = "Office" val newFirmwareVersion = "v2.0" val attMap: MutableMap<String, String> = HashMap() attMap["location"] = newLocation attMap["firmwareVersion"] = newFirmwareVersion val attributePayloadVal = AttributePayload { attributes = attMap } val updateThingRequest = UpdateThingRequest { thingName = thingNameVal attributePayload = attributePayloadVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> // Update the IoT thing attributes. iotClient.updateThing(updateThingRequest) println("$thingNameVal attributes updated successfully.") } } suspend fun updateShawdowThing(thingNameVal: String?) { // Create the thing shadow state document. val stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}" val byteStream: ByteStream = ByteStream.fromString(stateDocument) val byteArray: ByteArray = byteStream.toByteArray() val updateThingShadowRequest = UpdateThingShadowRequest { thingName = thingNameVal payload = byteArray } IotDataPlaneClient.fromEnvironment { region = "us-east-1" }.use { iotPlaneClient -> iotPlaneClient.updateThingShadow(updateThingShadowRequest) println("The thing shadow was updated successfully.") } } suspend fun attachCertificateToThing( thingNameVal: String?, certificateArn: String?, ) { val principalRequest = AttachThingPrincipalRequest { thingName = thingNameVal principal = certificateArn } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.attachThingPrincipal(principalRequest) println("Certificate attached to $thingNameVal successfully.") } } suspend fun describeThing(thingNameVal: String) { val thingRequest = DescribeThingRequest { thingName = thingNameVal } // Print Thing details. IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val describeResponse = iotClient.describeThing(thingRequest) println("Thing details:") println("Thing name: ${describeResponse.thingName}") println("Thing ARN: ${describeResponse.thingArn}") } } suspend fun createCertificate(): String? { IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val response = iotClient.createKeysAndCertificate() val certificatePem = response.certificatePem val certificateArn = response.certificateArn // Print the details. println("\nCertificate:") println(certificatePem) println("\nCertificate ARN:") println(certificateArn) return certificateArn } } suspend fun createIoTThing(thingNameVal: String) { val createThingRequest = CreateThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createThing(createThingRequest) println("Created $thingNameVal}") } }
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create an IoT wrapper class to manage operations.
class IoTWrapper: """Encapsulates AWS IoT actions.""" def __init__(self, iot_client, iot_data_client=None): """ :param iot_client: A Boto3 AWS IoT client. :param iot_data_client: A Boto3 AWS IoT Data Plane client. """ self.iot_client = iot_client self.iot_data_client = iot_data_client @classmethod def from_client(cls): iot_client = boto3.client("iot") iot_data_client = boto3.client("iot-data") return cls(iot_client, iot_data_client) def create_thing(self, thing_name): """ Creates an AWS IoT thing. :param thing_name: The name of the thing to create. :return: The name and ARN of the created thing. """ try: response = self.iot_client.create_thing(thingName=thing_name) logger.info("Created thing %s.", thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.info("Thing %s already exists. Skipping creation.", thing_name) return None logger.error( "Couldn't create thing %s. Here's why: %s: %s", thing_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def list_things(self): """ Lists AWS IoT things. :return: The list of things. """ try: things = [] paginator = self.iot_client.get_paginator("list_things") for page in paginator.paginate(): things.extend(page["things"]) logger.info("Retrieved %s things.", len(things)) return things except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't list things. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def create_keys_and_certificate(self): """ Creates keys and a certificate for an AWS IoT thing. :return: The certificate ID, ARN, and PEM. """ try: response = self.iot_client.create_keys_and_certificate(setAsActive=True) logger.info("Created certificate %s.", response["certificateId"]) except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't create keys and certificate. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def attach_thing_principal(self, thing_name, principal): """ Attaches a certificate to an AWS IoT thing. :param thing_name: The name of the thing. :param principal: The ARN of the certificate. """ try: self.iot_client.attach_thing_principal( thingName=thing_name, principal=principal ) logger.info("Attached principal %s to thing %s.", principal, thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot attach principal. Resource not found.") return logger.error( "Couldn't attach principal to thing. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def describe_endpoint(self, endpoint_type="iot:Data-ATS"): """ Gets the AWS IoT endpoint. :param endpoint_type: The endpoint type. :return: The endpoint. """ try: response = self.iot_client.describe_endpoint(endpointType=endpoint_type) logger.info("Retrieved endpoint %s.", response["endpointAddress"]) except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't describe endpoint. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["endpointAddress"] def list_certificates(self): """ Lists AWS IoT certificates. :return: The list of certificates. """ try: certificates = [] paginator = self.iot_client.get_paginator("list_certificates") for page in paginator.paginate(): certificates.extend(page["certificates"]) logger.info("Retrieved %s certificates.", len(certificates)) return certificates except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't list certificates. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def detach_thing_principal(self, thing_name, principal): """ Detaches a certificate from an AWS IoT thing. :param thing_name: The name of the thing. :param principal: The ARN of the certificate. """ try: self.iot_client.detach_thing_principal( thingName=thing_name, principal=principal ) logger.info("Detached principal %s from thing %s.", principal, thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot detach principal. Resource not found.") return logger.error( "Couldn't detach principal from thing. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def delete_certificate(self, certificate_id): """ Deletes an AWS IoT certificate. :param certificate_id: The ID of the certificate to delete. """ try: self.iot_client.update_certificate( certificateId=certificate_id, newStatus="INACTIVE" ) self.iot_client.delete_certificate(certificateId=certificate_id) logger.info("Deleted certificate %s.", certificate_id) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot delete certificate. Resource not found.") return logger.error( "Couldn't delete certificate. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def create_topic_rule(self, rule_name, topic, sns_action_arn, role_arn): """ Creates an AWS IoT topic rule. :param rule_name: The name of the rule. :param topic: The MQTT topic to subscribe to. :param sns_action_arn: The ARN of the SNS topic to publish to. :param role_arn: The ARN of the IAM role. """ try: self.iot_client.create_topic_rule( ruleName=rule_name, topicRulePayload={ "sql": f"SELECT * FROM '{topic}'", "actions": [ {"sns": {"targetArn": sns_action_arn, "roleArn": role_arn}} ], }, ) logger.info("Created topic rule %s.", rule_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.info("Topic rule %s already exists. Skipping creation.", rule_name) return logger.error( "Couldn't create topic rule. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def list_topic_rules(self): """ Lists AWS IoT topic rules. :return: The list of topic rules. """ try: rules = [] paginator = self.iot_client.get_paginator("list_topic_rules") for page in paginator.paginate(): rules.extend(page["rules"]) logger.info("Retrieved %s topic rules.", len(rules)) return rules except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't list topic rules. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def search_index(self, query): """ Searches the AWS IoT index. :param query: The search query. :return: The list of things found. """ try: response = self.iot_client.search_index(queryString=query) logger.info("Found %s things.", len(response.get("things", []))) except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't search index. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response.get("things", []) def update_indexing_configuration(self): """ Updates the AWS IoT indexing configuration to enable thing indexing. """ try: self.iot_client.update_indexing_configuration( thingIndexingConfiguration={"thingIndexingMode": "REGISTRY"} ) logger.info("Updated indexing configuration.") except ClientError as err: logger.error( "Couldn't update indexing configuration. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def delete_thing(self, thing_name): """ Deletes an AWS IoT thing. :param thing_name: The name of the thing to delete. """ try: self.iot_client.delete_thing(thingName=thing_name) logger.info("Deleted thing %s.", thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot delete thing. Resource not found.") return logger.error( "Couldn't delete thing. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def delete_topic_rule(self, rule_name): """ Deletes an AWS IoT topic rule. :param rule_name: The name of the rule to delete. """ try: self.iot_client.delete_topic_rule(ruleName=rule_name) logger.info("Deleted topic rule %s.", rule_name) except ClientError as err: logger.error( "Couldn't delete topic rule. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def update_thing_shadow(self, thing_name, shadow_state): """ Updates the shadow for an AWS IoT thing. :param thing_name: The name of the thing. :param shadow_state: The shadow state as a dictionary. """ import json try: self.iot_data_client.update_thing_shadow( thingName=thing_name, payload=json.dumps(shadow_state) ) logger.info("Updated shadow for thing %s.", thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot update thing shadow. Resource not found.") return logger.error( "Couldn't update thing shadow. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def get_thing_shadow(self, thing_name): """ Gets the shadow for an AWS IoT thing. :param thing_name: The name of the thing. :return: The shadow state as a dictionary. """ import json try: response = self.iot_data_client.get_thing_shadow(thingName=thing_name) shadow = json.loads(response["payload"].read()) logger.info("Retrieved shadow for thing %s.", thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot get thing shadow. Resource not found.") return None logger.error( "Couldn't get thing shadow. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return shadowRun an interactive scenario demonstrating IoT basics.
class IoTScenario: """Runs an interactive scenario that shows how to use AWS IoT.""" is_interactive = True def __init__(self, iot_wrapper, iot_data_client, cfn_client, stack_name="IoTBasicsStack", template_path=None): """ :param iot_wrapper: An instance of the IoTWrapper class. :param iot_data_client: A Boto3 IoT Data Plane client. :param cfn_client: A Boto3 CloudFormation client. :param stack_name: Name for the CloudFormation stack. :param template_path: Path to the CloudFormation template file. """ self.iot_wrapper = iot_wrapper self.iot_data_client = iot_data_client self.cfn_client = cfn_client self.thing_name = None self.certificate_arn = None self.certificate_id = None self.rule_name = None self.stack_name = stack_name self.template_path = template_path or "../../../scenarios/basics/iot/iot_usecase/resources/cfn_template.yaml" def _deploy_stack(self): """Deploy CloudFormation stack and return outputs.""" with open(self.template_path, "r") as f: template_body = f.read() try: self.cfn_client.create_stack( StackName=self.stack_name, TemplateBody=template_body, Capabilities=["CAPABILITY_NAMED_IAM"] ) waiter = self.cfn_client.get_waiter("stack_create_complete") waiter.wait(StackName=self.stack_name) response = self.cfn_client.describe_stacks(StackName=self.stack_name) outputs = {output["OutputKey"]: output["OutputValue"] for output in response["Stacks"][0]["Outputs"]} return outputs["SNSTopicArn"], outputs["RoleArn"] except ClientError as err: if err.response["Error"]["Code"] == "AlreadyExistsException": response = self.cfn_client.describe_stacks(StackName=self.stack_name) outputs = {output["OutputKey"]: output["OutputValue"] for output in response["Stacks"][0]["Outputs"]} return outputs["SNSTopicArn"], outputs["RoleArn"] raise def _cleanup_stack(self): """Delete CloudFormation stack.""" try: self.cfn_client.delete_stack(StackName=self.stack_name) waiter = self.cfn_client.get_waiter("stack_delete_complete") waiter.wait(StackName=self.stack_name) print("CloudFormation stack deleted successfully.") except ClientError as err: logger.error(f"Failed to delete stack: {err}") def run_scenario(self, thing_name, rule_name): """ Runs the IoT basics scenario. :param thing_name: The name of the thing to create. :param rule_name: The name of the topic rule to create. """ print("-" * 88) print("Welcome to the AWS IoT basics scenario!") print("-" * 88) print( "This scenario demonstrates how to interact with AWS IoT using the AWS SDK for Python (Boto3).\n" "AWS IoT provides secure, bi-directional communication between Internet-connected devices\n" "and the AWS cloud. You can manage device connections, process device data, and build IoT applications.\n" ) self.thing_name = thing_name self.rule_name = rule_name try: print("\nDeploying CloudFormation stack...") sns_topic_arn, role_arn = self._deploy_stack() print(f"Stack deployed. SNS Topic: {sns_topic_arn}") input("\nNext, we'll create an AWS IoT thing. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("1. Create an AWS IoT thing") print("-" * 88) response = self.iot_wrapper.create_thing(thing_name) print(f"Created thing: {response['thingName']}") print(f"Thing ARN: {response['thingArn']}") input("\nNext, we'll list things. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("2. List things") print("-" * 88) things = self.iot_wrapper.list_things() print(f"Found {len(things)} thing(s) in your account") for thing in things[:5]: # Show first 5 print(f" Thing name: {thing['thingName']}") input("\nNext, we'll generate a device certificate. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("3. Generate a device certificate") print("-" * 88) cert_response = self.iot_wrapper.create_keys_and_certificate() self.certificate_arn = cert_response["certificateArn"] self.certificate_id = cert_response["certificateId"] print(f"Created certificate: {self.certificate_id}") input("\nNext, we'll attach the certificate to the thing. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("4. Attach the certificate to the thing") print("-" * 88) self.iot_wrapper.attach_thing_principal(thing_name, self.certificate_arn) print(f"Attached certificate to thing: {thing_name}") input("\nNext, we'll update the thing shadow. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("5. Update the thing shadow") print("-" * 88) shadow_state = {"state": {"reported": {"temperature": 25, "humidity": 50}}} self.iot_wrapper.update_thing_shadow(thing_name, shadow_state) print(f"Updated shadow for thing: {thing_name}") input("\nNext, we'll get the thing shadow. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("6. Get the thing shadow") print("-" * 88) shadow = self.iot_wrapper.get_thing_shadow(thing_name) print(f"Shadow state: {json.dumps(shadow['state'], indent=2)}") input("\nNext, we'll get the AWS IoT endpoint. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("7. Get the AWS IoT endpoint") print("-" * 88) endpoint = self.iot_wrapper.describe_endpoint() print(f"IoT endpoint: {endpoint}") input("\nNext, we'll list certificates. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("8. List certificates") print("-" * 88) certificates = self.iot_wrapper.list_certificates() print(f"Found {len(certificates)} certificate(s)") for cert in certificates: print(f" Certificate ID: {cert['certificateId']}") print(f" Certificate ARN: {cert['certificateArn']}") print(f" Status: {cert['status']}") input("\nNext, we'll create a topic rule. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("9. Create a topic rule") print("-" * 88) self.iot_wrapper.create_topic_rule( rule_name, f"device/{thing_name}/data", sns_topic_arn, role_arn ) print(f"Created topic rule: {rule_name}") input("\nNext, we'll list topic rules. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("10. List topic rules") print("-" * 88) rules = self.iot_wrapper.list_topic_rules() print(f"Found {len(rules)} topic rule(s)") for rule in rules: print(f" Rule name: {rule['ruleName']}") print(f" Rule ARN: {rule['ruleArn']}") input("\nNext, we'll configure thing indexing. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("11. Configure thing indexing") print("-" * 88) self.iot_wrapper.update_indexing_configuration() print("Enabled thing indexing") print("Waiting for indexing to be ready...") time.sleep(10) input("\nNext, we'll search for things. Press Enter to continue...") if self.is_interactive else None print("\n" + "-" * 88) print("12. Search for things") print("-" * 88) try: things = self.iot_wrapper.search_index(f"thingName:{thing_name}") if things: print(f"Found {len(things)} thing(s) matching the query") for thing in things: print(f" Thing name: {thing.get('thingName', 'N/A')}") print(f" Thing ID: {thing.get('thingId', 'N/A')}") else: print("No things found. Indexing may take a few minutes.") except ClientError as err: if err.response["Error"]["Code"] in [ "IndexNotReadyException", "InvalidRequestException", ]: print("Search index not ready yet. This is expected.") else: raise except ClientError as err: logger.error( "Scenario failed: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise finally: self._cleanup() def _cleanup(self): """Cleans up resources created during the scenario.""" if not self.thing_name: return print("\n" + "-" * 88) print("Cleanup") print("-" * 88) if q.ask("Do you want to delete the resources? (y/n) ", q.is_yesno): try: if self.certificate_arn: print(f"Detaching certificate from thing: {self.thing_name}") self.iot_wrapper.detach_thing_principal( self.thing_name, self.certificate_arn ) if self.certificate_id: print(f"Deleting certificate: {self.certificate_id}") self.iot_wrapper.delete_certificate(self.certificate_id) if self.thing_name: print(f"Deleting thing: {self.thing_name}") self.iot_wrapper.delete_thing(self.thing_name) if self.rule_name: print(f"Deleting topic rule: {self.rule_name}") self.iot_wrapper.delete_topic_rule(self.rule_name) self._cleanup_stack() print("Resources deleted successfully.") except ClientError as err: logger.error( "Cleanup failed: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) else: print("Resources will remain in your account.") print("\n" + "-" * 88) print("Thanks for using AWS IoT!") print("-" * 88)