June 18, 2022 No Comments

What is an EC2 Instance?

In Amazon Web Services terminology, an EC2 instance is just a virtual server. EC2 stands for Elastic Compute Cloud. It is a web service that allows an AWS subscriber to request and provision a compute server in the AWS cloud.

The instance will be charged every hour at various prices depending on the instance type selected. AWS offers a variety of instance types depending on the user’s needs.

In this tutorial, we will learn how to set up an EC2 instance in the AWS cloud.

1. Login and access to AWS Console

Login to your AWS account and open the Amazon EC2 console.

Here is the EC2 dashboard. Here you will get all the information about the AWS EC2 resources running.

On the top right corner of the EC2 dashboard, choose the AWS Region. Here we are selecting N. Virginia.  AWS provides 22 Regions all over the globe.

2. Click on “Launch Instance"

Once your desired Region is selected, come back to the EC2 Dashboard. To create an EC2 AWS instance click on the ‘Launch instance’ button as shown below.

3. Enter an instance name

Once you click on Launch Instance, you will be able to see a configuration screen. Please enter an instance name as shown in the below image.

4. Choose an AMI (Amazon Machine Image)

You must specify an AMI when you create your EC2 instance in AWS. As you click on “Browse more AMIs”, you will get a list of all the available AMIs.

We are going to use “Ubuntu Server 20.04 LTS (HCM), SSD Volume Type”.

5. Choose an instance type

After selecting AMI, you need to select an instance type. AWS provides a wide range of instance types to fit different use cases. We are going to use “g4ad.8xlarge”.

6. Create a new key pair

To create new key pair for an instance click on “Create new key pair”.

Enter the name of the key pair, select key pair type, and select the private key file format as shown in the below image. Download the Key pair by clicking on “Create key pair”.

7. Settings

You can add rules to allow specific traffic to reach your instance.

8. Add storage

You can modify the root volume settings or you can add a new volume if required.

9. Launch Instance

In this step, we will review all our choices and parameters and go ahead to launch our instance by clicking on “Launch Instance”.

You will see a status like your instance is launching, like below.

As you click on “View all instances”, you are able to see the list of all available instances.

By default, created instance state is Running. You can click on the Instance ID and after that click on Instance state to start, stop or terminate an instance.

You can see more details about your instance by clicking on the Instance ID. A public IPv4 address can be used to connect your instance.

10. Connect to an EC2 instance using SSH (.pem key)

Open your terminal and change the directory with the command cd, where you downloaded your pem file. Copy the below command on your terminal with your .pem file name and IP address which is copied from instance details as shown in the above image.

				
					sudo ssh -i file.pem username@ip-address
				
			

After pressing enter, a question will display to add the host to your known_hosts file. Type yes. This will help to recognize the host each time you’re trying to connect to your instance.

Graphics card drivers and CUDA Installation

Now, to install the Graphics card drivers and CUDA, which will help in the training of Large Language Models, follow the below procedures.

1. GCC Installation

Before installing Graphics card drivers and CUDA you need to install GCC. You can install it by running the following command.

				
					sudo apt-get update 
sudo apt install build-essential 
sudo apt -y install gcc-9 g++-9
				
			

2. Graphics Card Drivers Installation

After the successful installation of GGC, we are going to install drivers for our graphics card. You can search for all available drivers with the following command.

				
					sudo apt search nvidia | grep headless
				
			

This should give you a list of all driver versions available. Search for the latest version, in our case this was version 460. Install it along with the utils with this command.

				
					sudo apt install nvidia-headless-460 nvidia-utils-460
				
			

After the installation is completed successfully, reboot your instance. Once it is back up, check if your graphics card gets detected correctly with this command.

				
					nvidia-smi
				
			

You should get an output similar to the picture below.

3. CUDA Installation

After our driver got installed successfully, it is time to install CUDA.

  • Open the NIVIDA Website and select the version of CUDA that you need.
  • After choosing the version, enter the correct information about your platform, in our case, this will be the following:
    Operating System: Linux
    Architecture: x86_64
    Distribution: Ubuntu
    Version: 20.04
    Installer Type: runfile (local)

When you entered all the information, the website will provide you with instructions on how to install CUDA.

				
					wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run 
sudo sh cuda_11.3.0_465.19.01_linux.run
				
			
  • Once the script start and you accept the EULA, you can choose what to install. It’s important to uncheck the driver here:

  • Once the installation completes, we need to configure our environment variables. Edit your .bashrc file with the following command:
				
					sudo vim ~/.bashrc
				
			
  • Now add the following lines to the end of the file:
				
					export PATH=/usr/local/cuda-11.3/bin:$PATH 
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib:$LD_LIBRARY_PATH
				
			
  • Reload your environment variables:
				
					source ~/.bashrc
				
			
  • Finally, check if CUDA is working properly:
				
					nvcc -V
				
			
  • Your output should look like the picture below. If it does, the installation of CUDA is now done.

We learned how to set up an EC2 instance in the AWS cloud by following the above tutorial. Now you can use this instance to deploy the app, train the large language models, and so on – Let’s get started on the next great thing!

If you face any problems following the guide, please let us know in the comments section.

Write a comment

Your email address will not be published. Required fields are marked *

Want to talk to an Expert Developer?

Our experts in Generative AI, Python Programming, and Chatbot Development can help you build innovative solutions and scale your business faster.