EC2 Operation

EC2 Instance Information

ec2-describe-instances

RESERVATION     r-1a053371      558556443206    webserver
INSTANCE        i-d77524bd      ami-1234de7b    ec2-174-129-147-80.compute-1.amazonaws.com
domU-12-31-39-0A-91-41.compute-1.internal       running webserver       0
m1.small        2010-09-07T17:41:40+0000        us-east-1c      aki-5037dd39
monitoring-enabled      174.129.147.80  10.211.146.175                  ebs
BLOCKDEVICE     /dev/sda1       vol-b0633bd9    2010-09-07T17:41:58.000Z

ec2-get-console-output instance_id

Find EC2 Instance Private IP

ifconfig eth0
curl http://169.254.169.254/latest/meta-data/local-ipv4

Find EC2 Instance Public IP

curl http://169.254.169.254/latest/meta-data/public-ipv4

Retrieve EC2 Instance Meta-data

  • Find the available version of meta-data
    curl http://169.254.169.254/
    1.0
    2007-01-19
    2007-03-01
    2007-08-29
    2007-10-10
    2007-12-15
    2008-02-01
    2008-09-01
    2009-04-04
    
  • Retrieve available meta-data
    curl http://169.254.169.254/2009-04-04/meta-data/
    ami-id
    ami-launch-index
    ami-manifest-path
    block-device-mapping/
    hostname
    instance-action
    instance-id
    instance-type
    kernel-id
    local-hostname
    local-ipv4
    placement/
    public-hostname
    public-ipv4
    public-keys/
    reservation-id
    
  • Retieve meta-data
    curl http://169.254.169.254/2009-04-04/meta-data/public-hostname
    
  • Retrieve an item from a list
    curl http://169.254.169.254/2009-04-04/meta-data/public-keys/0/openssh-key
    

Lock an EC2 Instance

Lock an instance will disable the AWS Management Console from terminating an instance

ec2-modify-instance-attribute instance_id --disable-api-termination true

To unlock

ec2-modify-instance-attribute instance_id --disable-api-termination false

Troubleshoot why EC2 Instant is terminated

ec2-describe-instances instance_id -v

Change EC2 Instance Attribute

  1. Stop an instance
  2. Change the attribute
    ec2-modify-instance-attribute instance_id --instance-type instance_type
    ec2-modify-instance-attribute instance_id --user-data user_data