There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CreateThing with an AWS SDK or CLI
The following code examples show how to use CreateThing.
- .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
. /// <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; } }-
For API details, see CreateThing in AWS SDK for .NET API Reference.
-
- 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. /*! \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(); }-
For API details, see CreateThing in AWS SDK for C++ API Reference.
-
- CLI
-
- AWS CLI
-
Example 1: To create a thing record in the registry
The following
create-thingexample creates an entry for a device in the AWS IoT thing registry.aws iot create-thing \ --thing-nameSampleIoTThingOutput:
{ "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2: 123456789012:thing/SampleIoTThing", "thingId": " EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLE " }Example 2: To define a thing that is associated with a thing type
The following
create-thingexample create a thing that has the specified thing type and its attributes.aws iot create-thing \ --thing-name"MyLightBulb"\ --thing-type-name"LightBulb"\ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"Output:
{ "thingName": "MyLightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797" }For more information, see How to Manage Things with the Registry and Thing Types in the AWS IoT Developers Guide.
-
For API details, see CreateThing
in AWS CLI Command Reference.
-
- 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
. /** * 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(); }-
For API details, see CreateThing in AWS SDK for Java 2.x API Reference.
-
- 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
. suspend fun createIoTThing(thingNameVal: String) { val createThingRequest = CreateThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createThing(createThingRequest) println("Created $thingNameVal}") } }-
For API details, see CreateThing
in AWS SDK for Kotlin API reference.
-