AWS cli installation, configuration and stopping/starting EC2 machines

Author - Navneet Kashyap
24.03.2017
|
0 Comments
|

Install the AWS CLI Using the MSI Installer (Windows)
Download the AWS CLI MSI installer for Windows (64-bit) click-here or directly access https://s3.amazonaws.com/aws-cli/AWSCLI64.msi.
Run the above downloaded MSI installer. Follow the instructions that appear:
The CLI installs to C:\Program Files\Amazon\AWSCLI

open “cmd” from start menu and run below command to check version info

# aws –version

Install the AWS CLI (Linux)

Download the AWS CLI Bundled Installer using wget or curl from here or directly open https://s3.amazonaws.com/aws-cli/awscli-bundle.zip

Now run these commands:

# curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
# unzip awscli-bundle.zip
# sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Important
The bundled installer does not support installing to paths that contain spaces.
To see an explanation of the -i and -b options, use the -h option:

# ./awscli-bundle/install -h
Test the AWS CLI Installation
# aws help

Setting up profiles for aws cli account

Setting up single profile

# aws configure
(replace the information details with correct ID & keys)

AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLEID
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yConvbEXAMPLEKEYS
Default region name [None]: us-east-1
Default output format [None]: json
Setting up multiple profiles

If you have multiple access keys for different purposes you can configure additional named profiles by using the –profile option.

# aws configure –profile user2
And
# aws configure –profile user3
And,
# aws configure –profile user2

And so on ………. But set one at a time and replace the below details with correct ID and keys

AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLEID
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yConvbEXAMPLEKEYS
Default region name [None]: us-east-1
Default output format [None]: json

Note: In case if you enter wrong ID and keys as per above steps, you need not to worry just run the same command and update the details wherever it is required.

How to use aws cli to stop and start ec2 machines at a scheduled intervals using different profiles
To start EC2
When running scheduler from windows open notepad, paste the following command and save it in .bat extension file in a particular folder

aws ec2 –profile user2 start-instances –instance-ids i-802xxxx
When running scheduler from linux, open crontab file and paste the below command and save it:
aws ec2 –profile user2 start-instances –instance-ids i-802xxxx
To stop EC2
create a separate script for stopping ec2 and follow the same steps for windows and linux.

aws ec2 –profile user2 stop-instances –instance-ids i-802xxxx

Explanation:

Stop-instances or start-instances are inbuilt feature of aws ec2 command
–profile: it is used to specify account that you can use other than default one
–instance-ids: it is unique ID provided by amazon at the time of creating an ec2 instance
Now add script in scheduler to start an ec2 or list of ec2 machines at specific intervals (windows).

Example: open Task Scheduler in control panel under Administrative Tools and add a new task
1

2

3

4

5

Add script in scheduler to stop an ec2 machine with notification to active user using that ec2 machine (Windows).

open notepad and paste the below command and save it in .bat extension in a particular folder

shutdown -t 600 -c “This server is going to be Turned OFF in next 10 minutes. Please save your work !!!”

Explanation of above command:
/t: time delay 10 min (in seconds it is 600)
/c : description of task like mentioned above about shutting down the server.
Add script in scheduler to stop an ec2 machine with notification to active user using that ec2 machine (Linux).

Here we are using crontab to schedule this task.
# vi /etc/crontab
Paste the below script to stop ec2 machine at 10 PM

0 22 * * * root shutdown -h +10 “This server is going to be Turned OFF in next 10 minutes. Please save your work !!!”

Explanation of above command:
0 22 : (time in 24 hr format)
Root : (run as root “user”)
Shutdown -h : (to halt the server)
+10 : (time delay of 10 min)
“description” : (description of task like mentioned above about shutting down the server.)

Note: the above schedule is required to place in each machine which you want to shut down and alert the active user to save their work before it get turned off.
Add script in scheduler to start an ec2 machine (Linux).
Here we are using crontab to schedule this task.
# vi /etc/crontab
Paste the below scripts to start multiple ec2 machine at 5AM, 6AM, 7AM.

0 5 * * * root aws ec2 –profile user1 start-instances –instance-ids i-801xxxx
0 6 * * * root aws ec2 –profile user2 start-instances –instance-ids i-802xxxx
0 7 * * * root aws ec2 –profile user3 start-instances –instance-ids i-803xxxx

Important Note: Stopping ec2 machine using aws cli will not give alert message to user, so it will be better to use proper shutdown command on each machine with alert message.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *