This is a very simple exploration of AWS EC2 Instances from the AWS CLI. To get started with the CLI first install it, then use aws configure to configure it for your account and region.

Create volume, create an instance with user data, attach the volume to an instance, manipulate the instance, take a snapshot, copy the snapshot, create an AMI, register the AMI, copy the AMI.

Create volume

List Volumes

aws ec2 describe-volumes --region us-west-2

Delete Volumes

aws ec2 delete-volume --region us-west-1 --volume-id vol-0a0deda1b7aa09c35

Create Volume

Encrypt the volume using –encrypted true; optionally use -–kms-key-id <arn> to use KMS master key

aws ec2 create-volume --region us-west-2 --availability-zone us-west-2a --size 80 --volume-type gp2 --tag-specifications 'ResourceType=volume,Tags=[{Key=purpose,Value=production},{Key=cost-center,Value=mystuff}]'

Filtering the Volume List

aws ec2 describe-volumes --region us-west-2 --filters Name=tag-key,Values="purpose" Name=tag-value,Values="production"

Create Instance

aws ec2 run-instances --image-id ami-6df1e514 --count 1 --instance-type t2.micro --key-name account-2 --security-groups the-new-SG --tag-specifications 'ResourceType=instance,Tags=[{Key=purpose,Value=production},{Key=cost-center,Value=mystuff}]'

Describe Instances

aws ec2 describe-instances
aws ec2 describe-instances  --filters "Name=tag:Purpose,Values=production"

Start/Stop/Terminate Instances

aws ec2 stop-instances --instance-id i-07423f36098de7b0c
aws ec2 stop-instances --instance-id i-07423f36098de7b0c
aws ec2 terminate-instances --instance-id i-07423f36098de7b0c

EBS volumes attached at creation time will have the DeleteOnTerminate flag set to true, and any new ones will have it set to false

Create AMI

If this is an EBS backed instance there is no need to register it…

aws ec2 create-image \
--instance-id i-07423f36098de7b0c  \
--name mygoodness-v1

–block-device-mappings can be specified for the image as well…

–no-reboot –reboot (to insure file system integrity)

Copy AMI to Another Region

aws ec2 copy-image --source-image-id ami-e0b3ab99 --source-region us-west-2 --region us-east-1 --name "mygoodness-v2"

Use –kms-key-id to encrypt...

Snapshot volumes

Encrypted volumes -> encrypted snapshots

aws ec2 create-snapshot --volume-id vol-08bf38a3b26ac47a4 

encrypted snapshots -> encrypted snapshot unencrypted snapshot + --encrypted -> encrypted snapshot

aws ec2 copy-snapshot --source-region us-west-2 --source-snapshot-id snap-066877671789bd71b --description "This is my copied snapshot." --region us-east-1 
  delete-snapshot

Handy stuff

Find tagged resources of a resource type

aws ec2 describe-tags --region us-west-2 --filters Name=resource-type,Values="volume" Name=tag-key,Values="purpose" Name=tag-value,Values="production"

Find tagged resources of any type

aws ec2 describe-tags --region us-west-2 --filters  Name=tag-key,Values="purpose" Name=tag-value,Values="production"

Instance Meta-Data

There is a ton of meta-data available about each EC2 instance available via this handy URL: curl http://169.254.169.254/latest/meta-data/ from the instance itself.