Amazon Elastic Compute Cloud (EC2)

Basics of EC2

Create EC2 instance using CLI

In this guide, you’ll learn how to launch and inspect EC2 instances using the AWS Command Line Interface (CLI). We’ll cover:

  1. Prerequisites
  2. Exploring EC2 commands
  3. Finding a suitable AMI
  4. Launching an instance
  5. Inspecting instance details

Note

Make sure you have the AWS CLI installed and configured with valid credentials. Set your default region with aws configure or by exporting the AWS_DEFAULT_REGION environment variable.

Table of Contents

StepDescriptionSample Command
1. Explore EC2 commandsView available EC2 operations and optionsaws ec2 help
2. Find an AMIList AMIs you own or public AMIs by Amazonaws ec2 describe-images --owners self amazon
3. Launch an instanceStart a new instance with AMI ID and typeaws ec2 run-instances …
4. Inspect instance detailsRetrieve instance metadata and statusaws ec2 describe-instances

1. Exploring EC2 Commands

To discover all EC2 subcommands, options, and examples:

aws ec2 help

For details on a specific operation—such as launching instances:

aws ec2 run-instances help

These built-in help pages include required parameters, optional flags, and sample usage.

2. Finding a Suitable AMI

An Amazon Machine Image (AMI) is required to launch an EC2 instance. To list AMIs you own and public AMIs provided by Amazon:

aws ec2 describe-images --owners self amazon

Example JSON output:

{
  "Images": [
    {
      "ImageId": "ami-056b35f582f6afbe7",
      "Name": "Cloud9AmazonLinux2-2023-04-04",
      "Architecture": "x86_64",
      "CreationDate": "2023-04-04T18:10:12.000Z",
      "PlatformDetails": "Linux/UNIX",
      "BlockDeviceMappings": [
        {
          "DeviceName": "/dev/xvda",
          "Ebs": {
            "SnapshotId": "snap-0875cc8a125bc63fd",
            "VolumeSize": 10,
            "VolumeType": "gp2"
          }
        }
      ]
    }
  ]
}

Key fields:

FieldDescription
ImageIdAMI identifier (used by run-instances)
NameHuman-readable name of the AMI
ArchitectureCPU architecture (e.g., x86_64, arm64)
PlatformDetailsOS platform (Linux/UNIX, Windows)
BlockDeviceMappingsRoot volume size and type

3. Launching an EC2 Instance

With your chosen AMI ID, launch a t2.micro instance:

aws ec2 run-instances \
  --image-id ami-056b35f582f6afbe7 \
  --instance-type t2.micro \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyDemoInstance}]'

Warning

Running EC2 instances incurs AWS charges. Always terminate resources you no longer need:

aws ec2 terminate-instances --instance-ids <your-instance-id>

Example JSON response:

{
  "Instances": [
    {
      "InstanceId": "i-076290d4a72165019",
      "ImageId": "ami-056b35f582f6afbe7",
      "InstanceType": "t2.micro",
      "LaunchTime": "2023-09-13T06:35:11+00:00",
      "Placement": {
        "AvailabilityZone": "ap-southeast-1a"
      }
    }
  ]
}

4. Inspecting Instance Details

To view the full set of metadata, including networking and state:

aws ec2 describe-instances --instance-ids i-076290d4a72165019

Partial JSON output:

{
  "Reservations": [
    {
      "Instances": [
        {
          "PrivateIpAddress": "172.31.39.161",
          "PublicDnsName": "",
          "State": {
            "Code": 0,
            "Name": "pending"
          }
        }
      ]
    }
  ]
}

Important fields to track:

FieldDescription
InstanceIdUnique identifier of the instance
PrivateIpAddressInternal IP within the VPC
PublicDnsNamePublic DNS assigned (if any)
State.NameCurrent state (pending, running)
LaunchTimeTimestamp of instance launch

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demo Create EC2 instance using UI Part 2