See Also

Amazon Web Services

AWS Elastic Compute CloudAWS Command Line Interface

AWS CLI EC2 Getting Started

1. 概述

更多AWS CLI EC2命令操作详见AWS_CLI#Using_Amazon_EC2_Instances

本文档将用命令行完成以下操作:

2. 实操

2.1. 查询需要的AMI

找到被我标记为fee=free的AMI

(pythonenv)➜  aws_cli  aws ec2 describe-images --filters "Name=tag:fee, Values=free"
{
    "Images": [
        {
            "VirtualizationType": "hvm", 
            "Name": "amzn-ami-minimal-hvm-2014.09.1.x86_64-ebs", 
            "Tags": [
                {
                    "Value": "free", 
                    "Key": "fee"
                }
            ], 
            "Hypervisor": "xen", 
            "ImageOwnerAlias": "amazon", 
            "SriovNetSupport": "simple", 
            "ImageId": "ami-41a7ea71", 
            "State": "available", 
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda", 
                    "Ebs": {
                        "DeleteOnTermination": true, 
                        "SnapshotId": "snap-e7cf042f", 
                        "VolumeSize": 2, 
                        "VolumeType": "standard", 
                        "Encrypted": false
                    }
                }
            ], 
            "Architecture": "x86_64", 
            "ImageLocation": "amazon/amzn-ami-minimal-hvm-2014.09.1.x86_64-ebs", 
            "RootDeviceType": "ebs", 
            "OwnerId": "137112412989", 
            "RootDeviceName": "/dev/xvda", 
            "Public": true, 
            "ImageType": "machine", 
            "Description": "Amazon Linux AMI 2014.09.1 x86_64 minimal HVM EBS"
        }
    ]
}

2.2. 启动EC2实例

启动EC2需要的参数:

#启动一个t2.micro实例
(pythonenv)➜  aws_cli  aws ec2 run-instances --image-id ami-41a7ea71 --count 1 --instance-type t2.micro --key-name liyan --security-groups default
{
    "OwnerId": "995517354230", 
    "ReservationId": "r-ce1323c5", 
    "Groups": [], 
    "Instances": [
        {
            "Monitoring": {
                "State": "disabled"
            }, 
            "PublicDnsName": null, 
            "RootDeviceType": "ebs", 
            "State": {
                "Code": 0, 
                "Name": "pending"
            }, 
            "EbsOptimized": false, 
            "LaunchTime": "2014-11-26T09:42:46.000Z", 
            "PrivateIpAddress": "172.31.33.56", 
            "ProductCodes": [], 
            "VpcId": "vpc-ee3fe28b", 
            "StateTransitionReason": null, 
            "InstanceId": "i-a5495351", 
            "ImageId": "ami-41a7ea71", 
            "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
            "KeyName": "liyan", 
            "SecurityGroups": [
                {
                    "GroupName": "default", 
                    "GroupId": "sg-d32056b6"
                }
            ], 
            "ClientToken": null, 
            "SubnetId": "subnet-b97cd6dc", 
            "InstanceType": "t2.micro", 
            "NetworkInterfaces": [
                {
                    "Status": "in-use", 
                    "MacAddress": "02:9e:dd:9a:58:fe", 
                    "SourceDestCheck": true, 
                    "VpcId": "vpc-ee3fe28b", 
                    "Description": null, 
                    "NetworkInterfaceId": "eni-e574ba81", 
                    "PrivateIpAddresses": [
                        {
                            "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
                            "Primary": true, 
                            "PrivateIpAddress": "172.31.33.56"
                        }
                    ], 
                    "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
                    "Attachment": {
                        "Status": "attaching", 
                        "DeviceIndex": 0, 
                        "DeleteOnTermination": true, 
                        "AttachmentId": "eni-attach-dfb114e9", 
                        "AttachTime": "2014-11-26T09:42:46.000Z"
                    }, 
                    "Groups": [
                        {
                            "GroupName": "default", 
                            "GroupId": "sg-d32056b6"
                        }
                    ], 
                    "SubnetId": "subnet-b97cd6dc", 
                    "OwnerId": "995517354230", 
                    "PrivateIpAddress": "172.31.33.56"
                }
            ], 
            "SourceDestCheck": true, 
            "Placement": {
                "Tenancy": "default", 
                "GroupName": null, 
                "AvailabilityZone": "us-west-2b"
            }, 
            "Hypervisor": "xen", 
            "BlockDeviceMappings": [], 
            "Architecture": "x86_64", 
            "StateReason": {
                "Message": "pending", 
                "Code": "pending"
            }, 
            "RootDeviceName": "/dev/xvda", 
            "VirtualizationType": "hvm", 
            "AmiLaunchIndex": 0
        }
    ]
}

#查看实例的运行状态:
(pythonenv)➜  aws_cli  aws ec2 describe-instance-status
{
    "InstanceStatuses": [
        {
            "InstanceId": "i-a5495351", 
            "InstanceState": {
                "Code": 16, 
                "Name": "running"
            }, 
            "AvailabilityZone": "us-west-2b", 
            "SystemStatus": {
                "Status": "ok", 
                "Details": [
                    {
                        "Status": "passed", 
                        "Name": "reachability"
                    }
                ]
            }, 
            "InstanceStatus": {
                "Status": "ok", 
                "Details": [
                    {
                        "Status": "passed", 
                        "Name": "reachability"
                    }
                ]
            }
        }
    ]
}

#运行详情
(pythonenv)➜  aws_cli  aws ec2 describe-instances 
{
    "Reservations": [
        {
            "OwnerId": "995517354230", 
            "ReservationId": "r-ce1323c5", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "ec2-54-68-227-195.us-west-2.compute.amazonaws.com", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2014-11-26T09:42:46.000Z", 
                    "PublicIpAddress": "54.68.227.195", 
                    "PrivateIpAddress": "172.31.33.56", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-ee3fe28b", 
                    "StateTransitionReason": null, 
                    "InstanceId": "i-a5495351", 
                    "ImageId": "ami-41a7ea71", 
                    "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
                    "KeyName": "liyan", 
                    "SecurityGroups": [
                        {
                            "GroupName": "default", 
                            "GroupId": "sg-d32056b6"
                        }
                    ], 
                    "ClientToken": null, 
                    "SubnetId": "subnet-b97cd6dc", 
                    "InstanceType": "t2.micro", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "02:9e:dd:9a:58:fe", 
                            "SourceDestCheck": true, 
                            "VpcId": "vpc-ee3fe28b", 
                            "Description": null, 
                            "Association": {
                                "PublicIp": "54.68.227.195", 
                                "PublicDnsName": "ec2-54-68-227-195.us-west-2.compute.amazonaws.com", 
                                "IpOwnerId": "amazon"
                            }, 
                            "NetworkInterfaceId": "eni-e574ba81", 
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
                                    "Association": {
                                        "PublicIp": "54.68.227.195", 
                                        "PublicDnsName": "ec2-54-68-227-195.us-west-2.compute.amazonaws.com", 
                                        "IpOwnerId": "amazon"
                                    }, 
                                    "Primary": true, 
                                    "PrivateIpAddress": "172.31.33.56"
                                }
                            ], 
                            "PrivateDnsName": "ip-172-31-33-56.us-west-2.compute.internal", 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-dfb114e9", 
                                "AttachTime": "2014-11-26T09:42:46.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "default", 
                                    "GroupId": "sg-d32056b6"
                                }
                            ], 
                            "SubnetId": "subnet-b97cd6dc", 
                            "OwnerId": "995517354230", 
                            "PrivateIpAddress": "172.31.33.56"
                        }
                    ], 
                    "SourceDestCheck": true, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": null, 
                        "AvailabilityZone": "us-west-2b"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-e2ec30f3", 
                                "AttachTime": "2014-11-26T09:42:50.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/xvda", 
                    "VirtualizationType": "hvm", 
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}

2.3. 为EC2实例添加标签

aws ec2 create-tags --resources i-a5495351 --tags Key=Onwer,Value=liyan Key=Stack,Value=test

2.4. 访问EC2实例

参考:here

#ssh -i /path/key_pair.pem ec2-user@public_dns_name
(pythonenv)➜  aws_cli  aws ec2 get-console-output --instance-id i-a5495351
➜  hst-dev.aws  ssh -i /opt/app/ssh/hst-dev.aws/liyan.pem ec2-user@ec2-54-68-227-195.us-west-2.compute.amazonaws.com 

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2014.09-release-notes/
5 package(s) needed for security, out of 11 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-33-56 ~]$ 

如果当前实例没有绑定Key Pair的解决办法:here

2.5. 注销这个EC2实例

(pythonenv)➜  aws_cli  aws ec2 terminate-instances --instance-ids i-a5495351
{
    "TerminatingInstances": [
        {
            "InstanceId": "i-a5495351", 
            "CurrentState": {
                "Code": 32, 
                "Name": "shutting-down"
            }, 
            "PreviousState": {
                "Code": 16, 
                "Name": "running"
            }
        }
    ]
}

(pythonenv)➜  aws_cli  aws ec2 describe-instances
{
    "Reservations": [
        {
            "OwnerId": "995517354230", 
            "ReservationId": "r-ce1323c5", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": null, 
                    "RootDeviceType": "ebs", 
                    "State": {
                        "Code": 48, 
                        "Name": "terminated"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2014-11-26T09:42:46.000Z", 
                    "ProductCodes": [], 
                    "StateTransitionReason": "User initiated (2014-11-26 11:00:06 GMT)", 
                    "InstanceId": "i-a5495351", 
                    "ImageId": "ami-41a7ea71", 
                    "PrivateDnsName": null, 
                    "KeyName": "liyan", 
                    "SecurityGroups": [], 
                    "ClientToken": null, 
                    "InstanceType": "t2.micro", 
                    "NetworkInterfaces": [], 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": null, 
                        "AvailabilityZone": "us-west-2b"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [], 
                    "Architecture": "x86_64", 
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 
                        "Code": "Client.UserInitiatedShutdown"
                    }, 
                    "RootDeviceName": "/dev/xvda", 
                    "VirtualizationType": "hvm", 
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}

MainWiki: AWS_CLI_EC2_GettingStarted (last edited 2014-11-26 16:39:59 by twotwo)