Open AWS Cloud Shell. The rest of the instructions on this page are for running inside Cloud Shell.
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:
$ export AWS_REGION='us-west-2'
You can find out more about available regions here.
Your new instance will be created in this region.
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:
$ export INSTANCE_NAME='myubuntu'
We will use this environment variable in the commands below.
Now create a new instance using that name:
$ aws lightsail create-instances \
--instance-names "$INSTANCE_NAME" \
--availability-zone "us-west-2a" \
--blueprint-id "ubuntu_20_04" \
--bundle-id "small_2_0"
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:
$ 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.
$ 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.
$ printf "\nInclude config-instances/$INSTANCE_NAME\n" >> ~/.ssh/config $ chmod 600 ~/.ssh/config
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:
$ 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:
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.
You now have a shiny new Ubuntu 20.04 server running in AWS Lightsail. Enjoy!