Services >> Cloud >> AWS >> CLI >> Examples of aws cli

 

Initial configuration

aws configure
AWS Access Key ID [None]: <your-access-key-id>
AWS Secret Access Key [None]: <your-secret-key>
Default region name [None]: <your desired default region>
Default output format [None]: json

 

Note:

1. If you have multiple AWS users configured in your CLI, replace aws with aws --profile profilename

2. If you want to put the command line parameters in a .json file use --cli-input-json file://yourparms.json

the .json file can be like the following

{
    "DryRun": true,
    "InstanceIds": [
        "XXXXXXXX"
    ],
    "Force": true
}

3.  If you want to use shorthand format for the CLI options

Linux, macOS, or Unix

--option key1=value1,key2=value2,key3=value3

Windows PowerShell

--option "key1=value1,key2=value2,key3=value3"
 

 Service Command Purpose
EC2 aws ec2 describe-regions Describes one or more regions that are currently available to you
EC2 aws ec2 start-instances --instance-ids instance_id  
EC2 aws ec2 stop-instances --instance-ids instance_id  
EC2 aws ec2 terminante-instances --dry-run --instance-ids instance_id  
EC2 aws ec2 create-tags --resources instance_id --tags Key=Department,Value=Finance  
EC2 aws ec2 describe-volumes  
EC2 aws ec2 attach-volume  --volume-id vol-1d5cc8cc --instance-id instance_id --device /dev/sdh  
EC2 aws ec2 run-instances
   --dry-run
   --image-id ami-08111162
   --count 1
   --instance-type t1.micro
   --key-name MyKeyPair
   --security-groups my-ami-security-group
 
EC2

aws ec2 run-instances
   -- image-id ami-xxxxxxxx
   -- count 1
   -- instance-type t2.micro
   -- key-name MyKeyPair
   --security-group-ids sg-xxxxxxxx
   -- subnet-id subnet-xxxxxxxx

 
EC2 aws ec2 reboot-instances --instance-ids instance_id  
EC2 aws ec2 modify-instance-attribute --instance-id instance_id --instance-type "{\"Value\": \"m1.small\"}"  
EC2 aws ec2 create-image --instance-id instance_id --name "Dev AMI" --description "AMI for development server"  
EC2 aws ec2 describe-images --image-ids ami-2d574747  
EC2 aws ec2 deregister-image --image-id ami-2d574747 && aws ec2 delete-snapshot --snapshot-id snap-4e665454  
EC2 aws ec2 delete-snapshot --snapshot-id snap-4e665454  
EC2 aws ec2 modify-instance-attribute --instance-id instance_id --disable-api-termination  
EC2 aws ec2 modify-instance-attribute --instance-id instance_id --no-disable-api-termination  
EC2 aws ec2 get-console-output --instance-id instance_id  
EC2 aws ec2 monitor-instances --instance-ids instance_id  
EC2 aws ec2 unmonitor-instances --instance-ids instance_id  
EC2 aws ec2 describe-key-pairs  
EC2 aws ec2 create-key-pair --key-name dev-servers  
EC2 aws ec2 delete-key-pair --key-name dev-servers  
EC2


aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key == `Environment`].Value)] | [].InstanceId' --output text

for i in `aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key == \`Environment\`].Value)] | [].InstanceId' --output text`;do aws ec2 stop-instances --instance-ids $i;done;
 

List InstanceId that does not have Environment tag

Loop through ech instance_id and stop the instance
 
EC2 (security group) aws ec2 authorize-security-group-ingress --group-name "Bastion" --protocol tcp --port 22 --cidr YOUR-PUBLIC-IP-ADDRESS/32 edit ec2 security group to add ingress rule of tcp port 22 for a specified source cidr
 EC2 aws ec2 revoke-security-group-ingress --group-name "Bastion" --protocol tcp --port  22 --cidr YOUR-PUBLIC-IP-ADDRESS/32 edit ec2 security group to remove ingress rule of tcp port 22 for a specified source cidr
 EC2

aws ec2 run-instances --launch-template LaunchTemplateId=template-id,Version=2

or

:: when launching template that is using non default VPC, should specify the subnet id that is in the non-default VPC

aws ec2 run-instances --launch-template LaunchTemplateId=template-id,Version=1 --subnet-id=subnet-xxx

 

launch ec2 instance from launch template specified
EC2

For all instances

aws ec2 describe-instance-status

For specific instance

aws ec2 describe-instance-status --instance-ids i-1234567890abcdef0

 

To view the instance(s) status
EC2
aws ec2 describe-instance-status --filters Name=instance-status.status,Values=impaired
To get the status of all instances with an instance status of impaired

 

 Service Command Purpose
S3

aws s3 cp --recursive /var/www/html/wp-content/uploads/ s3://wordpressmedia16mfb/

aws s3 sync --delete /var/www/html/wp-content/uploads/ s3://wordpressmedia16mfb/

To set up synchronization of your contents from EC2 to S3
S3 aws s3 mb s3://my-bucket make a bucket
S3 aws s3 ls list all buckets
S3 aws s3 ls s3://my-bucket list contents of specific bucket
S3 aws s3 cp file s3://my-bucket/file upload a file to a bucket
S3 aws s3 cp s3://my-bucket/file file download a file from a bucket
S3 aws s3 cp s3://bucket1/file s3://bucket2/file copy a file between buckets

 

 

  Command Purpose
route53

 aws route53 change-resource-record sets \
         --hosted-zone-id <value>
         --change-batch <value>
        [--cli-input-json <value>]
        [--generate-cli-skeleton <value>]

 
     

 

 Service Command Purpose
EBS aws ec2 create-snapshot --description CLI --volume-id YOUR-VOLUME-ID  
     

 

 

 Service Command Purpose
CloudWatch aws cloud watch put-metric-data --namespace Lab --metric-name YOUR-INITIALS --value 42  to push a metric to cloudwatch
     

 

 

 

 Service Command Purpose
 SQS aws sqs get-queue-attributes --queue-url  \
https://sqs.<region>.amazonaws.com/<accountno>/<queuename>  \
--attribute-names ApproximateNumberOfMessages
get the number of messages waiting in the queue (ApproximateNumberOfMessages)
     

 

 

 

 Service Command Purpose