These steps are optional and if you have extra time. Please consider moving to Lab I.
In this section, you create an SSH key-pair on your AWS Cloud9 instance, create an Amazon EC2 instance, then access it.
SSH is commonly used to connect to Amazon EC2 instances. To allow you to connect to your instances, you can generate a key-pair using the AWS CLI in your AWS Cloud9 instance. This example uses the key name lab-2-your-key but you can change the name of your key. Enter the following command to generate a key pair:
aws ec2 create-key-pair --key-name lab-2-your-key --query KeyMaterial --output text > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
Optionally, use the following command to check if your key is registered:
aws ec2 describe-key-pairs
When you create an EC2 instance, you need to place it in an Amazon Virtual Private Cloud (VPC). As a first step, identify the VPC and subnet of the AWS Cloud9 instance so you can place the EC2 instance in the same location.
MAC=$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/)
cat << EOF
***********************************************************************************
Subnet ID = $(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/subnet-id)
VPC ID = $(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/vpc-id)
************************************************************************************
EOF
Your instance is being launched! To check the status, view the Instances section of the EC2 Dashboard.
If you are having issues connecting to your instance, navigate to the EC2 Dashboard in the AWS Management Console. Select your instance and review its details. See also the troubleshooting section at the bottom of this page.
After the instance is running, follow these steps:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`]| [0].Value,InstanceType, PrivateIpAddress, PublicIpAddress]' --filters Name=instance-state-name,Values=running --output table
Make sure to select the IP address of the instance you want to connect to and not the example IP address shown.
# don't forget to use your OWN ip address
# keep the username to ec2-user as is, don't use your name!
ssh ec2-user@10.0.1.6
ping www.wikipedia.org
You now have an functional instance that can communicate with the outside world! Continue to the next section to see what else you can do.
Don’t forget to log out of your instance using ctrl-D or exit
.
There could be two primary reasons why you cannot connect to your instance: