Part 1: Creating an EC2 instance in AWS for deploying a Django application:
If you don’t have an AWS account then create one now and you will enjoy free tier services for 1 year.
First login to AWS then Click on services on the nav-bar and under the Compute domain of AWS Services click on the EC2 service. It will redirect you to the EC2 dashboard. Under the resources, you can see all the information about the EC2 instances. If you have no instances then all value will be 0.
Now to create an instance you have to click to Launch instance under the create instance section. On clicking, it will request to finish some setup steps for your instances like below:
Step 1: Choose an Amazon Machine Image (AMI)
In this step, you should choose the image you want your instance will be. An image is simply an Operating system Image with minimal tools installations like Ubuntu, Windows. So that you can use a fresh Setup. Choose any OS appropriate for you. For our case, we want to use Ubuntu OS so we will select Ubuntu 18.04.
Step 2: Choose an Instance type:
As per your requirement, you can choose your required instance type from the various instance family. If you need more computing power you can go for instances with higher Specifications. In our case we are going to use the free tier so we will go with t2.micro. Its performance is good. In the future, if you want to change the instance specification you can do that as well.
After selecting the instance type click on Review and launch.
Step 3: Edit Security groups:
As we want our instance to communicate with HTTP protocol we have to give permission in security groups for that under Security groups click on Edit security groups.
Now you will see there will be only SSH Protocol is available because you have to take the remote using SSH. Now click on Add Rule and select type as HTTP and change source as Anywhere.
Now click on Review and launch
Step 4: Launch Instance:
Now review all the instance specifications and click on launch.
Step 5: Select a key pair:
What is a key pair?
A key pair consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely.
Keep this key pair securely, if you miss the key pair then you can’t access your instance.
NOTE: Here you can choose your existing key pair if available. Or you can create a new key-pair. We are going to create a new key pair so click on the Dropdown and select Create a new key pair. Enter the name you want to give to the key pair and download the key pair.
Finally, click on Launch Instances
Wait for some time it will show you confirmation page of your instance creation, you can view all of your instances by clicking on view instances.
As you can see there will be your instance which will be initializing. This is the end of the instance creation process. Now you can give a name to your instance if you want to.
Connect to an EC2 instance:
We have created our instance, Now we have to take the instance on remote and make the environment ready for the deployment. For that, we need to connect to the Instance. For connecting to the Instance just choose the instance on the Instance list and click on the connect button on top of the instance lists. It will show the steps of how can you connect to the instance.
Note: In my case, I am using Linux so I can directly access SSH client. But if you have a window then you can connect using putty or other SSH providers.
Step 1: Locate your private key file and open SSH:
Now open the folder where you have placed your private key. Open terminal from that location.
Step 2: Change the access mode of the key:
chmod 400 Key_name.pem
Your key must not be publicly viewable for SSH to work. To change the access mode of the key by using the following commands.
NOTE: Change the Key_name with your key name.
Step 3: Connect to your instance using its Public DNS:
Now copy the command from the Example and paste it on your Terminal and hit enter. For the first time, it will ask your permission to connect type yes and hit enter.
After a successful connection, you will see your terminal will change to the IP of your instance. And if you want to exit from the terminal then you can, by using the command exit.