Use CreateKeysAndCertificate with an AWS SDK or command line tool - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use CreateKeysAndCertificate with an AWS SDK or command line tool

The following code examples show how to use CreateKeysAndCertificate.

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 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(); }
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.

public static String createCertificate(IotClient iotClient) { try { CreateKeysAndCertificateResponse response = iotClient.createKeysAndCertificate(); String certificatePem = response.certificatePem(); String certificateArn = response.certificateArn(); // Print the details. System.out.println("\nCertificate:"); System.out.println(certificatePem); System.out.println("\nCertificate ARN:"); System.out.println(certificateArn); return certificateArn; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
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.

public static String createCertificate(IotClient iotClient) { try { CreateKeysAndCertificateResponse response = iotClient.createKeysAndCertificate(); String certificatePem = response.certificatePem(); String certificateArn = response.certificateArn(); // Print the details. System.out.println("\nCertificate:"); System.out.println(certificatePem); System.out.println("\nCertificate ARN:"); System.out.println(certificateArn); return certificateArn; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }