Use UploadServerCertificate with an Amazon SDK or CLI
The following code examples show how to use UploadServerCertificate.
- CLI
-
- Amazon CLI
-
To upload a server certificate to your Amazon account
The following
upload-server-certificateexample uploads a server certificate to your Amazon account. In this example, the certificate is in the filepublic_key_cert_file.pem, the associated private key is in the filemy_private_key.pem, and the certificate chain provided by the certificate authority (CA) is in themy_certificate_chain_file.pemfile. When the file has finished uploading, it is available under the name myServerCertificate. Parameters that begin withfile://tell the command to read the contents of the file and use that as the parameter value instead of the file name itself.aws iam upload-server-certificate \ --server-certificate-namemyServerCertificate\ --certificate-bodyfile://public_key_cert_file.pem\ --private-keyfile://my_private_key.pem\ --certificate-chainfile://my_certificate_chain_file.pemContents of
public_key_cert_file.pem:-----BEGIN CERTIFICATE----- MIIDXTCCAkWgAwIBAgIJAEXAMPLE... -----END CERTIFICATE-----Contents of
my_private_key.pem:-----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAAEXAMPLE... -----END PRIVATE KEY-----Contents of
my_certificate_chain_file.pem:-----BEGIN CERTIFICATE----- MIIEkjCCA3qgAwIBAgIQEXAMPLE... -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- MIIDSjCCAjKgAwIBAgIQEXAMPLE... -----END CERTIFICATE-----Output:
{ "ServerCertificateMetadata": { "Path": "/", "ServerCertificateName": "myServerCertificate", "ServerCertificateId": "ASCAEXAMPLE123EXAMPLE", "Arn": "arn:aws:iam::1234567989012:server-certificate/myServerCertificate", "UploadDate": "2019-04-22T21:13:44+00:00", "Expiration": "2019-10-15T22:23:16+00:00" } }For more information, see Manage server certificates in IAM
in the Amazon IAM User Guide. -
For API details, see UploadServerCertificate
in Amazon CLI Command Reference.
-
- JavaScript
-
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. import { UploadServerCertificateCommand, IAMClient } from "@aws-sdk/client-iam"; import { readFileSync } from "node:fs"; import { dirnameFromMetaUrl } from "@aws-doc-sdk-examples/lib/utils/util-fs.js"; import * as path from "node:path"; const client = new IAMClient({}); const certMessage = `Generate a certificate and key with the following command, or the equivalent for your system. openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \ -keyout example.key -out example.crt -subj "/CN=example.com" \ -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1" `; const getCertAndKey = () => { try { const cert = readFileSync( path.join(dirnameFromMetaUrl(import.meta.url), "./example.crt"), ); const key = readFileSync( path.join(dirnameFromMetaUrl(import.meta.url), "./example.key"), ); return { cert, key }; } catch (err) { if (err.code === "ENOENT") { throw new Error( `Certificate and/or private key not found. ${certMessage}`, ); } throw err; } }; /** * * @param {string} certificateName */ export const uploadServerCertificate = (certificateName) => { const { cert, key } = getCertAndKey(); const command = new UploadServerCertificateCommand({ ServerCertificateName: certificateName, CertificateBody: cert.toString(), PrivateKey: key.toString(), }); return client.send(command); };-
For API details, see UploadServerCertificate in Amazon SDK for JavaScript API Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: This example uploads a new server certificate to the IAM account. The files containing the certificate body, the private key, and (optionally) the certificate chain must all be PEM encoded. Note that the parameters require the actual content of the files rather than the file names. You must use the
-Rawswitch parameter to successfully process the file contents.Publish-IAMServerCertificate -ServerCertificateName MyTestCert -CertificateBody (Get-Content -Raw server.crt) -PrivateKey (Get-Content -Raw server.key)Output:
Arn : arn:aws:iam::123456789012:server-certificate/MyTestCert Expiration : 1/14/2018 9:52:36 AM Path : / ServerCertificateId : ASCAJIEXAMPLE7J7HQZYW ServerCertificateName : MyTestCert UploadDate : 4/21/2015 11:14:16 AM-
For API details, see UploadServerCertificate
in Amazon Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: This example uploads a new server certificate to the IAM account. The files containing the certificate body, the private key, and (optionally) the certificate chain must all be PEM encoded. Note that the parameters require the actual content of the files rather than the file names. You must use the
-Rawswitch parameter to successfully process the file contents.Publish-IAMServerCertificate -ServerCertificateName MyTestCert -CertificateBody (Get-Content -Raw server.crt) -PrivateKey (Get-Content -Raw server.key)Output:
Arn : arn:aws:iam::123456789012:server-certificate/MyTestCert Expiration : 1/14/2018 9:52:36 AM Path : / ServerCertificateId : ASCAJIEXAMPLE7J7HQZYW ServerCertificateName : MyTestCert UploadDate : 4/21/2015 11:14:16 AM-
For API details, see UploadServerCertificate
in Amazon Tools for PowerShell Cmdlet Reference (V5).
-
For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.