Computer Science Atlas
Tutorial

How to Create an Ubuntu 20.04 Server on AWS Lightsail

January 16, 2021
 

Overview

Lightsail is a virtual private server (VPS) service from Amazon Web Services (AWS). It allows you to rent by the hour computers (called servers) running in one of AWS's many data centers across the globe. You can use your VPS by connecting to it over the Internet.

A VPS is "virtual" because the server you're renting is one of many running on a single physical hardware computer. Although you're not using an entire physical hardware computer by yourself, the VPS still acts like a whole standalone machine, and you have full control over it (hence the word "private").

A single server running in Lightsail is called an instance. Thus when we launch a new server in Lightsail, we say we are "creating a new instance."

This tutorial describes how to create a new instance in Lightsail running the Ubuntu 20.04 Linux-based operating system.

Step 1. Open AWS Cloud Shell

Open AWS Cloud Shell. The rest of the instructions on this page are for running inside Cloud Shell.

Step 2. Set the AWS Region

Tell the aws command which AWS geographic region you want to use by setting the AWS_REGION environment variable. For example, if you want to use the us-west-2 (Oregon) region, you should run:

Bash
Copy
export AWS_REGION='us-west-2'
$
export AWS_REGION='us-west-2'

You can find out more about available regions here.

Your new instance will be created in this region.

Step 3. Create a New Instance

At the Cloud Shell prompt, choose a name for your new instance and export it as an environment variable. For example, if you chose the name "myubuntu" you would run:

Bash
Copy
export INSTANCE_NAME="myubuntu"
$
export INSTANCE_NAME="myubuntu"

We will use this environment variable in the commands below.

Now create a new instance using that name:

Bash
Copy
aws lightsail create-instances           \
    --instance-names    "$INSTANCE_NAME" \
    --availability-zone "us-west-2a"     \
    --blueprint-id      "ubuntu_20_04"   \
    --bundle-id         "small_2_0"
$




aws lightsail create-instances           \
    --instance-names    "$INSTANCE_NAME" \
    --availability-zone "us-west-2a"     \
    --blueprint-id      "ubuntu_20_04"   \
    --bundle-id         "small_2_0"

Step 4. Configure SSH Access from Cloud Shell

Since we did not specify a key pair in the create-instances command above, Lightsail automatically used the default key pair for your account for the new instance.

Let's download the default key pair and save it to your Cloud Shell:

Bash
Copy
aws lightsail download-default-key-pair --output text --query publicKeyBase64 > ~/.ssh/lightsail-default-key.pub
chmod 644 ~/.ssh/lightsail-default-key.pub
aws lightsail download-default-key-pair --output text --query privateKeyBase64 > ~/.ssh/lightsail-default-key
chmod 600 ~/.ssh/lightsail-default-key
$
$
$
$
aws lightsail download-default-key-pair --output text --query publicKeyBase64 > ~/.ssh/lightsail-default-key.pub
chmod 644 ~/.ssh/lightsail-default-key.pub
aws lightsail download-default-key-pair --output text --query privateKeyBase64 > ~/.ssh/lightsail-default-key
chmod 600 ~/.ssh/lightsail-default-key

Now let's create a new SSH config file for your new instance.

Bash
Copy
mkdir -p ~/.ssh/config-instances
IP=$(aws lightsail get-instance --instance-name $INSTANCE_NAME --output text --query instance.publicIpAddress); \
printf "\nHost $INSTANCE_NAME\n\tUser ubuntu\n\tHostName $IP\n\tIdentityFile ~/.ssh/lightsail-default-key\n" > ~/.ssh/config-instances/"$INSTANCE_NAME"
chmod 600 ~/.ssh/config-instances/"$INSTANCE_NAME"
$
$

$
mkdir -p ~/.ssh/config-instances
IP=$(aws lightsail get-instance --instance-name $INSTANCE_NAME --output text --query instance.publicIpAddress); \
printf "\nHost $INSTANCE_NAME\n\tUser ubuntu\n\tHostName $IP\n\tIdentityFile ~/.ssh/lightsail-default-key\n" > ~/.ssh/config-instances/"$INSTANCE_NAME"
chmod 600 ~/.ssh/config-instances/"$INSTANCE_NAME"

Finally, add a reference to the new config file in your main ~/.ssh/config file so the ssh program knows to load it.

Bash
Copy
printf "\nInclude config-instances/$INSTANCE_NAME\n" >> ~/.ssh/config
chmod 600 ~/.ssh/config
$
$
printf "\nInclude config-instances/$INSTANCE_NAME\n" >> ~/.ssh/config
chmod 600 ~/.ssh/config

Step 5. SSH into the New Instance

Now you can use the instance name you chose above to open an SSH connection to your new instance. If you chose "myubuntu" as the name, you can run:

Bash
Copy
ssh myubuntu
$
ssh myubuntu

If this command fails immediately after you create your new instance, try waiting a minute or two for the server to finish booting before retyring the command.

When you connect, you should see a message like:

Out
Copy
The authenticity of host 'xx.xx.xx.xx (xx.xx.xx.xx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxx.
ECDSA key fingerprint is MD5:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?
The authenticity of host 'xx.xx.xx.xx (xx.xx.xx.xx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxx.
ECDSA key fingerprint is MD5:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?

At the prompt, type yes and press Enter on your keyboard to continue. You will only have to do this once — the next time you open an SSH connection, this message will not appear.

Congratulations!

You now have a shiny new Ubuntu 20.04 server running in AWS Lightsail. Enjoy!