AWS Starting, Stopping and Terminating an EC2 Instance

An EC2 Instance in AWS can be considered a server and as such can reside in your AWS environment without executing.   In an AWS environment this has sever ramifications on Cost,  because an EC2 instance that is not currently executing only incurs storage costs and the customer avoids virtual machine costs and network utilization costs.   In this article we will review the process for starting, stopping and terminating already created EC2 instances.

1.  The following is required before you create IAM user for your AWS Free Tier Account.

 –  An Active AWS Account with admin level permissions..

2.  Logon to AWS as an IAM user at URL:  https://signin.aws.amazon.com/

3.  From the Home Console type EC2 in the search bar, select the star next to EC2, and select EC2

4.  On the right hand menu bar select Instance.

5.  We currently have one EC2 Instances built out, select your instance with a check marki and press the Instances state.  NOTE:  Record the instance ID for each of your EC2 instances to control them from the CLI.   In this example our Instance ID is i-0a223b5a7b0e24408

6.  From this menu you can start, stop and terminate the instance.  Note:   Terminating an instance is recoverable, but the resources you had before are not guarantied to be available, so insure that you will not need the instance before terminating and place it in a stop state if there are any doubts.

7.  You can also start, stop and terminate EC2 instances from you CLI application without logging into the console with the following commands and the instance id shown in setup 5 and the region id of the instance.   In this example our Instance ID is i-0a223b5a7b0e24408 and the region_id is  us-east-1   NOTE:   You have to have

   – Reports the details of your instance

  aws ec2 describe-instances –instance-ids i-0a223b5a7b0e24408 –region us-east-1

   – Stops the instance

  aws ec2 stop-instances –instance-ids i-0a223b5a7b0e24408 –region us-east-1

   – Starts the instance

  aws ec2 start-instances –instance-ids i-0a223b5a7b0e24408 –region us-east-1

   – Deletes the instance

  aws ec2 terminate-instances –instance-ids i-0a223b5a7b0e24408 –region us-east-1

AWS Creating EC2 Instance

The creation of an AWS EC2 Instance is normally accomplished via the AWS Console, but can be performed via the Command Line Interface (CLI).   In this example we will create a simple EC2 virtual instance with various parameters.  We will start the instance using a User Data script defined for mysql configuration.

1.  The following is required before you create IAM user for your AWS Free Tier Account.

        –  An Active AWS Account with admin level permissions..

2.  Logon to AWS as an IAM user at URL:  https://signin.aws.amazon.com/

3.  From the Home Console type EC2 in the search bar, select the star next to EC2, and select EC2

4.  On the right hand menu bar select Instance.

5.  We currently have no EC2 Instances built out so this screen will be empty,  press the Lauch Instances to begin

6.  There are multiple sections on a single screen so we will take the inputs section by section.  First provide a name for the new instance and select RHEL as the OS.

7.  Next select the AMI (Amazon Machine Image) and the architecture, here insure that you select the one which states “Free tier eligible”.

8.  Next in the section Instance Type select t2.micro which is free tier and in Key pair (login) click Create new key pair.

9.  On the Create key pair page, name you key pair, select RSA, and Private key file format of .pem and select Create key pair.    NOTE:  if you are using windows 10 or below or Mac you may want to select .ppk if you intend to use Putty.   Most will be using OpenSSH.

10.  Insure that you store your .pem file in a secure location

11.  The next section is Network settings and will except the general defaults.  A public IP will be assigned to our instance with the security group of launch-wizard-1 but you do have the option of selecting your owner security group for firewall configuration.   We are creating a database EC2 instances so will only allow SSH traffic from outside, but if you where configuring a webserver or boundary server you may allow HTTPS and HTTP connections.  

 12.  In the next section Configure Storage, you will select the type and speed of storage you like. We will leave the defaults.

13.  Next click the Advanced details section to expand.  This is a simple EC2 database instance, so we will select all of the defaults, except the User data section.  We will cut and past our own code to install a mysql database, start the database, and enable the database at first startup. Note: User Data is only executed at first instance startup.

14.  Next select you can execute Launch instance button.

15.  You will get a confirmation windows of Successful EC2 Instance startup.

16.  Moving back to the EC2 ? Instances Console and selecting refresh, you can see that the new EC2 Instance is in a Running state by is currently initializing (meaning the user data is still executing).

17.  Once completed, the status will change to green and is available for user,  insure that you note the IP address for future connections via CLI.

18.  This completes creating of a database EC2 Instance.