This Node.js code example shows:
- How to carry out basic tasks in managing server certificates for HTTPS connections.
To enable HTTPS connections to your website or application on AWS, you need an SSL/TLS server certificate. To use a certificate that you obtained from an external provider with your website or application on AWS, you must upload the certificate to IAM or import it into AWS Certificate Manager.
In this example, a series of Node.js modules are used to handle server certificates in IAM. The Node.js modules use the SDK for JavaScript to manage server certificates using these methods of the AWS.IAM
client class:
For more information about server certificates, see Working with Server Certificates in the IAM User Guide.
To set up and run this example, you must first complete these tasks:
- Install Node.js. For more information about installing Node.js, see the Node.js website.
- Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading Credentials in Node.js from the Shared Credentials File.
Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2
.
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'us-west-2'});
Create a Node.js module with the file name iam_listservercerts.js
. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM
service object. Call the listServerCertificates
method of the AWS.IAM
service object.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the IAM service object
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
iam.listServerCertificates().eachPage(function(err, data) {
if (err) {
throw err;
}
if (data && data.ServerCertificateMetadataList) {
data.ServerCertificateMetadataList.forEach(function(metadata) {
console.log(metadata);
});
}
});
To run the example, type the following at the command line.
node iam_listservercerts.js
This sample code can be found here on GitHub.
Create a Node.js module with the file name iam_getservercert.js
. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM
service object. Create a JSON object containing the parameters needed get a certificate, which consists of the name of the server certificate you want. Call the getServerCertificates
method of the AWS.IAM
service object.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the IAM service object
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
iam.getServerCertificate({ServerCertificateName: 'CERTIFICATE_NAME'}, function(err, data) {
if (err) {
throw err;
} else {
console.log('Server Certificate:');
console.log(data);
}
});
To run the example, type the following at the command line.
node iam_getservercert.js
This sample code can be found here on GitHub.
Create a Node.js module with the file name iam_updateservercert.js
. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM
service object. Create a JSON object containing the parameters needed to update a certificate, which consists of the name of the existing server certificate as well as the name of the new certificate. Call the updateServerCertificate
method of the AWS.IAM
service object.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the IAM service object
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
var params = {
ServerCertificateName: 'CERTIFICATE_NAME',
NewServerCertificateName: 'NEW_CERTIFICATE_NAME'
};
iam.updateServerCertificate(params, function(err, data) {
if (err) {
throw err;
} else {
console.log('Server Certificate updated.');
}
});
To run the example, type the following at the command line.
node iam_updateservercert.js
This sample code can be found here on GitHub.
Create a Node.js module with the file name iam_deleteservercert.js
. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM
service object. Create a JSON object containing the parameters needed to delete a server certificate, which consists of the name of the certificate you want to delete. Call the deleteServerCertificates
method of the AWS.IAM
service object.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create the IAM service object
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
iam.deleteServerCertificate({ServerCertificateName: 'CERTIFICATE_NAME'}, function(err, data) {
if (err) {
throw err;
} else {
console.log('Server Certificate deleted.');
}
});
To run the example, type the following at the command line.
node iam_deleteservercert.js
This sample code can be found here on GitHub.