
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎08-22-2019 10:36 AM
Starting/Stopping multiple VMs in AWS
This article deals with how we can implement starting/stopping multiple VMs in AWS. The same technique can be used for other clouds as well.
AWS has an API to start and stop multiple VMs in a single API call. We are going to use those APIs.
https://ec2.amazonaws.com/?Action=StopInstances&InstanceId.1=i-1234567890abcdef0 https://ec2.amazonaws.com/?Action=StartInstances&InstanceId.1=i-1234567890abcdef0 |
We will be adding a Day-2 operation at the AWS Datacenter level. The flow will be such that the users will select a particular AWS Datacenter first and then choose the operation (start / stop multiple VMs) and then identify those instances that need to be targeted.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
==>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now let us look a the steps that are needed. In the end we will provide an update set which will do all the above as well.
- Make sure that the AWS Datacenters show up in the End User portal.
- Extend the OOB AWS Cloud API MID server scripts to do the multi VM Start and Stop.
- Create a new operation in AWS Datacenter for Starting and Stopping multiple VMs.
- Create a Catalog Item and use List Collector for choosing multiple VMs
Show AWS Datacenters in the End User Portal
This is captured elaborately in this article https://community.servicenow.com/community?id=community_article&sys_id=b28f7f711b4f7348ada243f6fe4bc.... Please read this. It is about making sure the AWS Datacenter CIs are visible in the portal. The change will be about setting the CAPI Resource type for AWS Datacenter to be visible and have proper category.
You can also import the attached xml into your instance and skip this step.
Extend OOB AWS Scripts
Create MID Scripts
The OOB Cloud API takes care of authentication and many operations. We will extend the OOB classes to do the specific Start/Stop multiple VMs.
We will use the AWSCloudAPIBase class that comes with the product. We will provide the proper service and APIVersion.
var AWSVMOperations = Class.create();
AWSVMOperations.prototype = Object.extendsObject(AWSCloudAPIBase, {
service: 'ec2',
apiVersion: '2016-11-15',
initialize: function(parameters, headers) {
AWSCloudAPIBase.prototype.initialize.call(this, parameters, headers);
},
multiVMDirectOps: function(action, vmInstanceIds) {
var index = 1;
var vms = new JSON().decode(vmInstanceIds);
var actionParameters = {};
for(var vmindex = 0; vmindex < vms.length; vmindex ++) {
actionParameters['InstanceId.'+(vmindex+1)] = vms[vmindex];
}
return this.executeAction(action, actionParameters);
},
addTags: function(tags,vmInstanceIds) {
var action = 'CreateTags';
var customTags = new JSON().decode(tags);
var index = 1;
var vms = new JSON().decode(vmInstanceIds);
var actionParameters = {};
for(var vmindex = 0; vmindex < vms.length; vmindex ++) {
actionParameters['ResourceId.'+(vmindex+1)] = vms[vmindex];
var i = 1;
for(var key in customTags){
actionParameters['Tag.'+(i+1)+'.Key'] = key;
actionParameters['Tag.'+(i+1)+'.Value'] = customTags[key];
i++;
}
}
return this.executeAction(action, actionParameters);
},
type: 'AWSVMOperations'
});
Look for the 'multiVMDirectOps' method. It takes in the action and array of VMs that need to be operated upon. The action could be either 'StartInstances' or 'StopInstances'. The array notation in the parameters are specific to the way AWS does it.
https://ec2.amazonaws.com/?Action=StopInstances&InstanceId.1=i-1234567890abcdef0 https://ec2.amazonaws.com/?Action=StartInstances&InstanceId.1=i-1234567890abcdef0 |
Create CAPI Artifacts
Create a single AWS Extensions Interface where you can put in all your methods. For starters, added 'StartNodes' and 'StopNodes' as the operation. Both these operation will take in 'Location' and 'NodeIDs' as parameters.
Create one CAPI API impl and call it as 'AWS Extensions API'. This could be your single API for all extensions. As a start it would have just two operations. Have the API Config Overrides setup as these pertain to the passing of the credentials seemlessly.
By default both the Start and Stop Nodes will be tied to a MID script include which does nothing. Get into that script and make the call to the AWSVMOperations class to do the needful.
Click on the record indicator as depicted in the image below:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Click on the Request Script record indicator to get to the auto-generated mid script include.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Create StopInstances Operation in AWS Datacenter Resource Block
Get to Cloud Admin Portal ==> Design ==> Resource Blocks ==> AWS Datacenter ==> Operation.
Override any operation (for example ConnectAndStopVirtualMachine) in Compute Interface and name it as 'AWS Extensions'. This resource interface will have the custom operations that will be exposed to the end user via a Day-2 catalog item.
Then add an operation called 'StopInstances' in the extension. Call the CAPI method which we did in the first step. The NodeIDs parameter is something which should be exposed to the catalog item. Check the checkbox for 'Create Form Parameter' and 'Mandatory'.
Now let us turn our attention to the catalog item as this is what the end user will be consuming. Click on the 'Generate Catalog' button to create the catalog item for this operation. After creation, the button will change to 'View Catalog'. You can click that to get to the catalog item. It will create a variable for the NodeIDs in addition to the others. The NodeIDs is something which we want to be multi-selectable. The service catalog provides a type called 'List Collector' and we will use this.
Let us make the NodeIDs to be List Collector and be backed by Virtual Machine Instance. Add a proper reference qualifier so that the only those VMs that are pertinent to the chosen datacenter/region will be picked.
The List Collector will always return a CSV of sys IDs. The AWS API though will need the resource IDs (object_id). We will use a script include to convert the CSV of sys_IDs to a list of Object IDs. We will use a method in CMDBResourceUtils to make this happen. The mapping would be '$(Script:sn_cmp.CMDBResourceUtils.convertVMSysIDs_to_ObjectIDs[arg=${parameter.NodeIDs}])' as indicated in the image below.
All the above changes are captured in the update set here : https://developer.servicenow.com/app.do#!/share/contents/4708895_stop_and_start_multiple_vms_in_aws?...
- 5,700 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Ashok this just came up yesterday in the forums as a question and you already provided the solution.. Shows power of the platform - totally rocking it.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks Ram. We need to enable our customers and prospects as much as possible 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Ashok,
Could you please help me to Create CAPI Artifacts?
I navigated to Cloud API > Library > Interface Tab and created one new AWS Extensions Interface.
I created 2 CAPI Interface Operations: StartNodes & Stop Node.
Later, I switched to API tab and created "AWS Extensions API". Unfortunately, Cloud API Interface, Connector, Scripted were ReadOnly. I cannot see any Mappers and COnfig Overrides populated. I believe this is because of the record not having Cloud API Interface.
Could please let me know what I'm missing?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Not sure. Can you delete that 'AWS Extensions API' and re-create it?
Did you import the update set or hand creating it? seems to me that you are hand creating. just want to be sure.
regards
ashok M
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes, I had hand created. Once I deleted and created again, I can see the fields populating. Now I can see CAPI Method Mappers but unable to see API Config Overrides.
Do you recommend by importing update set?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
you will have to hand create those records.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Ashok,
I imported the update sets and everything looks good on Cloud User Portal. I can request multiple VM's under on single Stop request.
When I request the Stop operation, CAPI Trails shows the Route status as "Success" but the instances were not stopping in AWS console.
I checked with our AWS team and they mentioned that they didn't see any API calls in Cloud Trials from their end.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Can you check in the cloud trail and look for any issues.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Also look at the data that went into cloud API and the response that came back.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Just sharing the info:
Route data:
{"ScriptType":"JavaScript","Endpoint":"$(CloudCredential.URL)","NodeIDs":"[\"i-0cd71b0a27fcf4bd5\",\"i-095dacc847a391688\",\"i-0c123c549cf09fcb9\"]","ScriptName":"aws-ec2-1.0-StopNodes","Identity":"$(CloudCredential.access_key)","Credentials":"$(CloudCredential.secret_key)","CloudOperation":"ExecuteScript","Provider":"aws-ec2","AccountAliasName":"$(CloudCredential.Alias)","Location":"us-east-1"}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The issue is resolved! Thanks for your inputs. Reason issue: I created manual steps and also imported the update set. Found duplicate CAPI Interface & API. Once removed the manual created Interface & API, I can successfully Start/Stop multiple VM's in AWS.
I'm looking forward to any future developments to have a relationship between the Location & AWS Account in the Cloud User Portal Resource table.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Can't we directly utilize the below endpoints to stop/start the instance?
Any idea how to pass authentication parameters and other parameters to this end point as I am planning to use it through ServiceNow Rest Messages which would be much easier.
Appreciate your quick response.
https://ec2.amazonaws.com/?Action=StartInstances&InstanceId.1=i-1234567890abcdef0 |
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I am using the same MID server script include as you provided. Can you please confirm if I can call it from a workflow? I want to reboot instance from Service Catalog, not from Cloud Portal.
Appreciate if you could respond.
var AWSVMOperations = Class.create();
AWSVMOperations.prototype = Object.extendsObject(AWSCloudAPIBase, {
service: 'ec2',
apiVersion: '2016-11-15',
initialize: function(parameters, headers) {
AWSCloudAPIBase.prototype.initialize.call(this, parameters, headers);
},
multiVMDirectOps: function(action, vmInstanceIds) {
var index = 1;
var vms = new JSON().decode(vmInstanceIds);
var actionParameters = {};
for(var vmindex = 0; vmindex < vms.length; vmindex ++) {
actionParameters['InstanceId.'+(vmindex+1)] = vms[vmindex];
}
return this.executeAction(action, actionParameters);
},
addTags: function(tags,vmInstanceIds) {
var action = 'CreateTags';
var customTags = new JSON().decode(tags);
var index = 1;
var vms = new JSON().decode(vmInstanceIds);
var actionParameters = {};
for(var vmindex = 0; vmindex < vms.length; vmindex ++) {
actionParameters['ResourceId.'+(vmindex+1)] = vms[vmindex];
var i = 1;
for(var key in customTags){
actionParameters['Tag.'+(i+1)+'.Key'] = key;
actionParameters['Tag.'+(i+1)+'.Value'] = customTags[key];
i++;
}
}
return this.executeAction(action, actionParameters);
},
type: 'AWSVMOperations'
});

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This script works in CMP and uses CMP dependencies. It wont wont in non CMP env.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Not useful at all.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for the feedback. Sorry about this but do note this tool was built specifically in response to customer asks - customers who use cloud management plugin (CMP). So also why this article is posted under cloud management category.
Would this be of use to you as an independent tool, to use without CMP? if so, how do you visualize this being used?
Can you give an idea of who will be using it?
Please let us know your needs so we can give appropriate guidance or take steps in the future roadmap.
Ram
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Most of our users use Service Catalogs/Service Portal from where they want to reboot their workspaces/EC2 Instances/VM Instnaces. Nobody is aware of CloudPortal and its very ambigious to provide so many consoles to the end user. I was trying to provde this feature from Service Catalog but it seems very hard.
Now I am trying the AWS CLI interfaces to provide the same feautures from Service Catalog.
Having so many consoles with no alternatives is of no use. It would be better to think in the perspective of Service Catalog or Service Portal.
Developing deferrent consoles is no ideal. Think about it.