How to Provision a Linux EC2 Instance (server) on AWS Step-by-step
Published: 2022-10-30 17:58:52

Have you ever needed a server to deploy a web app, complete a lab or something else? I have had need to do both, and AWS always came through for me.

In this tutorial, you will learn how to set up and connect to your own AWS Linux EC2 instance to meet your needs for a server.

Let's go...

### What is an EC2 Instance?
An Amazon EC2 _instance_ is a virtual server in the AWS cloud. With the Amazon EC2 service, you can configure your instance to run operating system or applications of your choice.

### Overview
When launching an instance, you have to secure it using security groups(which acts like a virtual firewall to control traffic to and from your instance) and key pairs(a proof of identity).

Here is an architectural diagram of what we will be setting up:

![ec2 setup architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcqogj2i5yre7uh0o0eg.png)

### Prerequisites:
- An AWS account
- Create key pair and security group by following instructions found [here](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/get-set-up-for-amazon-ec2.html)

>To avoid being billed unnecessarily, please ensure to terminate you instance when you complete this tutorial. Instructions for that are included in the last step here.

### Step 1: Launch Your Instance
There are several ways to launch an EC2 instance, but in this tutorial, we will use the AWS console.

Here are the steps:
- Open the EC2 console at [AWS console link]( https://console.aws.amazon.com/ec2/)

- From the dashboard, click on **Launch instance**

![Launch Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5e1w3e8fdr3vcflcpyfn.png)

- Next, enter a name for your server.

- Under the **Application and OS Images (Amazon Machine Image)**, select the "Quick Start" tab and select the Ubuntu image. This will be the OS for your instance.

- Under the Amazon Machine Images(AMI) section, pick an option with the "free tier eligible" label for now.

![Select image and AMIs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65cnne6gq7q9xbrf16uf.png)

- Next, under **Instance type**, choose an option that has "free tier eligible" too. These instance types are hardware configurations for your server.

- Under **Key pair(login)** choose a key pair you create before, or create one at this step.

>Warning: If you launch your instance without a key pair, then you can't connect to it.


![Instance type and key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5u4i8th6ovqc70i9eyso.png) 

- Next, we have to configure **Network settings**
    - Choose "select existing security group"
    - From the "Common security groups" dropdown, pick the security group you configured earlier from the **Prerequisites** section of this blog. If you didn't do that, go [here](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/get-set-up-for-amazon-ec2.html).

- In order to easily access your access your instance, check to see whether the "Auto-assign public ip" is enabled. If not, enable it to allow a public Ip to be associated with your instance when it is launched.


![security group and public IP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogpr8q9uaslb7ygml95f.png)

- Keep the other default selections, review the configurations for your instance and confirm instance launch.

- On the **Instances** screen, you could monitor the status of your instance. It may take some time for the instance to launch. Just wait until the **state** changes to "running."
- Also wait for the **Status check** to pass before attempting to connect to it.

### Step 2: Connect to Your Instance (via SSH)
>You can't connect to your instance unless:
> - You have the ```.pem``` file for the key pair you used to create the instance.

> - Your security group is configured to allow you to connect via SSH from your computer.

>...so ensure that you have these setup correctly.

- In the **Instances** page, select your instance and copy the Public IP address.


![Copy public IP Address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9tqmu8dyt85qgxq5n5a.png)

- Head over to your terminal and let's connect to our instance.

The syntax for connecting to the instance is:
``` bash
$ ssh -i <path_to_pem_file> user@<Instance_public_ip>
```

In my case, it was:
``` bash
$ ssh -i ~/Downloads/Davi-test.pem ubuntu@54.175.160.84
```
Successful connection should look something like this:

![SSH connection successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cp36g2ou87ipvmhr0h1z.png)

> - If ssh complains about the key permission, ensure to change the permission of the .pem file to 400, using chmod. For example, by running: ```sudo chmod 400 <path_to_pem_file>)```

> - If you still can't connect to your instance, see [Troubleshoot Connecting to Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html) for assistance.



### Step 3: Clean Up Your Instance
- Head over to your EC2 instances page and select the instance you just created.
- Click on the "Instance state" dropdown and select **Terminate instance**.
- Amazon will then shutdown and terminate your instance. You will no longer be able to connect to it after termination.



### Conclusion
Congratulations! You just provisioned a server in the AWS cloud.

I hope you enjoyed the tutorial.

If you have any questions or had any difficulty following along, kindly drop a comment below or reached out to me on [LinkedIn](https://www.linkedin.com/in/davidomokhodion). Till we meet again...

Stay awesome!
~ David Omokhodion.