If you are looking to start your career in AWS cloud then knowing your first service that is AWS EC2 can give you a good understanding around the compute resources in AWS cloud. With AWS EC2 you will also understand which all services utilize AWS EC2.
Amazon EC2 stands for Amazon Elastic compute cloud that allows you to launch servers or virtual machines that are scalable in the Amazon Web service cloud. Also, with AWS EC2 instance, you don’t require to invest in any hardware or electricity costs, and you just pay for what you use.
When required, you can quickly decrease or scale up the number of AWS EC2 instances.
Instance requires operating systems, additional software, etc to get launched, so they use templates known as Amazon machine images (AMI).
You can work with various configurations with respect to computing such as Memory or CPU for that you will need to select the appropriate instance_type.
To securely log in to these instances you will need to generate the key pair where you store the private key and AWS manages key.
Instance can have two types of data ie. instance store that is temporary and the Amazon Elastic block store also known as EBS volumes.
Amazon EC2 (AWS Elastic compute Cloud)
Provides scalable computing capacity in Amazon web service cloud. You don’t need to invest in hardware up front etc. It takes few mins to launch your virtual machine and deploy your applications.
You can use preconfigured templates known as Amazon machine images (AMI’s) that includes OS and additional software’s. The launched machines are known as instances and instances comes with various compute configurations such as CPU, Memory known as instance type.
To securely login you need to key pairs where public key is stored with AWS and private key is stored with customers. Key pair choose either RSA or ED25519 types where windows doesn’t support ED25519.
To use a key on mac or Linux computer grant the following permissions:
chmod 400 key-pair-name.pem
Storage volumes for temporary data can use Instance store volumes however when you need permanent data then consider using EBS i.e., Elastic block store.
To secure your Instance consider using security groups.
If you need to allocate the static IP address to an instance, then consider using Elastic address.
Your instance can be EBS backed instance or instance store-based instance that means the root volume can be either EBS or the Instance store. Instance stored backed Instances are either running or terminated but cannot be stopped. Also, instance attributes such as RAM, CPU cannot be changed.
Instances launched from an Amazon EBS-backed AMI launch faster than instances launched from an instance store-backed AMI
When you launch an instance from an instance store-backed AMI, all the parts have to be retrieved from Amazon S3 before the instance is available. With an Amazon EBS-backed AMI, only the parts required to boot the instance need to be retrieved from the snapshot before the instance is available
Use Amazon Inspector to automatically discover software vulnerabilities and unintended network exposure.
Use Trusted advisor to inspect your environment.
Use separate Amazon EBS volumes for the operating system versus your data.
Encrypt EBS volumes and snapshots.
Regularly back up your EBS volumes using EBS Snapshots, create AMI’s from your instance.
Deploy critical applications across multiple AZ’s.
Set TTL to 255 or nearby on your application side so that the connection are intact otherwise it can cause reachability issues.
When you install Apache then you will have document root on /var/www/html directory and by default root user have access to this directory. But if you want any other use to access these files under the directory perform the below steps as below. Let’s assume the user is ec2-user
sudo usermod -a -G apache ec2-user # Logout and login back
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \; # For Future files
Pricing of Amazon Linux 2
There are different plans available for different EC2 instance such as:
On demand Instances: No longer commitments and you only pay per second and the minimum period should be 60 seconds.
Saving Plans: You can book your instance for a year or 3 years.
Reserved instances: You can book your instance for a year or a period of 3 years to a specific configuration.
Spot instances: If you need cheap instance which are unused you can go ahead and use them.
Configure SSL/TLS on Amazon Linux 2
SSL/TLS creates an encrypted channel between a web server and web client that protects data in transit from being eavesdropped on.
Make sure you have EBS backed Amazon Linux 2, Apache installed, TLS Public Key Infrastructure (PKI) relies on DNS. Also make sure to register domain for your EC2 instance.
Nowadays we are using TLS 1.2 and 1.3 versions and underlying TLS library is supported and enabled.
Enable TLS on server by Installing Apache SSL module using below command followed by configuring it.
yum install -y mod_ssl
vi etc/httpd/conf.d/ssl.conf
Generate certificate using
sudo ./make-dummy-cert localhost.crt inside cd /etc/pki/tls/certs
How to add extra AWS EBS Volumes to an AWS EC2 instance
Basically this section is to add the Extra volume to an instance. There are two types of volumes first is root volume and other is extra volume (EBS) which you can add. To add the extra volume on AWS EC2 below are the steps:
Launch one AWS EC2 instance and while launching under Configure storage, choose Add new volume. Ensure that the added EBS volume size is 8 GB, and the type is gp3. AWS EC2 instance will have two volumes one for root and other added storage.
Before modifying or updating the volume, make sure to take the snapshot of current vol by navigating to storage tab under EC2 and then block devices, volume ID.
Now create a file system and attach it to non-mounted EBS volume by running the following command.
You can create an AMI using snapshot or a template.
You can deregister the AMI as well.
AMI’s are either EBS backed or instance backed.
With EBS backed AMI’s the Root volume is terminated and other EBS volume is not deleted.
When you launch an instance from an instance store-backed AMI, all the parts have to be retrieved from Amazon S3 before the instance is available.
With an Amazon EBS-backed AMI, only the parts required to boot the instance need to be retrieved from the snapshot before the instance is available
Cost of EBS backed Instance are less because only changes are stored but in case of Instance store backed instances each time customized AMI is stored in AWS S3.
AMI uses two types of virtualizations: paravirtual (PV) or Hardware virtual machine (HVM) which is better performer.
HVM are treated like actual physical disks. The boot process is similar to bare metal operating system.
The most common HVM bootloader is GRUB or GRUB2.
HVM boots by executing master boot record of root block device of your image.
HVM allows you to run OS on top of VM as if its bare metal hardware.
HVM can take advantage of hardware extensions such as enhanced networking or GPU Processing
PV boots with special boot loader called PV-GRUB.
PV runs on hardware that doesn’t have explicit support for virtualization.
PV cannot take advantage of hardware extensions.
All current, regions, generations support HVM API however this is not true with PV.
The first component to load when you start a system is BIOS in case of [ Intel and AMD] instance types run on Legacy and UEFI and Unified Extensible Firmware Interface (UEFI) in case of Graviton instance. To check the boot mode of an AMI run the below command. Note: To check the boot mode of an Instance you can run the describe instance command.
To check the boot mode of Operating system, SSH into machine and then run the below command.
sudo /usr/sbin/efibootmgr
To set the boot mode you can do that while registering an image not while creating an image.
Shared AMI: These are created by developers and made available for others to use.
You can deprecate or Deregister the AMI anytime.
Recycle Bin is a data recovery feature that enables you to restore accidentally deleted Amazon EBS snapshots and EBS-backed AMIs. Provided you have permissions such as ec2:ListImagesInRecycleBin and ec2:RestoreImageFromRecycleBin
Launch an instance1 using AMI (Marketplace, Your own AMI, Public AMI, Shared AMI)
Customize the instance by adding the software’s etc.
Create new image from customized instance. When you create a new image then you create a new AMI as well. Amazon EC2 creates snapshots of your instance’s root volume and any other EBS volumes attached to your instance
Launch another instance2
Creating an Instance Store backed Linux AMI
Launch an instance1 only from instance backed AMI.
SSH Into Instance, customize it.
Bundle it which contains image manifest and files that contain template for root volume. Bundling might take few minutes.
Next upload the bundle to AWS S3.
Now, register your AMI.
Note 1: To create and manage Instance store backed Linux AMI you will need AMI tools to create and manage instance store-backed Linux AMIs. You will also need AWS CLI and AWS S3 bucket.
Note 2: You can’t convert an instance store-backed Windows AMI to an Amazon EBS-backed Windows AMI and you cannot convert an AMI that you do not own.
Copying an Amazon AMI
You can copy AMI’s within region or across regions
You can also copy AMI along with encrypted snapshot.
When you copy Ami the target AMI has its own identifier.
Make sure your IAM principal has the permissions to copy AMI.
Provide or update Bucket policy so that new AMI can be copied successfully.
You can copy an AMI in another region
You can copy an AMI in another account. For copying the AMI across accounts make sure you have all the permissions such as Bucket permission, key permissions and snapshot permissions.
Storing and restoring an Amazon AMI
You can store AMI’s in AWS S3 bucket by using CreatStoreImageTask API
To monitor the progress of AMI use DescribeStoreImageTask
copy AMI to another bucket.
You can restore only EBS backed AMI’s using CreateRestoreImageTask.
To store and restore AMI the S3 bucket must be in same region.
Amazon Linux 2
It supports kernel 4.14 and 5.10. You can also upgrade it to 5.15 version. It allows greater parallelism and scalability.
New improvements in EXT file system such as large files can be managed easily.
DAMON is better supported as the data access monitoring for better memory and performance analysis.
To install and verify by upgrading kernel use below command.
sudo amazon-linux-extras install kernel-5.15
The cloud-init package is an open-source application built by Canonical that is used to bootstrap Linux images in a cloud computing environment, such as Amazon EC2. It enables you to specify actions that should happen to your instance at boot time.
Amazon Linux also uses cloud-init package to perform initial configuration of the ec2-user account, setting hostname, generate host keys, prepare repositories for package management.
Add users public key,
Amazon Linux uses the cloud-init actions found in /etc/cloud/cloud.cfg.d and /etc/cloud/cloud.cfg. You can create your own cloud-init action files in /etc/cloud/cloud.cfg.d.
AWS Instances
An instance is a virtual server in the cloud. Instance type essentially determines the hardware of the host computer used for your instance. Each instance type offers different compute and memory capabilities.
The root device for your instance contains the image used to boot the instance. The root device is either an Amazon Elastic Block Store (Amazon EBS) volume or an instance store volume.
Your instance may include local storage volumes, known as instance store volumes, which you can configure at launch time with block device mapping
Stop/Start Instance EBS Backed instance:
All the storage and EBS Volumes remains as it is ( they are stopped not deleted).
You are not charged for the instance when it is in stopped stage.
All the EBS volumes including root device usage are billed.
During the instance in stopped stage you can attach or detach EBS volumes.
You can create AMI’s during stopped state and you can also configure few instance configurations such as kernel, RAM Disk and instance type.
The Elastic IP address remains associated from the instance
The instance stays on the same host computer
The RAM is erased
Instance store volumes data is erased
You stop incurring charges for an instance as soon as its state changes to stopping
Reboot AWS EC2 Instance
The instance stays on the same host computer
The Elastic IP address remains associated from the instance
The RAM is erased
Instance store volumes data is preserved
Hibernated Instance ( EBS Backed instance)
The Elastic IP address remains associated from the instance
We move the instance to a new host computer
The RAM is saved to a file on the root volume
Instance store volumes data is erased
You incur charges while the instance is in the stopping state, but stop incurring charges when the instance is in the stopped state
Terminated Instance EBS Backed instance:
The root volume device is deleted but any other EBS volumes are preserved.
Instances are also terminated and cannot be started again.
You are not charged for the instance when it is in stopped stage.
The Elastic IP address is disassociated from the instance
AWS Instance types
General Purpose: These instances provide an ideal cloud infrastructure, offering a balance of compute, memory, and networking resources for a broad range of applications that are deployed in the cloud.
Compute Optimized instances: Compute optimized instances are ideal for compute-bound applications that benefit from high-performance processors.
Memory optimized instances: Memory optimized instances are designed to deliver fast performance for workloads that process large data sets in memory.
Storage optimized instances: Storage optimized instances are designed for workloads that require high, sequential read and write access to very large data sets on local storage. They are optimized to deliver tens of thousands of low-latencies, random I/O operations per second (IOPS) to applications
Note: EBS-optimized instances enable you to get consistently high performance for your EBS volumes by eliminating contention between Amazon EBS I/O and other network traffic from your instance.
You can enable enhanced networking on supported instance types to provide lower latencies, lower network jitter, and higher packet-per-second (PPS) performance
AWS Instance Lifecycle
Note: You cannot stop and then start an Instance store backed instance.
FROM AMI
Launch Instance
Pending
Running to Rebooting or Stopping
Shutting Down
Terminated
Amazon EC2 instances support multithreading, which enables multiple threads to run concurrently on a single CPU core. Each thread is represented as a virtual CPU (vCPU) on the instance. An instance has a default number of CPU cores, which varies according to instance type. For example, an m5.xlarge instance type has two CPU cores and two threads per core by default—four vCPUs in total.
Number of CPU cores: You can customize the number of CPU cores for the instance. You might do this to potentially optimize the licensing costs of your software with an instance that has sufficient amounts of RAM for memory-intensive workloads but fewer CPU cores.
Threads per core: You can disable multithreading by specifying a single thread per CPU core. You might do this for certain workloads, such as high performance computing (HPC) workloads.
Monitoring AWS EC2 instance
You can monitor AWS EC2 instances either manually or automatically. Lets discuss few of Automated monitoring tools.
System status checks
Instance status checks
Amazon Cloud watch alarms
Amazon Event Bridge
Amazon CloudWatch Logs
Cloud Watch agent
Now, lets discuss few of manual tools to monitor AWS EC2 instance.
Amazon EC2 Dashboard.
Amazon Cloud Watch Dashboard
Instance Status Checks on the EC2 Dashboard.
Scheduled events on EC2 Dashboard.
Cloud-init
It is used to bootstrap the Linux images in cloud computing environment. Amazon Linux also uses cloud-init to perform initial configuration of the ec2-user account. Amazon Linux uses the cloud-init actions found in /etc/cloud/cloud.cfg.d and /etc/cloud/cloud.cfg and you can also add your own actions in this file.
The tasks that are performed by default by this script.
Set the default locale.
Set the hostname.
Parse and handle user data.
Generate host private SSH keys.
Add a user’s public SSH keys to .ssh/authorized_keys for easy login and administration.
Prepare the repositories for package management.
Handle package actions defined in user data.
Execute user scripts found in user data.
AWS EC2 Monitoring
By default, AWS EC2 sends metrics to CloudWatch every 5 mins.
To send metric data for your instance to CloudWatch in 1-minute periods, you can enable detailed monitoring on the instance but You are charged per metric that is sent to CloudWatch.
To list all the metrics of a particular AWS EC2 instance use the below command.
To create CloudWatch alarms, you can Select the instance and choose Actions, Monitor and troubleshoot, Manage CloudWatch alarms.
You can use Amazon EventBridge to automate your AWS services and respond automatically to system events, such as application availability issues or resource changes.
Events from AWS services are delivered to Event Bridge in near real time. For example: Activate a Lambda function whenever an instance enters the running state. Create events and rules on event on AWS EC2 service. Once generated then it will run the lambda function.
You can use the Cloud Watch agent to collect both system metrics and log files from Amazon EC2 instances and on-premises servers
sudo yum install amazon-cloudwatch-agent
AWS EC2 Networking
If you require a persistent public IP address, you can allocate an Elastic IP address for your AWS account and associate it with an instance or a network interface.
To increase network performance and reduce latency, you can launch instances in a placement group
To increase network performance and reduce latency, you can launch instances in a placement group.
Local Zones
A Local Zone is an extension of an AWS Region in geographic proximity to your users. Local Zones have their own connections to the internet and support AWS Direct Connect, so that resources created in a Local Zone can serve local users with low-latency communications.
AWS Wavelength
AWS Wavelength enables developers to build applications that deliver ultra-low latencies to mobile devices and end users. Wavelength deploys standard AWS compute and storage services to the edge of telecommunication carriers’ 5G networks. Developers can extend a virtual private cloud (VPC) to one or more Wavelength Zones, and then use AWS resources like Amazon EC2 instances to run applications that require ultra-low latency and a connection to AWS services in the Region.
Elastic Network Interface
Eni is basically a Virtual Network adapter which contains following attributes:
1 primary private IPv4
1 or more secondary private IPv4
1 Elastic IP per private IP
One Public IPv4 address
1 Mac address
You can create and configure network interfaces and attach them to instances in the same Availability Zone.
The below diagram is just the one ENI ( Network card adapter) however for some of them have multiple adapters.
Each instance has a default network interface, called the primary network interface.
Each instance has a default network interface, called the primary network interface.
Instances with multiple network cards provide higher network performance, including bandwidth capabilities above 100 Gbps and improved packet rate performance. All the instances have mostly one network card which has further ENI’s.
The following instances support multiple network cards.
You can attach a network interface to an instance when it’s running (hot attach), when it’s stopped (warm attach), or when the instance is being launched (cold attach).
You can detach secondary network interfaces when the instance is running or stopped. However, you can’t detach the primary network interface.
Configure your network interface using ec2-net-utils for Amazon Linux
There is an additional script that is installed by AWS which is ec2-net-utils. To install this script, use the following command.
yum install e2-net-utils
To list the configuration files that are generated can be checked using the below command:
ls -l /etc/sysconfig/network-scripts/*-eth?
IP Address
You can specify multiple private IPv4 and IPv6 addresses for your instances.
You can assign a secondary private IPv4 address to any network interface. The network interface does not need to be attached to the instance.
Secondary private IPv4 addresses that are assigned to a network interface can be reassigned to another one if you explicitly allow it.
Secondary private IPv4 addresses that are assigned to a network interface can be reassigned to another one if you explicitly allow it.
Although you can’t detach the primary network interface from an instance, you can reassign the secondary private IPv4 address of the primary network interface to another network interface.
Each private IPv4 address can be associated with a single Elastic IP address, and vice versa.
When a secondary private IPv4 address is reassigned to another interface, the secondary private IPv4 address retains its association with an Elastic IP address.
When a secondary private IPv4 address is unassigned from an interface, an associated Elastic IP address is automatically disassociated from the secondary private IPv4 address.
Assign a secondary private IPv4 address
In EC2, choose Network Interfaces
Allow secondary IP address.
Again verify in EC2 instance networking tab
What is Elastic IP address?
Static Ip address
It is region specific and cannot be moved to another region.
First thing is to allocate to the account.
When you associate an Elastic IP address with an instance, it is also associated with the instance’s primary network interface
Associate an Elastic IP address with the secondary private IPv4 address
In the navigation pane, choose Elastic IPs.
Again verify in EC2 instance networking tab
Conclusion
In the long ultimate guide we learned everything one must know about AWS EC2 in the AWS Cloud.
Are you new to Cloud, if yes then consider learning the most widely used open-source tool to automate your infrastructure using Terraform the Infrastructure as a code tool?
In this tutorial, you’ll learn how to Install and set up Terraform on Windows Machine step by step.
Terraform is a tool for building, versioning, and changing the infrastructure. Terraform is Written in GO Language, and the syntax language of configuration files is HashiCorp configuration language(HCL) which is much easier than yaml or json.
Any Windows Machine should work but this tutorial will use Windows 10 Machine.
Notepad or Notepad ++ or Visual Studio code editor on your windows Machine to create terraform configuration files. To install visual code studio click here.
Now that you have a basic idea about terraform let’s kick off this tutorial by first installing terraform on a Windows machine.
First open your favorite browser and download the appropriate version of Terraform from HashiCorp’s download Page. This tutorial will download terraform 0.13.0 version but you will find latest versions on the Hashicorps download page.
Downloading Terraform from Hashicorp website
Make a folder on your C:\ drive where you can put the Terraform executable something Like C:\tools where you can put binaries.
Downloading Terraform binary on the local machine
Extract the zip file to the folder C:\tools
Extracting the Terraform binary executable
Now Open your Start Menu and type in “environment” and the first thing that comes up should be Edit the System Environment Variables option. Click on that and you should see this window.
Editing the System Environment Variables option.
Now under System variables look for Path and edit it.
Editing the Path with Terraform binary location
ClickNew and add the folder path where terraform.exe is located to the bottom of the list. By adding the terraform.exe in PATH will allow you to execute terraform command from anywhere in the system.
Updating the Windows Path with Terraform binary location
Click OK on each of the menus and further open command prompt or PowerShell to check if terraform is properly added in PATH by running the command terraform from any location.
Terraform command on command Prompt in Windows Machine
Terraform command on PowerShell in Windows Machine
Verify the installation was successful by entering terraform --version. If it returns a version, you’re good to go.
Running the terraform –version command
Creating an IAM user in AWS account with programmatic access
There are two ways to connect to an AWS account, the first is providing a username and password on the AWS login page and another is configuring the Access key ID and secret keys of IAM users in AWS CLI to connect programmatically.
Earlier, you installed AWS CLI successfully on a Windows machine, but you will need an IAM user with programmatic access to run commands from it.
While in the Console, click on the search bar at the top, search for ‘IAM’, and click on the IAM menu item.
Checking the IAM AWS service
To Create a user click on Users→ Add user and provide the name of the user myuserand make sure to tick the Programmatic access checkbox in Access type which enables an access key ID and secret access key and then hit the Permissions button.
Adding the IAM user in AWS CLoud
Now select the “Attach existing policies directly” option in the set permissions and look for the “Administrator” policy using filter policies in the search box. This policy will allow myuser to have full access to AWS services.
Attaching the admin rights to IAM users in AWS CLoud
Finally click on Create user.
Now, the user is created successfully and you will see an option to download a .csv file. Download this file which contains IAM users i.e. myuserAccess key ID and Secret access key which you will use later in the tutorial to connect to AWS service from your local machine.
Downloading the AWS credentials of IAM user
Configure AWS credentials using aws configure in AWS CLI
You are an IAM user with Access key ID and secret keys, but AWS CLI cannot perform anything unless you configure AWS credentials. Once you configure the credentials, AWS CLI allows you to connect to the AWS account and execute commands.
Configure AWS Credentials by running the aws configure command on command prompt.
aws configure
Enter the details such as AWS Access key ID, Secret Access Key, region. You can skip the output format as default or text or json .
Configure AWS CLI using aws configure command
Once AWS is configured successfully , verify by navigating to C:\Users\YOUR_USER\.aws and see if two file credentials and config are present.
Checking the credentials file and config on your machine
Now open both the files and verify and you can see below you’re AWS credentials are configured successfully using aws configure.
Checking the config file on your machine
Checking the config file on your machine
Verify aws configure from AWS CLI by running a simple commands
Now, you can test if AWS Access key ID, Secret Access Key, region you configured in AWS CLI is working fine by going to command prompt and running the following commands.
aws ec2 describe-instances
Describing the AWS EC2 instances using AWS CLI
You can also verify the AWS CLI by listing the buckets in your acount by running the below command.
aws cli s3
Creating AWS EC2 Instance Using Terraform
In this demonstration, you will learn how to create Amazon Web Service (AWS) EC2 instance using Terraform commands on a Windows machine. Lets dive in.
First, create a folder Terraform-EC2-simple-demo on your desktop or any location on Windows Machine.
Now create a file main.tf inside the folder you’re in and copy/paste the below content.
resource "aws_instance" "my-machine" { # This is Resource block where we define what we need to create
ami = var.ami # ami is required as we need ami in order to create an instance
instance_type = var.instance_type # Similarly we need instance_type
}
Create one more file named vars.tf file under Terraform-EC2-simple-demo folder and copy/paste the content below. The vars.tf file contains the variables that you referred in main.tf file.
variable "ami" { # We are declaring the variable ami here which we used in main.tf
type = string
}
variable "instance_type" { # We are declaring the variable instance_type here which we used in main.tf
type = string
}
To select the the image ID ( ami ), navigate to the LaunchInstanceWizard and search for ubuntu in the search box to get all the ubuntu image IDs. This tutorial will use Ubuntu Server 18.04.LTS image.
Choosing the Amazon Machine Image
Create one more file output.tf file under Terraform-EC2-simple-demomo folder and paste the content below. This file will allow Terraform to display he output after running terraform apply command.
output "ec2_arn" {
value = aws_instance.my-machine.arn
}
Create provider.tf file under Terraform-EC2-simple-demo folder and paste the content below.
provider "aws" { # Defining the Provider Amazon as we need to run this on AWS
region = "us-east-2"
}
Create terraform.tfvars file under Terraform-EC2-simple-demo folder and paste the content below. This file contains the value of Terraform vaiables declared in vars.tf file.
ami = "ami-013f17f36f8b1fefb"
instance_type = "t2.micro"
Now your files and code are ready for execution and the folder structure should look something like below.
folder structure of terraform configuration files
Now your files and code are ready for execution. Initialize the terraform using the terraform init command.
terraform init
Initializing the terraform using the terraform init command.
Terraform initialized successfully , now its time to run the plan command which provides you the details of the deployment. Run terraform plan command to confirm if correct resources is going to provisioned or deleted.
terraform plan
Running the terraform plan command
After verification, now its time to actually deploy the code using terraform apply command.
terraform apply
Running the terraform apply command
Great Job; terraform commands were executed successfully. Now you should have the AWS EC2 instance launched in AWS Cloud.
Verifying the AWS instance
Generally takes a minute or so to launch an instance, and yes, you can see that the instance is successfully launched now in the us-east-2 region as expected.
Conclusion
In this tutorial, you learned What is terraform, how to Install Terraform on the Windows machine and launch an ec2 instance on an AWS account using terraform.
Now that you have the AWS EC2 instance launched, what are you planning to deploy next using Terraform?
Are you spending unnecessary money in AWS Cloud by keeping unused AWS EC2 instances in running states? Why not stop the AWS EC2 instance and only start when required by running a single Shell Script?
Multiple AWS accounts contain dozens of AWS EC2 instances that require some form of automation to stop or start these instances, and to achieve this, nothing could be better than running a shell script.
In this tutorial, you will learn step by step how to Start and Stop AWS EC2 instance in AWS account using Shell script.
Shell Script is a text file containing lists of commands executed on the terminal or shell in one go in sequential order. Shell Script performs various important tasks such as file manipulation, printing text, program execution.
Shell script includes various environmental variables, comments, conditions, pipe commands, functions, etc., to make it more dynamic.
When you execute a shell script or function, a command interpreter goes through the ASCII text line-by-line, loop-by-loop, test-by-test, and executes each statement as each line is reached from top to bottom.
What is AWS EC2 instance?
AWS EC2 stands for Amazon web service elastic compute cloud. AWS EC2 is simply a virtual server that gets launched in quick time, and you don’t need to worry about the hardware. After the AWS EC2 instance is launched, you can deploy highly scalable and available applications.
There are some important components in AWS EC2 instance such as:
AWS EC2 AMI
AWS EC2 contains preconfigured templates known as AMI ( Amazon Machine Image ) that include an operating system and software configurations that are highly required. Using these preconfigured templates you can launch as many AWS EC2 instances.
You can configure your own software’s and data you wish to have when an instance on top of Preconfigured templates.
Amazon Machine Image template
AWS EC2 instance type
AWS EC2 contains various AWS EC2 instance types with different CPU and memory configurations such as t2.micro, t2.medium, etc.
AWS EC2 instance type
Amazon EC2 key pairs
AWS EC2 instance allows you to log in to these launched instances with complete security by creating a Keypair where one of the keys is public that remains within the AWS account, and another is the private key that remains with the owner of the instance.
AWS EC2 EBS Storage
AWS EC2 allows you to add two kinds of storage that is ec2 instance store volumes which are temporary storage, and Elastic block storage (AWS EBS), the permanent storage.
AWS EC2 is launched with root device volume ( ec2 instance store volumes or AWS EBS ) that allows you to boot the machine.
AWS EC2 EBS Storage
AWS EC2 instance state
AWS EC2 service provides various states of a launched instance such as stopped, started, running, terminated. Once the instance is terminated, it cannot be restarted back.
AWS EC2 instance state
Prerequisites
AWS account to create ec2 instance. If you don’t have AWS account please create from AWS account or AWS Account
Windows 7 or plus edition where you will execute the shell script.
Code editor for writing the shell script on windows machine such as visual studio code. To install visual studio click here.
Building a shell script to start and stop AWS EC2 instance
Now that you have a good idea about the AWS EC2 instance and shell script but let’s learn how to build a shell script to start and stop the AWS EC2 instances.
Create a folder of your windows machine at any location. Further under the same folder create a file named start-stop-ec2.sh and copy/paste the below code.
# /usr/bin/bash
set -e # set -e stops the execution of a script if a command or pipeline has an error
id=$1 # Provide the instance ID with the name of the script
# Checking if Instance ID provided is correct
function check_ec2_instance_id () {
if echo "$1" | grep -E '^i-[a-zA-Z0-9]{8,}' > /dev/null; then
echo "Correct Instance ID provided , thank you"
return 0
else
echo "Opps !! Incorrect Instance ID provided !!"
return 1
fi
}
# Function to Start the instance
function ec2_start_instance () {
aws ec2 start-instances --instance-ids $1
}
# Function to Stop the instance
function ec2_stop_instance () {
aws ec2 stop-instances --instance-ids $1
}
# Function to Check the Status of the instance
function ec2_check_status () {
aws ec2 describe-instances --instance-ids $1 --query "Reservations[].Instances[].State.Name" --output text
}
# Main Function
function main () {
check_ec2_instance_id $1 # First it checks the Instance ID
echo " Instance ID provided is $1" # Prints the message
echo "Checking the status of $1" # Prints the message
ec2_check_status $1
# Checks the Status of Instance
status=$(ec2_check_status $id) # It stores the status of Instance
if [ "$status" = "running" ]; then
echo "I am stopping the instance now"
ec2_stop_instance $1
echo "Instance has been stopped successfully"
else
echo "I am starting the instance now"
ec2_start_instance $1
echo "Instance has been Started successfully"
fi
}
main $1 # Actual Script starts from main function
Executing the Shell Script to Stop AWS EC2 instance
Previously you created the shell script to start and stop the AWS EC2 instance, which is great; but it is not doing much unless you run it. Let’s execute the shell script now.
Open the visual studio code and then open the location of file start-stop-ec2.sh.
Opening Shell script on visual studio code
Finally execute the shell script.
./start-stop-ec2.sh <Instance-ID> # Provide the EC2 instance ID along with script
Executing the shell script to stop the AWS Ec2 instance
Verifying the Stopped AWS EC2 instance
Earlier in the previous section, the shell script ran successfully; let’s verify the if AWS EC2 instance has been stopped from running state in the AWS account.
While in the Console, click on the search bar at the top, search for ‘EC2’, and click on the EC2 menu item and you should see the instance you specified in shell script has stopped now.
Viewing the stopped AWS EC2 instance
Executing the Shell Script to Start AWS EC2 instance
Now thaYouuccessfully stopped and verified the AWS EC2 instance in the AWS cloud. This time let’s restart the instance using the same script.
./start-stop-ec2.sh <Instance-ID> # Provide the EC2 instance ID along with script
Executing the shell script to start the instance
Verifying the Running AWS EC2 instance
Similarly, in this section, let’s verify the if AWS EC2 instance has been restarted successfully in the AWS account.
Viewing the running AWS EC2 instance
Conclusion
In this tutorial, you learned what is Amazon EC2 and learned how to start or stop AWS EC2 using shell script on AWS step by step. It is always a good practice to turn off your lights when you leave your home or room, similarly do for EC2 instances.
So which AWS EC2 instance are you planning to stop going further and save dollars?