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
3. Access EC2 from the terminal
Select your instance and click "Connect".
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
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 flagfeb19.pem
- file with our keyhello_world.py
- python file we want to copy on a serverec2-user
- a standard usernameec2-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:.
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:~/
Once we're connected to the instance, we can delete a file
rm hello_world.py
As well as see all files currently available on the instance by running
ls -la
5. Installing libraries on the instance
To install any particular library on the instance run
sudo yum install <package_name>
List command will allow to see all packages that are already installed.
yum list installed
6. Run python script
To run python scripts on EC2 instance, first we need to set up python environment.
sudo yum install python36
Once python is installed, let's run our script
Python hello_world.py
- 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
- Disconnect from the EC2 instance
To halt the connection with the instance, run simple Ctrl + D
command.
Resources:
Top comments (0)