0
0
AWScloud~5 mins

Launching an EC2 instance in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need a virtual computer in the cloud to run your apps or store data. Launching an EC2 instance creates this virtual computer quickly and easily.
When you want to run a website without buying a physical server.
When you need a test environment to try new software safely.
When you want to run a database accessible from anywhere.
When you need extra computing power for a short time.
When you want to host a small app for friends or customers.
Config File - ec2-instance.json
ec2-instance.json
{
  "ImageId": "ami-0c02fb55956c7d316",
  "InstanceType": "t2.micro",
  "KeyName": "my-key-pair",
  "SecurityGroupIds": ["sg-0a1b2c3d4e5f6g7h"],
  "MinCount": 1,
  "MaxCount": 1
}

ImageId: The ID of the operating system image to use (Amazon Linux 2 here).

InstanceType: The size and power of the virtual computer (t2.micro is free tier eligible).

KeyName: The name of your SSH key to connect securely.

SecurityGroupIds: The firewall rules group allowing traffic.

MinCount and MaxCount: Number of instances to launch (1 here).

Commands
This command launches a new EC2 instance using the settings in the JSON file.
Terminal
aws ec2 run-instances --cli-input-json file://ec2-instance.json
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "ImageId": "ami-0c02fb55956c7d316", "State": { "Code": 0, "Name": "pending" }, "InstanceType": "t2.micro", "KeyName": "my-key-pair", "LaunchTime": "2024-06-01T12:00:00.000Z" } ] }
--cli-input-json - Specifies the JSON file with instance configuration.
This command checks the status and details of the launched EC2 instance.
Terminal
aws ec2 describe-instances --instance-ids i-0123456789abcdef0
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0123456789abcdef0", "State": { "Code": 16, "Name": "running" }, "PublicIpAddress": "54.123.45.67", "InstanceType": "t2.micro" } ] } ] }
--instance-ids - Specifies which instance to describe.
This command connects you securely to the EC2 instance using SSH and your key.
Terminal
ssh -i my-key-pair.pem ec2-user@54.123.45.67
Expected OutputExpected
The authenticity of host '54.123.45.67 (54.123.45.67)' can't be established. ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '54.123.45.67' (ECDSA) to the list of known hosts. [ec2-user@ip-172-31-xx-xx ~]$
-i - Specifies the private key file for authentication.
Key Concept

If you remember nothing else from this pattern, remember: launching an EC2 instance means creating a virtual computer in the cloud ready to use with your settings.

Common Mistakes
Using a wrong or missing KeyName in the JSON file.
You won't be able to connect to the instance securely without the correct SSH key.
Make sure the KeyName matches an existing key pair in your AWS account.
Not specifying the SecurityGroupIds or using a group without SSH access.
You cannot connect to the instance via SSH if the firewall blocks port 22.
Use a security group that allows inbound SSH (port 22) traffic.
Trying to SSH before the instance state is 'running'.
The instance is not ready to accept connections yet.
Check the instance state with describe-instances and wait until it is 'running'.
Summary
Create a JSON file with the EC2 instance settings like image, type, key, and security group.
Run 'aws ec2 run-instances' with the JSON file to launch the instance.
Use 'aws ec2 describe-instances' to check the instance status and get its IP address.
Connect to the instance securely using SSH with your private key and the public IP.