Thursday, 21 April 2022

Using AWS EC2 Linux servers with Github

 This post attempts to show you how to

  • create AWS EC2 Linux server instances
  • connect to them using SSH
  • clone your GitHub repo onto the instance

Create AWS EC2 Instance

AWS is 'Amazon Web Services'.  Basically Amazon provide rack upon rack of computers that you can borrow.  They provide many cloud based services most of which I don't really understand what they are, but when I want to use an AWS Linux server I need to use EC2.  It stands for "Elastic Compute Cloud" and it basically offers a whole range of different machines with different operating systems running on them.  You can rent just one of them, or you can rent armfuls of them.

  • Login to AWS and go to the EC2 dashboard
  • click on 'Launch Instances' to configure an instance and launch it.

AWS EC2 Instance configurations: Operating System

Choose the operating system you want installed on the instance.  I choose Amazon Linux because I think I read that it is best tailored to AWS EC2 instances.  Ubuntu is an alternative option.

AWS EC2 Instance configurations: Instance Type

This is basically how good the computer you are renting is and what it's spec is.  It's worth considering a 'free tier' config if you are in your first year. 
  • t1 is older than t2 and t3 is the newest.
  • a vCPU is a sort of standardised AWS performance CPU
  • other specs are obvious

AWS EC2 Instance configurations: Key Pair

You need to create a pair of keys to ssh to the instance from a terminal.  The public key is stored on the instance and you must save the private key locally.  This means that only someone with the private key can connect to the instance.
  • Create a key pair - use RSA and .pem
You must save the private key.  Confusingly the private key has a .cer filetype not a .pem one, but it still seems to work fine.

Make sure you change the permissions of the private key because ssh checks and fails if it is public:

chmod 400 woodys_private_key.cer

AWS EC2 Instance configurations: Network Settings

Allow SSH traffic from anywhere unless you're doing something very sensitive.

AWS EC2 Instance configurations: Storage

Self explanatory

AWS EC2 Launch Instance

Check the summary matches how you think you've configured it and , hit the 'Launch Instance' button.

Congratulations.

Connect to an AWS EC2 Instance using SSH

#1 private key

Make sure you have the private key from the instance's key pair on the machine you want to ssh from.

Make sure it only has user read access:

chmod 400 woodys_private_key.cer

#2 Public IPv4 address

Note the public IPv4 address associated with the instance from the EC2 Dashboard. E.g. 34.226.22.46

#3 SSH

The user for the EC2 instance is always `ec2-user`.

ssh -i ~/Downloads/woodys_private_key.cer ec2-user@34.226.22.46

Reply 'yes' to confirm you want to connect.


Clone your GitHub Repo

Assuming you have code already on a github repo.

Install Git on your instance

sudo yum install git

Clone repo

On the repo's GitHub page, click on the green 'code' button.  Copy the https .git address.  E.g. https://github.com/Woody/my_repo.git

Clone the repo into the 'workspace' directory

git clone https://github.com/Woody/my_repo.git workspace

It shouldn't need any username/passwords

Push repo changes back to GitHub

This process is slightly more involved.  Basically you need to use a username and 'password', but GitHub have replaced passwords with personal access tokens.

Check that you have a valid GitHub 'personal access token'

GitHub have sort of replaced passwords with 'personal access token's or PATs.  You need to create one through a bizzare route, then use the PAT as your password.  

Make sure you select the permissions that you want the PAT user to have. Presumably you want them to be able to access the 'repo' so check all the repo boxes at least.

The PAT is basically a password to you or I when you actually come to use it.

Push repo changes back to GitHub

Assuming you've committed changes locally on your AWS EC2 instance.  To push the repo back:

git push

This will prompt you for your "Username for 'https://github.com':"
Respond with your GitHub username (obviously).  E.g. woody

It will then prompt you for your "Password 'https://woody@github.com':"
Respond with your GitHub PAT not your GitHub password (less obviously).


No comments: