Deploying project and installing Anaconda on Amazon EC2 [for Windows]
By: Deepak Avudiappan
Tags: Deployment, Amazon Web Services, Windows 10, Ec2, Ec2 Instance

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. (source)

Amazon EC2 is a common platform when you want to deploy the project.

Just follow the steps🙂

Step1: Login to your Amazon AWS account and in services select EC2

Step2: Choose your desired Engine, I selected Amazon Linux 2 AMI.

Step3: Select the Instance(virtual server) Type, (I have selected free tier)

And select “Review and Launch” to review your instance.

Step4: Launch the Instance.

Step5: Create a new Key Pair, this key is important to log in to the EC2 server every time after you have logged out.

Step6: After selecting “Create a new key pair”. Enter a name for your key pair, better to keep the project name as this will help in the future when you have many instances.

Step7: Download Key Pair and save it in a safe file separately.

Step8: Launch the Instances after saving the ‘.pem’ file.

Your instance is launched Successfully!🎉

Scroll down and click View Instances.

You can name your instance if you want by clicking the edit symbol:

Step9: See the Instance state, wait till it shows running.

Then click Connect.

Note these three: > Instance ID

> Public DNS address

> Example

Now in order to make a connection using SSH, you need to check whether SSH services are installed in your system or not.

Step10: Go to Settings/Apps

Click “Optional features” and search SSH

Note these two are important for our connection to the EC2 server. If not installed click “Add a feature” and install these two.

Step11: By right-clicking on the ‘.pem’ file, open file properties that you downloaded before.

Inside Properties/Security see the groups that have access.

These are all explicit permissions given as default but we only want the current user to get the read permission(to prevent data corruption).

Step12: Open the Windows PowerShell and navigate to the directory where you kept your ‘.pem’ file.

Execute the below command respectively:

Rename the .pem file to original ‘.pem’ filename

> path = “.\test.pem”

2 . Reset permissions to remove explicit permissions:

> icacls.exe $path /reset

3. Give current user explicit read-permission to avoid data corruption.

> icacls.exe $path /GRANT:R “$($env:USERNAME):(R)”

4. Disable inheritance and remove inherited permissions

> icacls.exe $path /inheritance:r

Note: If you don’t execute the above command you’ll get the below errors: Windows SSH: Permissions for ‘private-key’ are too open Set correct permissions → An unprotected private key file (etc…)

Step13: Now copy the Example: ssh command.

And past it in cmd in the path where the ‘.pem’ key file is present.

You have Successfully connected to the Instance!!

Now to add a file download Cyberduck for windows.

After installing, click Open Connection.

Select “SFTP(SSH File Transfer Protocol)”

Why SFTP? We have selected SFTP because it is a Secure Shell protocol that provides file access, file transfer, and file management over any reliable data stream.

Add your Pem file that you downloaded in “SSH Private Key”, Add the name of your server visible in your cmd where you connected your SSH instance. Like [ec2-user@ip-######]$.

Lastly, add Server address i.e Public DNS address:

Copy the Public DNS address and paste it in Server.

Click Connect and wait…

Step14: Drag and drop your project folder.

Now You can view your file in your cmd terminal by typing the ‘ls’ command

Installing Anaconda on EC2:

Step 1: Download the latest version of Anaconda

Right Click on the 64-Bit (x86) Installer (522 MB) and click on “Copy Link Location/Copy Link Address“.After the copy, you will get the following link address or any latest distribution.

Step 2: Connect to your EC2 Instance(Amazon Linux) where you want to install Anaconda and run the following command.

$ wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

Step 3: After download run the downloaded script

$ sh Anaconda3–2020.02-Linux-x86_64.sh

You will be promoted for the following options:

In order to continue the installation process, please review the license agreement.

Please, press ENTER to continue →Press Enter

Do you accept the license terms? [yes|no] →Type Yes

Anaconda3 will now be installed into this location:

[/home/xyz/anaconda3] → Press Enter

Do you wish the installer to initialize Anaconda3

by running conda init? [yes|no]

[no] → Type Yes

As soon as you enter Yes Anaconda will be installed on your Linux box and you will also get the following message.

Thank you for installing Anaconda3!

Step 4 — After installation, use the following command to activate the installation

$ source ~/.bashrc

Command-line tool to Use Conda

For CLI use the following commands. Some examples are as follows:

To install a package

$ conda install package_name

To update a package

$ conda update package_name

To remove a package

$ conda remove package_name

After installing the Anaconda on EC2 you will see (base):

Now you can install your packages and run your project here

But there is a problem when exiting the terminal your project will terminate.

To avoid this you need to install Screen that allows us to resume the sessions after exiting.

Type this command:

“sudo yum install screen”

To start a new session on screen, type this command:

“screen -S <name you screen>”

Now you can run your project on this screen.

To go back to the main terminal press “ctrl + a + d”.

And to go back to the screen you just created type this command:

“screen -r <name of your screen>”

Lastly to exit your server simply type this in terminal:

“exit”

Congratulations your model is Successfully deployed!🎉