Skip to content

Latest commit

 

History

History
executable file
·
164 lines (120 loc) · 7.04 KB

iam-examples-server-certificates.md

File metadata and controls

executable file
·
164 lines (120 loc) · 7.04 KB

Working with IAM Server Certificates

[JavaScript code example that applies to Node.js execution]

This Node.js code example shows:

  • How to carry out basic tasks in managing server certificates for HTTPS connections.

The Scenario

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.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

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'});

Listing Your Server Certificates

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.

Getting a Server Certificate

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.

Updating a Server Certificate

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.

Deleting a Server Certificate

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.