AWS study notes (2)--CLI management of 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 will be output after successful execution.

 

create-tags

When viewing AMIs through the EC2 Management Console, the first item in the list is Name. After executing the create-image command, this item is empty, and you need to execute:

$ 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

A snapshot is created when an image is created. To delete an image, first execute deregister-image and then delete-snapshot.

Query SnapshotId:

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

 

delete snapshot

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

 

describe-images

View all images, including public ones (a lot of AWS public images will be displayed)

$ aws ec2 describe-images

 

View images created by your account

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

 

View 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

Note: --security-group-ids must be used

 

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-volume

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

Copy an object from S3 to local
$ aws s3 cp s3://my_bucket/my_folder/my_file.ext my_copied_file.ext

Copy an object to S3
$ aws s3 cp my_copied_file.ext s3://my_bucket/my_folder/my_file.ext

Copy an object from S3 to another 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.

Sync local files to S3

$ aws s3 sync . s3://mybucket

 

Download the entire S3 bucket to a local directory

$ aws s3 sync s3://remote_S3_bucket local_directory

 

Sync between 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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327026334&siteId=291194637