Wednesday, July 31, 2019

AWS Command Line Interface (CLI) commands


$ pip install aws-shell
$ aws help
$ aws s3 help
$ aws s3 mb help
$ aws configure 
$ aws configure list

AWS CLI Command Completer

1.) $ PATH=/usr/local/aws/bin:$PATH
2.) $ complete -C '/usr/local/aws/bin/aws_completer' aws
3.) Test it works: aws s3 M TAB
4.) MB or MV

What it does:  Enables command completion for the AWS CLI, enter a partial command and press the Tab key to see the available commands.  Add it to your ~/.bashrc


Why it's cool:  Great for when you can't quite remember the name of a command, also good for exploring the CLI without reading the documentation!!!

S3 List Buckets
$ aws s3 ls

What it does:  Lists out the buckets in your AWS.


Why it's cool:  It is one of the simplest commandsto test if the CLI is working.  You type in and it will spit out a list of all your buckets in your AWS account.


Copy a File From One S3 Bucket to Another S3 Bucket
aws s3 cp s3://source-bucket/test.txt s3://destination-bucket/ --storage-class REDUCED_REDUNDANCY

What it does:  The cp command specifies copy and, as can be seen, the storage class of REDUCED_REDUNDANCY has been specified for the storage of the file in the destination bucket.


Why it's cool:  If you are copying a large filethen make sure to specify the required storage class at the time of the copy as this will save time.  Changing the storage class of a large file in S3 can take a while so it's quickest to do so at the time of the creation of the file.


Sync S3 with Another Location
aws --region us-east-2 s3 sync ./new-stuff  s3://my-awesome-bucket --delete

What it does:  Syncs S3 and the folder of your choice.  Because we use the delete marker, this will delete any objects in the destination bucket that are not in the source bucket.

Why it's cool:  If you have bucket or files that you want to be mirrored in S3, this command does exactly that.  It mirrors two locations, like twins.

Delete an S3 Bucket and all its Contents
aws s3 rb s3://bucket-name –-force


What it does:  This will delete the bucket in its entirety, very quickly.

Why it's cool:  If you have to ever had to delete a substantial number of items in S3, you know that this can be a little time consuming.  Not anymore !!!

Instances that are currently stopped
aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped


What it does:  This command will describe all of your stopped instances.

Why it's cool:  Shows the reason they were stopped.

                          "StateReason":{"Message":"Client.UserInitiatedShutdown:User initiated shutdown",
                     "Code":"Client.UserInitiatedShutdown"
}

History for m1.xlarge Instances for a particular day
aws ec2 describe-spot-price-history --instance-types m1.xlarge --start-time 2019-03-17T07:08:09 --end-time 2019-03-18T08:09:10


What it does:  Returns the Spot prices of the instance types within the time range that you specified and the time when the price changed.

Why it's cool:  Great for researching the historical cost of Spot instances before you go ahead and bid.  You can also narrow the results by referencing a particular AZ, e.g.
--availability-zone eu-west-1c

List of all your Key Pairs
aws ec2 describe-key-pairs --region us-west-1

What it does:  Returns the names of all the key pairs you have configured in the specified region.

Why it's cool:  Great for anyone who keeps forgetting the name of their active key pairs when logging into EC2 instances!!!

Describe your EC2 instances in Another Account
aws ec2 describe-instances --profile production

What it does:  Describes your EC2 instances from the profile called "production".  You can configure the CLI to have multiple profiles (i.e. have multiple AWS accounts).

Why it's cool:  The AWS CLI doesn't just work in one account,it works in as many accounts as you want to configure.  By using the --profile option, you can query any account youlike from the same command line interfaces.  So you could have a "Dev" profile and a "production" profile.
$ aws configure --profile production

Retrieve Information About IAM Users
aws iam get-user

What it does:  Retrieves information about the specified IAM user, including the user's creation date, path, unique ID, and ARN.  If you do not specify a user name, it returns the details of the user whose credentials you used to run the command.

Why it's cool:  Great for quickly getting information about IAM users or confirming which IAM user your AWS CLI is configured to use!!!

Generate a Secure Password
aws secretsmanager get-random-password

What it does:  Generates a complex password.

Why it's cool:  Rather than cranking up that password manager or searching for a site to generate a password for you, just use this command to get back a long, complex password suitable for any account or service.

Validate the Content on a CloudFront Distribution
aws cloudfront create-invalidation --distribution-id ABC123 --paths /*

What it does:  Clears the cache on your CloudFront distribution so that all your new content is immediately available.

Why it's cool:  No more worrying about TTL and old content.  The viewers of your content can see the latest and greatest!!!

Validate a CloudFormation Template Before Upload
aws cloudformation validate-template --template-body file://sampletemplate.json


What it does:  Validates your CloudFormation template before deploying to CloudFormation.

Why it's cool:  It saves you time by ensuring you have a properly structured template.

Generate a Custom Narration of a Text
aws polly synthesize-speech --output-format mp3 --text "Hello there" --voice-id Brian hello.mp3


What it does:  Creates a text-to-speech narration of text in MP3 format.

Why it's cool:  Maybe you want to speech-enable your BASH script to provide positive supportive messages during your next production build.  Or maybe you want to embarass the person who broke the build over the intercom :-)

Translate from One Language to Another
aws translate translate-text \
--text "Keep Being Awesome" \
--source-language-code EN \
--target-language-code FR

What it does:  Translates text from a source language to a target language through the command line.

Why it's cool:  Quickly translate text from the command line --- suitable for automation scripts where you might want to enable localized output or logging.

Detect the Sentiment in a String of Text
aws comprehend detect-sentiment --text 'This sure is helpful' --language-code en

What it does:  Detects the sentiment in a string of text

Why it's cool:  It can tell you if a string of text is positive, negative or neutral in those situations that you can't figure out.