Creating a Key Pair Using Amazon EC2
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- In the navigation pane, under NETWORK & SECURITY, choose Key Pairs.
- The navigation pane is on the left side of the Amazon EC2 console. If you do not see the pane, it might be minimized; choose the arrow to expand the pane.
- Choose Create Key Pair.
- Enter a name for the new key pair in the Key pair name field of the Create Key Pair dialog box, and then choose Create.
- The private key file is automatically downloaded by your browser. The base file name is the name you specified as the name of your key pair, and the file name extension is .pem Save the private key file in a safe place.
- This is the only chance for you to save the private key file. You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance.
Trending AI Articles:
1. Ten trends of Artificial Intelligence (AI) in 2019
2. Bursting the Jargon bubbles — Deep Learning
3. How Can We Improve the Quality of Our Data?
4. Machine Learning using Logistic Regression in Python with Code
Connecting and changing instance settings
- Use your current Amazon user id and password or create an IAM user with your existing login.
- Follow the default settings to create an EC2 instance and choose the Amazon Ubuntu OS. For now, just select the free-tier t.2 micro as your instance type. You can change this in the future to something more powerful that fits your project’s needs.
- Instead of clicking “Review and Launch” button right away, click the “Next:” button until you get to Security Groups. Under Security Groups, select an existing group or create a new one then open the inbound port 8888. Port 8888 is what we’ll use for the Jupyter Notebook server towards the end of this tutorial. “SSH-ing” into port 22 should already be set to open by default.
4. Follow the rest of the prompts using whatever the default values are and then finally click “Launch.” At this point, you should be prompted with some security key options. You can use an existing key or download a new one. Let’s assume you don’t have one yet. The PEM file is a key that AWS will check when you try to access (or SSH) into your EC2 instance from your local computer’s terminal.
Select the option to create a new one and give it an easy to remember the name (all lower case with no spaces for ease of typing too). Download the PEM file and put it in an easy to reach location like your home folder. Now you can finally launch your EC2 instance by selecting… you guessed it…Launch Instance.
5. Once the EC2 instance is up (usually take a minute or less), SSH into your EC2 instance from your terminal by typing:
ssh -i "my-key-pair.pem" email@example.com
This assumes your PEM file is in the same directory as your present working directory (type “pwd” into your terminal if you’re unsure what your present working directory is). Also, you’ll need to type in your own user@ec2-instance address. If you’ve been following my steps so far the user is “ec2-user” (this is the default) and the address after “@” is your instance’s Public DNS (IPv4) address which you can view by selecting your instance (see screenshot below). If you get an error that your PEM file is not publicly viewable, you made need to execute this command:
chmod 400 my-key-pair.pem
Alternatively, you can click the Connect button next to Launch Instance (see above) and AWS will give you your instance specific instructions for SSH-ing in (see below). Thanks AWS!
Once you execute the SSH command, you’ll be prompted with a yes/no question. Type yes and you should be SSH-ed into your instance! (see screenshot below).
6. Download Anaconda 3 installer by typing this command:
Note: If you’re reading this tutorial in the distant future, you can get the exact link to the latest version on Anaconda3 by going to https://www.continuum.io/downloads and copying the link to the latest command line installer (see below).
7. Install Anaconda3 by typing:
You’ll have hit enter to get thru all the legalese, but eventually you need to type yes to agree and then just hit enter to install Anaconda3 into the default directory. Once it starts installing, you’ll see all the packages included in Anaconda3 also being installed. Anaconda3 gives you everything you’ll need to get your Jupyter Notebook running. At the end you’ll be prompted to include Anaconda3 into your .bashrc PATH. Make sure to type “yes” (see below).
If you accidentally hit enter before typing “yes”, it will default to “no.” To correct this you’ll have to manually type the PATH into your .bashrc file. To do this type:
Your screen will now be in vim mode. If you’ve never used vim before you might feel like you’ve entered an alternate reality. Basically, yes. Vim is a text editor for your terminal and is one of the greatest tools to learn and master is you plan on doing a significant amount of hacking. But let’s focus on the task ahead of us! You need to add the text:
8. Set Anaconda3 as your default Python environment. You’ll notice that your EC2 instance is configured to use the system’s Python 2.7. This is fine if you want to use 2.7 for all your projects, but you really should be using Python 3 for future projects which is why we installed Anaconda3 so we can switch to using the latest stable version of Python 3 instead of the default 2.7. To switch your environment to use Python 3 type the command:
which python /usr/bin/python
9. Create your Jupyter/Ipython password. In this step, we create a password to access out Jupyter Notebook from the web (remember that Jupyter runs notebooks on a server which anyone can access with an internet browser, so we need to set a password to prevent any unauthorized access to our notebooks). First we access the Ipython console by typing:
Now we want to create a password so we import the password module and type in our password to generate an SHA hashed version. You’ll need the remember the actual password you typed in and copy the SHA version for use in the next step. Type:
from IPython.lib import passwd
You’ll be prompted to type in your password and then verify. After that you’ll see an SHA version and then you can exit by typing “exit.” Make sure to save your SHA hash for future reference by copying and pasting it to a text file, you’ll need this later. It should look something like this:
10. Configure Jupyter/Ipython server to access your notebooks from your local computer via your internet browser. First we’ll create a default config file by just typing:
jupyter notebook --generate-config
Next, we’ll need to generate SSL certificates so our browser will trust our Jupyter Notebooks server (*sigh* I know). Luckily, this is pretty straightforward. Just type:
then go into your certs directory by typing:
then create your PEM file (this is separate from the PEM file on your local computer which we downloaded from AWS):
sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
This certificate is good for one year. You will be prompted to enter in some personal info for your certificate, but you can type in whatever you want. When you’re done, it should look something like this:
11. Edit your Jupyter configuration file. Let’s use vim to edit the configuration file we crated in the previous step:
You’ll notice that all the text in the config file is commented out and there’s no real structure beyond what you decide. So really, just paste the following text anywhere you want (remember to use your SHA hash instead of mine).
Hit “i” to enter INSERT mode and type in the configuration or copy and paste it in with COMMAND-c and COMMAND-v. Use the arrow keys to navigate around if you need to. See screenshot below for reference.
c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
# Notebook config
c.NotebookApp.certfile = u'/home/ec2-user/certs/mycert.pem' #location of your certificate file
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False #so that the ipython notebook does not opens up a browser by default
c.NotebookApp.password = u'sha1:262....your hash here.........65f' #edit this with the SHA hash that you generated after typing in Step 9
# This is the port we opened in Step 3.
c.NotebookApp.port = 8888
Once you got that in, hit ESC and type “:wq” to save and quit out of vim.
12. Create a folder for your notebooks and start Jupyter Notebook:
Now that you’re in the Notebooks directory you created, you can open Jupyter Notebooks:
11. Access Jupyter Notebooks from your browser. To get there, you’ll need your Public DNS (IPv4) we accessed in Step 5 to get this:
Chrome will show you this because it realizes the SSL certificate being used is the one we issued ourself in Step 11. We are aware of this so to continue on we just click the “Advanced” button and then follow the “Proceed to…” link at the bottom.
Click “Log In” and you’re ready to create and run your own notebooks on Jupyter!