Enterprise Java

How to Launch an EC2 Instance in AWS

Hello Friends,

In this tutorial,we will see how we can spin an EC2 instance in AWS in no time.

Prerequisite :

You should have access to the AWS console.If you don’t have AWS account already,you can click  here and and create free tier Account on AWS.

How to Launch an EC2 instance in AWS

Step 1 :

Login to Your AWS account with your credentials.

Step 2 :

Click on Services and you will see following screen

Step 3 :

Click on EC2 and you will see following screen :

Step 4 :

Click on Launch Instance and you will see following screen.Here basically you will find all available AMIs.

As per Amazon “An AMI is a template that contains the software configuration (operating system, application server, and applications) required to launch your instance. You can select an AMI provided by AWS, our user community, or the AWS Marketplace; or you can select one of your own AMIs.”

If you want to see  “Free tier Only” AMIs,just check the checkbox in front of “Free tier only”.

Step 5 : 

We will select Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type
 – ami-08d489468314a58df AMI by clicking on Select button in front of it.

It has following configuration :

The Amazon Linux AMI is an EBS-backed, AWS-supported image. The default image includes AWS command line tools, Python, Ruby, Perl, and Java. The repositories include Docker, PHP, MySQL, PostgreSQL, and other packages.

On Clicking Select,you will get following screen :

Here you have option to choose Instance Type.

As per Amazon,

 “Amazon EC2 provides a wide selection of instance types optimised to fit different use cases. Instances are virtual servers that can run applications. They have varying combinations of CPU, memory, storage, and networking capacity, and give you the flexibility to choose the appropriate mix of resources for your applications”.

For detailed list of various Instance Types available in AWS,please check 
EC2 Instance Types.It tells you features of various Instance types and use case where they can be used.

Step 6 :

We will choose the default one,which is t2.micro ,which is from General Purpose family and is free tier.Click on “Next: Configure Instance Details” button and you will see following screen.

Some Important properties from above screen,we will discuss below.

Number of Instances : By default it will be populated as 1,which means that only 1 EC2 instance will be created.You can create more than 1 EC2 instance by mentioning number greater than 1 here.Also if you want to launch your EC3 instance in Auto Scaling Group,you can click on “Launch into Auto Scaling Group” and configure it to be launched in Auto scaling group.

Network : By default it is showing here the default VPC created by AWS.If you want to associate this EC2 instance with VPC created by you,you can click on “create New VPC” and then choose that VPC name.Alternatively it is also possible(Which is normally the case) that you first create your VPC and then come to this screen for Launching EC2 instance,in that case you will see your VPC name as well in the drop down and you can choose your VPC.

Note : Once you create an account in AWS,AWS creates

– A default VPC in each region for your account with size /16 IPv4 CIDR block 172.31.0.0/16.This provides 65536 private IP addresses.

– Create a size /20 subnet in each availability zone.This provides around 4096 IP addresses in each subnet.

– Create a internet Gateway and connect it to your default VPC.

– Create a default Security group and associate it with your default VPC.

– Create a default Network access control list(NACL) and associate it with your default VPC.

– Associate the default DHCP options set with default VPC.

– main route table

Subnet : Here you are given option to Launch your EC2 instance in a specific subnet or you have option to choose “No preference”,in that case AWS will choose one of the subnet to launch your EC2 instance.

Auto assign Public IP : As default subnets are always public subnet,you will see that Auto assign Public IP option in enabled by default.Having public IP for your EC2 instance means your EC2 instance can communicate with internet.Also with public IP addresses,they are disassociated with instance once instance is stopped or restarted which means every time you restart your EC2 instance,your EC2 instance will get new IP address.

Enable termination Protection : You can protect your EC2 instance from being accidentally terminated via API or console,if you enable this protection.

Step 7 :

Click on Add Storage and you will see following screen.Here you will see the default storage provided by AWS for your t2.micro instance.The storage is named as EBS or Elastic block store.You can think of it as hard disk of your computer.Apart from this default storage you can add more EBS volumes by clicking on “Add New Volume”.

Let us see one of the important property to discuss :

Delete on Termination : By default this,this option is turned on,which means that if you will terminate your EC2 instance,your EBS storage will also be deleted,so you will loose all your data on the storage.If you don’t want so,you can uncheck this option.

Step 8 :

Click on “Next: Add tags” and you will see following screen :

Tag is basically key value pair which you can use to assign some name to your EC2 instance.For example you might be spinning this EC2 instance to use it as web server, in that case you can use key as “Name” and value as “Web Server”.

Let us go ahead and add tag by clicking on Add tag.

Step 9 : 

Click on “Next :Configure Security Group” button and you will see following screen :

Security group basically define what traffic is allowed into your EC2 instance and what traffic is allowed out from your EC2 instance.

By default as you can see there is one entry which means that your EC2 instance can be connected from any where on the internet(0.0.0.0/0) using SSH and TCP protocol on port 22.

Also,you can see there is warning,which says that “
Rules with source of 0.0.0.0/0 allow all IP addresses to access your instance. We recommend setting security group rules to allow access from known IP addresses only.”

Which in other words is trying to say that this security is not a safe as anyone from internet can connect to your instance.so what you can do is configure more rules in security group which are more constrained.

Step 10 :

Click on “Review and Launch button and you will see following screen :

Step 11 :

Click on “Launch” button and you will get following pop up.As you can see this pop up is for creating Key pair which you can use then to SSH into your EC2 instance.This is to make sure that only authorised person with valid private key only can access EC2 instance.

Give any name to key pair like “EC2Keypair”  and click on “Download Key Pair” button.It will save key pair on your local machine.

Step 12 :

Click on “Launch Instances” button and you will see following screen :

Step 13 :

Click on “View Instances” and you will see that your EC2 instance is spinning up and is in pending status

Step 14 :

Wait for 1-2 minutes and you will see that your EC2 instance is in running state

Step 15 :

Make sure that you stop your EC2 instance,if you don’t want to use it and have created for learning purpose only,because there is cost involved if your EC2 instance keeps on running.Actually you should terminate it,to avoid the cost(of course if you don’t want to use it any more).

For stopping ,go to Action -> Instance State -> Stop

For terminating,go to Action -> Instance State -> Terminate

Summary :

In this tutorial,we saw that how we can spin an EC2 instance quickly by going through following configurations :

Choosing Amazon Machine Image (AMI)

Choosing Instance Type

Configuring Instance details

Adding Storage

Adding Tag

Adding Security Group

Creating KeyPair and saving it

Please feel free for any comments,Questions or to share it with someone you feel it might be helpful.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to Launch an EC2 Instance in AWS

Opinions expressed by Java Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button