AWS学习笔记(二)--CLI管理Image,Instance,Snapshot,S3

1. Image

create-image

$ aws ec2 create-image --instance-id i-825465ba --name "Prod Template" --description "Prod Template" --no-reboot

执行成功后会输出ImageId.

create-tags

通过EC2 Management Console查看AMIs时,列表中的第一项是Name,执行create-image命令后,这项是空的,还需执行:

$ aws ec2 create-tags --resources ami-19af7b74 --tags "Key=Name,Value=Prod Template"

modify-image-attribute

To make an AMI public

$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Add\": [{\"Group\":\"all\"}]}"

To make an AMI private

$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Remove\": [{\"Group\":\"all\"}]}"

To grant launch permission to an AWS account

$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Add\": [{\"UserId\":\"123456789012\"}]}"

 
To removes launch permission from an AWS account

$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Remove\": [{\"UserId\":\"123456789012\"}]}"

deregister-image

After you deregister an AMI, it can't be used to launch new instances. This command does not delete the AMI.

$ aws ec2 deregister-image --image-id ami-19af7b74

Delete Image

创建image时会创建一个snapshot,要删除image,先执行deregister-image,再执行delete-snapshot即可。

查询SnapshotId:

$ aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId] --filter Name=description,Values=*ami-19af7b74*

删除snapshot

$ aws ec2 delete-snapshot --snapshot-id snap-8e2718bd

describe-images

查看所有image,包含public的(会显示很多AWS public image)

$ aws ec2 describe-images

查看自己帐号创建的image

$ aws ec2 describe-images --owners self --query Images[*].[Name,ImageId]

查看private image

$ aws ec2 describe-images --query Images[*].[Name,ImageId,OwnerId] --filter "Name=is-public,Values=false"

2. Instance

run-instances

Launches the specified number of instances using an AMI for which you have permissions.

To launch an instance in EC2-Classic
$ aws ec2 run-instances --image-id ami-1a2b3c4d --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups MySecurityGroup

To launch an instance in EC2-VPC
$ aws ec2 run-instances --image-id ami-abc12345 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-903004f8 --subnet-id subnet-6e7f829e

注意: 必须使用--security-group-ids

start-instances

$ aws ec2 start-instances --instance-ids i-1234567890abcdef0

stop-instances

$ aws ec2 stop-instances --instance-ids i-1234567890abcdef0

reboot-instances

$ aws ec2 reboot-instances --instance-ids i-1234567890abcdef5

terminate-instances

$ aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

describe-instances

To describe all instances with a Name=A000 tag
$ aws ec2 describe-instances --filters "Name=tag:Name,Values=A000"

To describe all EC2 instances that have an instance type of m1.small or m1.medium that are also in the us-west-2c Availability Zone
$ aws ec2 describe-instances --filters "Name=instance-type,Values=m1.small,m1.medium" "Name=availability-zone,Values=us-west-2c"

monitor-instances

By default, Amazon EC2 sends metric data to CloudWatch in 5-minute periods. To send metric data for your instance to CloudWatch in 1-minute periods, you can enable detailed monitoring on the instance.

To enable detailed monitoring for an existing instance

$ aws ec2 monitor-instances --instance-ids i-1234567890abcdef0

To enable detailed monitoring when launching an instance
$ aws ec2 run-instances --image-id ami-09092360 --monitoring Enabled=true...

To disable detailed monitoring
$ aws ec2 unmonitor-instances --instance-ids i-1234567890abcdef0

3. Snapshot & Volume

create-snapshot

Creates a snapshot of an EBS volume and stores it in Amazon S3. You can use snapshots for backups, to make copies of EBS volumes, and to save data before shutting down an instance.

$ aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my root volume snapshot."

create-volum

To create a new volume
$ aws ec2 create-volume --size 80 --region us-east-1 --availability-zone us-east-1a --volume-type gp2

To create a new Provisioned IOPS (SSD) volume from a snapshot
$ aws ec2 create-volume --region us-east-1 --availability-zone us-east-1a --snapshot-id snap-066877671789bd71b --volume-type io1 --iops 1000

attach-volume
Attaches an EBS volume to a running or stopped instance and exposes it to the instance with the specified device name.
$ aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf

Available Device Names

delete-volume

Deletes the specified EBS volume. The volume must be in the available state (not attached to an instance).

$ aws ec2 delete-volume --volume-id vol-049df61146c4d7901

4. S3

mb
Creates an S3 bucket.

$ aws s3 mb s3://mybucket

cp

将一个对象从S3 复制到本地
$ aws s3 cp s3://my_bucket/my_folder/my_file.ext my_copied_file.ext

将一个对象复制到S3
$ aws s3 cp my_copied_file.ext s3://my_bucket/my_folder/my_file.ext

将一个对象从S3复制到另一个S3
$ aws s3 cp s3://my_bucket/my_folder/my_file.ext s3://my_bucket/my_folder/my_file2.ext

sync

Syncs directories and S3 prefixes. Recursively copies new and updated files from the source directory to the destination.

同步本地文件到S3

$ aws s3 sync . s3://mybucket

将整个S3 存储桶下载到本地目录

$ aws s3 sync s3://remote_S3_bucket local_directory

S3间的同步

$ aws s3 sync s3://mybucket s3://mybucket2

rb

Deletes an empty S3 bucket. A bucket must be completely empty of objects and versioned objects before it can be deleted. However, the --force parameter can be used to delete the non-versioned objects in the bucket before the bucket is deleted.

$ aws s3 rb s3://mybucket --force

猜你喜欢

转载自billben.iteye.com/blog/2328925