Launch Your First EC2 Instance
Amazon Elastic Compute Cloud (EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Understanding how to provision and connect…
Amazon Elastic Compute Cloud (EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Understanding how to provision and connect to an EC2 instance is fundamental for anyone working with AWS, from deploying applications to setting up development environments. This guide walks through the process of launching a basic EC2 instance, focusing on practical steps and common configurations.
We'll cover the essential components: choosing an Amazon Machine Image (AMI), selecting an instance type, configuring network access with security groups, and securely connecting using SSH key pairs. The process described here is applicable to the AWS Management Console and utilizes components that are generally within the AWS Free Tier for new accounts, minimizing costs during initial exploration.
I. Navigating to the EC2 Dashboard and Launching an Instance
To begin, log in to the AWS Management Console. Navigate to the EC2 service by typing "EC2" into the search bar at the top or finding it under the "Compute" section. From the EC2 dashboard, locate the "Launch instance" button, typically prominent in the main view or under the "Instances" section.
Choosing an Amazon Machine Image (AMI)
The first step in launching an instance is selecting an Amazon Machine Image (AMI). An AMI is a template that contains the software configuration (operating system, application server, applications, etc.) required to launch your instance. For this guide, we'll use a common, well-supported Linux distribution.
- On the "Name and tags" screen, give your instance a descriptive name, e.g.,
MyFirstEC2Instance. - Under "Application and OS Images (Amazon Machine Image)", search for
Amazon Linux 2023 AMI. This is the latest generation of Amazon's own Linux distribution, optimized for AWS. Select it. - Verify that the "Architecture" is set to
64-bit (x86), which is standard for most general-purpose instances.
Selecting an Instance Type
The instance type determines the hardware resources (CPU, memory, storage, network performance) available to your instance. AWS offers a wide array of instance types, each optimized for different use cases. For a first instance, we'll stick to a Free Tier eligible option.
- Under "Instance type", select
t3.micro. Thet3family are "burstable performance" instances, ideal for workloads that don't require consistently high CPU utilization but can burst when needed. - The
.microsize typically includes 2 vCPUs and 1 GiB of memory. Crucially,t3.microinstances are generally eligible for the AWS Free Tier, allowing new accounts to run them for up to 750 hours per month without charge.
II. Configuring Key Pairs for Secure Access
SSH key pairs are the primary method for securely connecting to Linux EC2 instances. A key pair consists of a public key (stored on AWS and associated with your instance) and a private key (stored on your local machine). You will need the private key to authenticate yourself when connecting.
- Under "Key pair (login)", choose "Create a new key pair".
- Key pair name: Enter a memorable name, such as
my-ec2-key. - Key pair type: Select
RSA. - Private key file format: Choose
.pemfor use with OpenSSH (Linux/macOS) or PuTTY (Windows, though PuTTY typically uses.ppkafter conversion). For this guide, we'll assume a Linux/macOS client using.pem. - Click "Create key pair". Your browser will automatically download the
.pemfile (e.g.,my-ec2-key.pem). Store this file securely; if you lose it, you cannot connect to your instance.
Once downloaded, immediately restrict permissions on the private key file on your local machine to prevent unauthorized access. For Linux/macOS:
chmod 400 my-ec2-key.pem
This command changes the file permissions so that only the owner can read the file (-r--------). SSH clients will often refuse to use private keys with more permissive access rights.
III. Configuring Network Settings and Security Groups
Security groups act as virtual firewalls, controlling inbound and outbound traffic to your instance. This is a critical security configuration.
- Under "Network settings", ensure "Create security group" is selected for a new configuration.
- Security group name:
MyFirstEC2SecurityGroup - Description:
Allow SSH access to MyFirstEC2Instance - Inbound security group rules:
- Type: SSH
- Source type: My IP
Selecting "My IP" automatically populates the source with your current public IP address (e.g., 192.0.2.0/32). This is the most secure option for initial setup as it only allows SSH access from your specific machine. If your IP address changes frequently or if you need access from multiple locations, you might need to update this rule or use a broader range (e.g., 0.0.0.0/0 for anywhere, not recommended for SSH unless absolutely necessary and paired with strong security practices). SSH operates on TCP port 22 by default.
IV. Configuring Storage and Launching the Instance
Every EC2 instance requires at least one root volume for the operating system. Additional volumes can be attached for data storage.
- Under "Configure storage", the default root volume for Amazon Linux 2023 is typically 8 GiB, General Purpose SSD (gp2 or gp3). This is sufficient for most basic operations and is often within the Free Tier limits (up to 30 GiB of gp2/gp3 storage).
- You can increase the size if needed, but be mindful of costs. For this exercise, the default is fine.
Review all your settings on the "Summary" panel. Once satisfied, click the "Launch instance" button at the bottom right. AWS will begin provisioning your instance. This typically takes a few minutes.
V. Connecting to Your EC2 Instance via SSH
After launching, return to the EC2 dashboard. Under "Instances", you should see your newly created instance. Its "Instance state" will transition from "pending" to "running".
- Select your instance from the list.
- In the "Details" tab, locate the "Public IPv4 address" (e.g.,
54.XXX.YYY.ZZZ). - Open a terminal on your local machine.
- Use the
sshcommand with your private key and the public IP address.
ssh -i my-ec2-key.pem ec2-user@<Public_IPv4_Address>
-i my-ec2-key.pem: Specifies the path to your private key file.ec2-user: This is the default username for Amazon Linux AMIs. Other AMIs may use different default usernames (e.g.,ubuntufor Ubuntu,centosfor CentOS,adminorrootfor others).<Public_IPv4_Address>: Replace this with the actual public IP address of your EC2 instance.
The first time you connect, you might see a warning about the authenticity of the host. Type yes and press Enter to proceed. You should then be successfully logged into your EC2 instance's shell.
The authenticity of host '54.XXX.YYY.ZZZ (54.XXX.YYY.ZZZ)' can't be established.
ED25519 key fingerprint is SHA256:EXAMPLEFINGERPRINTS.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Last login: Wed Oct 26 10:00:00 2023 from your.local.ip
, #_
~\_ ####_ Amazon Linux 2023
~~ \_ #####
~~ \ ###
~~ \_ #
~ ~~
~ ~
~ ~
~ _ _ ~
~~~(~_~)~~~
~~\ V /~~
~ \ / ~
\ /
V
For documentation, visit https://docs.aws.amazon.com/AL2023/
ec2-user@ip-172-31-XX-YY:~ $
Congratulations, you have successfully launched and connected to your first EC2 instance!
VI. Common Pitfalls and Troubleshooting
Permission denied (publickey)error:- Ensure the
.pemfile has correct permissions (chmod 400 my-ec2-key.pem). - Verify you are using the correct private key for the instance.
- Check the default username (
ec2-userfor Amazon Linux).
- Ensure the
Connection timed outerror:- The most common cause is the Security Group not allowing SSH (port 22) from your IP. Double-check the inbound rules for the security group associated with your instance.
- Verify the instance state is "running".
- Ensure you are using the correct Public IPv4 address.
- Instance takes too long to launch or is stuck in "pending":
- This is rare but can happen if there are temporary capacity issues in the chosen Availability Zone or region. Wait a few minutes, or try launching in a different AZ.
- Check the "System Log" from the "Monitor and troubleshoot" tab of the instance details for boot errors.
- Free Tier eligibility: While
t3.microis generally free tier eligible, ensure your usage stays within the 750 hours/month limit for EC2 instances and 30 GiB for EBS storage. Exceeding these limits will incur charges. Always review your AWS Billing Dashboard.