Miscellanous AWS CLI Example
1
2
3
4
5
6
7
| aws ec2 run-instances
--image-id ami-xxxxxxxxxx
--network-interface "NetworkInterface='eni=xxxxxx',DeviceIndex=0"
--key-name MY_KEY
--instance-type "m4.xlarge"
--disable-api-termination
--iam-instance-profile "Arn=arn:aws-iso:iam::1234567890:instance-profile/myiamprofile"
|
Lines
- Base command
- ID of AMI
- Network Interface ID and its placement (if known). You can opt instead to provide subnet-id if you want a new interface to be used
- Key Name
- Instance Type
- Disable API termination (remove if you want to enable API termination)
- IAM instance profile (remove if you don't want to use IAM profile)
Other options and their defaults
- Security Group: Default
- Shutdown Behavior: Stop
- EBS Optimized: False
- Enhanced Monitoring: False
Create new AMI
1
2
3
4
| aws ec2 create-image
--instance-id i-xxxxxxxxxxxxxxxxxx
--name MY_AMI_NAME_01
--description "My Description"
|
Tagging Resource
aws ec2 create-tags --resources SOME_ID --tags "Key=MYKEY,Value='MYVALUE'..."
Notes:
- Any EC2 resource ID can be used
- tag must be a key, value pair separated by a comma
- Multiple tags can be provided, they must be separated by a space
View all of my AMIs
aws ec2 describe-images --owners "self"
View all Instances of some account
aws ec2 describe-instances --filters "Name=owner-id,Values=XXXXXX"
View all snapshots of some account
aws ec2 describe-snapshots --owner-ids XXXXXXX
Remove Termination Protection
1
2
3
| aws ec2 modify-instance-attribute
--instance-id i-xxxxxxxxxxx
--no-disable-api-termination
|
Terminate Instance
1
2
| aws ec2 terminate-instances
--instance-id i-xxxxxxxxxxx
|
Create Volume from Snapshot
1
2
3
4
5
| aws ec2 create-volume
--snapshot-id snap-xxxxxxxxxx
--size 50
--availability-zone us-east-1a
--volume-type gp2
|
Copy single file to S3
aws s3 cp filename.log s3://bucketname
Copy directory to S3
aws s3 cp \\path\directory\ s3://bucketname/prefix --recursive
Note
- Case sensitive
- Empty sub-directories will be ignored
Copy with filter (only copy .log files from all path and sub-path)
aws s3 cp \\path\directory\ s3://bucketname/prefix/ --exclude '*' --include '*.log' --recursive
Note
- Exclude everything but .log extensions
- Order of operation is important
Copy from bucket to bucket
aws s3 cp s3://bucketA s3://bucketB --recursive
Sync local content to bucket
aws s3 sync \\localpath\ s3://bucketA/path/ --exclude '*' --include '*.log' --delete
Note
- Delete flag ensures what is deleted at source is also deleted at destination
- Recurse flag is always assumed
Sync local to bucket except a directory
aws s3 sync s3://bucketA/path/ \\localpath\test\ --exclude 'Special/*'
S3 Permissions
Use "private" default - only "me" is granted permission
aws s3 cp filename.txt s3:/bucketA/path/
Also allow publc read
aws s3 cp filename.txt s3:/bucketA/path/ --acl public-read
Also allow public read/write
aws s3 cp filename.txt s3:/bucketA/path/ --acl public-read-write
Give owner of bucket full control too
aws s3 cp filename.txt s3:/bucketA/path/ --acl bucket-owner-full-control
Upload Bucket Policy
aws s3api put-bucket-policy --bucket myBucket --policy file://myPolicy.json
Notes
- myPolicy.json file is expected in the current directory
Various other EC2 describes
- Customer Gateways: aws ec2 describe-customer-gateways
- Network ACL: aws ec2 describe-network-acls
- ENIs: aws ec2 describe-network-interfaces
- Route Table: aws ec2 describe-route-tables
- Security Group: aws ec2 describe-security-groups
- Key Pairs: aws ec2 describe-key-pairs
- Subnets: aws ec2 describe-subnets
- VPN GWs: aws ec2 describe-vpn-gateways
- VPCs: aws ec2 describe-vpcs
- Peering Connections: aws ec2 describe-vpc-peering-connections
- VPN Connections: aws ec2 describe-vpn-connections