In this article, let me explain how you can create an Linux EC2 instance in AWS and connect to it with Elastic IP. As you might know, AWS is a popular cloud platform and EC2 is Infrastructure As Service( IaaS ) Cloud solution from AWS. Basically EC2 instance is a virtual machine which can be launched on desired OS as per requirement. You can create an Linux EC2 instance within few minutes in AWS. If you don’t have an AWS account yet, please create one from here >>
This is a video demonstration of this article. Please
Step1 – Create Linux EC2 instances in AWS:
Login to AWS EC2 dashboard at https://console.aws.amazon.com/ec2/ and Click Launch Instance .
Select AMI :
On the next page, select Amazon Machine Image( AMI ). AMI is an OS image provided by AWS. There are AMIs of Ubuntu, CentOS even Windows. Select your desired AMI and click Select . I selected free tier Ubuntu Server 16.04 LTS.
Select Instance Type:
On the next page, you need to select the instance type . In this step, you are actually selecting the hardware resources for your instance. Select a instance type based on CPU, Memory and other resources. AWS bill you per hour. So don’t forget to check the cost here >>. I selected t2.micro which is free of cost. Once done, click Next: Configure Instance Details .
On the next page, You can keep the default settings. JFYI, if you want the instance to be launched in a specific VPC (VPC is a just an another name for network in AWS), you can select it from the drop down of Network option. Click Next:Add Storage .
Add EBS volume:
On the next page, you can add EBS volume to your instance. EBS volume is nothing but an external storage you can mount to your instance. This is useful if you have a lot of files like logs to store. Proceed further by clicking Next: Add Tags .
Add Instance Tags:
On the next page, you can add tags for your instance. Tags are key/value pair. For example, if your instance is in production environment, you can add “env” and “production” as key and value respectively. Once done, click on Next: Configure Security Group .
Add Security Group:
On this page, you are asked to configure Security Group for your instance. Security Group is a external firewall of your instance. Network packets reaches your instance only through this security group. Security group deny all requests by default, so you need to add specific rules for your instance. If you would like to setup a webserver on your instance, make sure 80/443 port is added in the security group. Put source IP as 0.0.0.0/0,::/0 so that anybody from the Internet(both IPv4 and IPv6 ) can access your webserver.
Add SSH key pair:
Click Review and Launch , and then Launch . A popup will be shown asking you to create SSH key pair. If you don’t have any existing key with AWS, you can create a new one. Just give a key pair name and click on Download Key Pair button. Keep the downloaded PEM key safe somewhere. Feel free to check the video tutorial if you have any doubts.
Once done, Click Launch Instances and make note of the instance ID . Instance ID is an unique ID given to the instance by AWS. Paste the instance ID on the search bar to find the instance. Initially the Instance State would be pending , then it will turn to running state in sometime.
Step2 – Attach Elastic IP to Linux EC2 instance:
Let’s connect to the Linux EC2 instance. How do we do it? We need IP address of the Linux EC2 instance to do that. AWS has given you a random IPv4 Public IP when you created the Linux EC2 instance. But you cannot trust this IP address because this IP change once you reboot the instance. So what do we do here? We cannot use IP address which is not stable 🙁 . AWS has something called Elastic IP which is free of cost. Once you allocate an Elastic IP address for your instances, it won’t change until you change it! . Let’s make use of it 🙂
To do this, Click on Elastic IPs under NETWORK AND SECURITY in the left sidebar and then click Allocate New Address .
Then click Allocate . Once an IP address is allocated, Click on the IP address as shown below. Also, make a note of the IP address.
Now Click Actions > Associate address . You can associate the Elastic IP address to either Instance or Network Interface. In our case, it is Instance.
On the next page, Select Instance ID and Private IP address of the Instance and then click Associate .
Step3 – Connect to the Linux EC2 instance:
Now we have the IP address and SSH PEM file. Let’s connect to the Linux instance. Assume easyaslinux.pem and 18.104.22.168 are the SSH PEM filename and Elastic IP address respectively. Now open your terminal and run below commands.
Let’s make the SSH PEM file readable only by the user.
chmod 400 easyaslinux.pem
Now access your Linux instance with SSH by running below command.
ssh -i easyaslinux.pem firstname.lastname@example.org
The default user varies according your Linux AMI.
- For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user .
- For a Centos AMI, the user name is centos .
- For a Debian AMI, the user name is admin or root .
- For a Fedora AMI, the user name is ec2-user or fedora .
- For a RHEL AMI, the user name is ec2-user or root .
- For a SUSE AMI, the user name is ec2-user or root .
- For an Ubuntu AMI, the user name is ubuntu .
Hurray, You have completed the course :). Now start working on your Linux box. Terminate or Stop whenever needed. Just a side note, your data will be lost if you terminate your EC2 instance.
Let me come back with other useful articles soon. Subscribe to this blog so that you don’t miss out anything useful (Checkout Right Sidebar for the Subscription Form) . Please also put your thought on this article as a comment .