Use ExportCertificate with an AWS SDK or CLI - AWS SDK Code Examples

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

Use ExportCertificate with an AWS SDK or CLI

The following code examples show how to use ExportCertificate.

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.

//! Export an AWS Certificate Manager (ACM) certificate. /*! \param certificateArn: The Amazon Resource Name (ARN) of a certificate. \param passphrase: A passphrase to decrypt the exported certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::ACM::exportCertificate(const Aws::String &certificateArn, const Aws::String &passphrase, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::ACM::ACMClient acm_client(clientConfiguration); Aws::ACM::Model::ExportCertificateRequest request; Aws::Utils::CryptoBuffer cryptoBuffer( reinterpret_cast<const unsigned char *>(passphrase.c_str()), passphrase.length()); request.WithCertificateArn(certificateArn).WithPassphrase(cryptoBuffer); Aws::ACM::Model::ExportCertificateOutcome outcome = acm_client.ExportCertificate(request); if (!outcome.IsSuccess()) { std::cerr << "Error: ExportCertificate: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Success: Information about certificate with ARN '" << certificateArn << "':" << std::endl << std::endl; auto result = outcome.GetResult(); std::cout << "Certificate: " << std::endl << std::endl << result.GetCertificate() << std::endl << std::endl; std::cout << "Certificate chain: " << std::endl << std::endl << result.GetCertificateChain() << std::endl << std::endl; std::cout << "Private key: " << std::endl << std::endl << result.GetPrivateKey() << std::endl; } return outcome.IsSuccess(); }
CLI
AWS CLI

To export a private certificate issued by a private CA.

The following export-certificate command exports a private certificate, certificate chain, and private key to your display:

aws acm export-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --passphrase file://path-to-passphrase-file

To export the certificate, chain, and private key to a local file, use the following command:

aws acm export-certificate --certificate-arn arn:aws:acm:region:sccount:certificate/12345678-1234-1234-1234-123456789012 --passphrase file://path-to-passphrase-file > c:\temp\export.txt
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.

/** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ExportCertificate { public static void main(String[] args) throws Exception { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; exportCert(certArn); } /** * Exports an SSL/TLS certificate and its associated private key and certificate chain from AWS Certificate Manager (ACM). * * @param certArn The Amazon Resource Name (ARN) of the certificate that you want to export. * @throws IOException If an I/O error occurs while reading the private key passphrase file or exporting the certificate. */ public static void exportCert(String certArn) throws IOException { AcmClient acmClient = AcmClient.create(); // Initialize a file descriptor for the passphrase file. RandomAccessFile filePassphrase = null; ByteBuffer bufPassphrase = null; // Create a file stream for reading the private key passphrase. try { filePassphrase = new RandomAccessFile("C:\\AWS\\password.txt", "r"); } catch (IllegalArgumentException | SecurityException | FileNotFoundException ex) { throw ex; } // Create a channel to map the file. FileChannel channelPassphrase = filePassphrase.getChannel(); // Map the file to the buffer. try { bufPassphrase = channelPassphrase.map(FileChannel.MapMode.READ_ONLY, 0, channelPassphrase.size()); channelPassphrase.close(); filePassphrase.close(); } catch (IOException ex) { throw ex; } // Create a request object. ExportCertificateRequest req = ExportCertificateRequest.builder() .certificateArn(certArn) .passphrase(SdkBytes.fromByteBuffer(bufPassphrase)) .build(); // Export the certificate. ExportCertificateResponse result = null; try { result = acmClient.exportCertificate(req); } catch (InvalidArnException | InvalidTagException | ResourceNotFoundException ex) { throw ex; } // Clear the buffer. bufPassphrase.clear(); // Display the certificate and certificate chain. String certificate = result.certificate(); System.out.println(certificate); String certificateChain = result.certificateChain(); System.out.println(certificateChain); // This example retrieves but does not display the private key. String privateKey = result.privateKey(); System.out.println("The example is complete"); } }