DEV Community

MariaZentsova
MariaZentsova

Posted on

How to interact with AWS EC2 instance via git bash

AWS EC2 is a service, which could be used for almost anything from website hosting to training machine learning models.

AWS allows encrypted access to EC2 using key pairs, where a user stores its own key.

One of the easiest ways to connect to running EC2 instance is by using git bash.

1. Check that Git is installed

Open the terminal and run git --version, which would show the git version currently installed on the system. If it is not installed, the download git from the official website: https://git-scm.com/downloads

2. Set the permission on your .pem file

To use this .pem file with ssh, we need to set a permission on it with chmod command, where feb19.pem is a file with our key. We need to do it, so the key won't be publicly visible.



chmod 400 feb19.pem


Enter fullscreen mode Exit fullscreen mode

3. Access EC2 from the terminal

Select your instance and click "Connect".

connect to EC2 instance

Once we have an encryption file and EC2 instance public DNS, then we can connect.



ssh -i feb19.pem ec2-user@ec2-44-201-115-163.compute-1.amazonaws.com


Enter fullscreen mode Exit fullscreen mode

One thing to keep in mind, when we restart an instance, public IP gets reassigned, and often due to permissions we won't be able to reuse the same encryption file with the new dns. To avoid this, we could use AWS Elastic IP service.

When this is useful? Let imagine, we have to retrain a machine learning model weekly, to save the costs we will stop the instance, when it's not utilised. To avoid modifying our scripts, we could allocate an Elastic IP, so we won't have any problems that arise from the constantly changing address. AWS has a good tutorial, how to allocate static IP to an instance.

4. Copy files to the EC2 instance

To perform some tasks, often we would copy local file on the instance, using the secure copy command scp, where:

  • - i - identity file flag

  • feb19.pem - file with our key

  • hello_world.py - python file we want to copy on a server

  • ec2-user - a standard username

  • ec2-44-201-115-163.compute-1.amazonaws.com - the dns address of ec2 instance

  • :~/ - indicates a root folder, where we want to copy the file



scp -i feb19.pem hello_world.py ec2-user@ec2-44-201-115-163.compute-1.amazonaws.com:.


Enter fullscreen mode Exit fullscreen mode

To copy entire local folder, we need to use -r flag for recursive option to copy all files in a directory.



scp -i feb19.pem -r to_test ec2-user@ec2-44-201-115-163.compute-1.amazonaws.com:~/


Enter fullscreen mode Exit fullscreen mode

Once we're connected to the instance, we can delete a file



rm hello_world.py


Enter fullscreen mode Exit fullscreen mode

As well as see all files currently available on the instance by running



ls -la


Enter fullscreen mode Exit fullscreen mode

5. Installing libraries on the instance

To install any particular library on the instance run



sudo yum install <package_name>


Enter fullscreen mode Exit fullscreen mode

List command will allow to see all packages that are already installed.



yum list installed


Enter fullscreen mode Exit fullscreen mode

6. Run python script

To run python scripts on EC2 instance, first we need to set up python environment.



sudo yum install python36


Enter fullscreen mode Exit fullscreen mode

Once python is installed, let's run our script



Python hello_world.py

Enter fullscreen mode Exit fullscreen mode



  1. Download files from the instance

Once the work is done and we generated an output, we could copy it back to our machine. Here I copy all files from the instance source directory to my local machine.



scp -i feb20.pem -r ec2-user@ec2-3-86-190-91.compute-1.amazonaws.com:~/ <localmachine>/Downloads

Enter fullscreen mode Exit fullscreen mode



  1. Disconnect from the EC2 instance

To halt the connection with the instance, run simple Ctrl + Dcommand.

Resources:

Top comments (0)