# Hands-on AWS CLI Basics

We already know how to configure AWS account using AWS CLI. If you don't know yet how to do that then please spare a couple of minutes and go through my previous blog
[Beginner's Guide to AWS CLI: Download, Install & Configure](https://nayeemsweb.hashnode.dev/beginners-guide-to-aws-cli-download-install-and-configure). 

Today our aim is to know some basic commands of AWS CLI. As we know there's a ton of cloud services of Amazon. So, let's get started . . . 

### Where the credentials are stored?

When we run the command `aws configure` then we need to pass the credentials. 
```
aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
```
This configuration is saved as `default` profile in your local machine. But do you know where these credentials are stored? It's actually stored in the path: `C:\Users\{YourUserName}\.aws` in Windows machine. In the `.aws` folder we will get 2 files named `config` and `credentials`. We open these files with `Notepad`.

![aws_files.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654670185939/SLgqQn8gK.png align="left")

So whenever you open the AWS CLI, you will have to use this `default` profile. 

### Use a different Profile

Let's see how many profiles are there in your machine.
```
aws configure list-profiles
```
This command will list you down all the configured aws profiles stored in your machine. So according to the above mentioned screenshot AWS CLI should show only one profile name which is `default`.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654670545856/gn8C5lAEY.png align="left")

Now, let's add a new profile. Run this command - `aws configure --profile {CustomProfileName}`
```
aws configure --profile nayeem
```
Then you will be asked to provide the credentials as before. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654671003438/FUKF7Ihre.png align="left")

Cross check the `config` and `credentials` files in the`.aws` folder and you will see `[profile nayeem]` is added.

We are done with configuring the new profile. Now we can switch to this new profile using the following command- `setx AWS_PROFILE <YourProfileName>`
```
setx AWS_PROFILE nayeem
``` 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654671968485/LbZ-MihAg.png align="left")

**Let's pick a simple and easy service using which we can practice and learn the AWS CLI basics. We think Amazon S3 will be a perfect match for this purpose.**
### Amazon S3
S3 stands for Simple Storage Service. [Details](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)  
#### List All S3 Buckets
To get the list of all available buckets run the list `ls` command-
```
aws s3 ls
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654679167598/FpmKLiB-b.png align="left")

#### Create a Bucket
For [making a  bucket](https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html) run the `mb` command in this format- `aws mb s3://{BucketName}`
```
aws s3 mb s3://test-bucket-using-cli
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654682296492/dn8Rwu_t5.png align="left")
Let's check it on `S3 Management Console` - 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654682572698/ivh0RZIt-.png align="left")
Done!!!

#### Upload Files to S3 Bucket
Uploading file(s) means actually we will be copying file(s) from one location to another. It may be from Local to S3 bucket or S3 bucket to Local or one S3 bucket to another S3 bucket. In our case we will be uploading one single file to our newly created S3 bucket using the `cp` command. In our `C:\\` drive we have created a folder named `Files` and there is a text file named `file1.txt`. [Details](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html#examples)
```
aws s3 cp C:\Files\file1.txt s3://test-bucket-using-cli/file1.txt
```
Output:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654683924929/XtEhoVNR1.png align="left")

Again, let's upload all files from a particular local folder to S3 bucket. 
```
aws s3 cp C:\Files\ s3://test-bucket-using-cli/ --recursive
```
Output:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654684097810/KwFlPwW_t.png align="left")


#### Download Files from S3 Bucket
It's totally similar to what we did in the previous section. Just the difference is that first we need to mention the S3 bucket location and then the local storage location. Because, we are downloading from S3 bucket to our local storage.
```
aws s3 cp s3://test-bucket-using-cli/ C:\Files\DownloadedFiles --recursive
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654684568996/cre7kav-O.png align="left")

#### Delete Files from S3 Bucket
Deleting is actually [removing object](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html) from S3. The keyword used for removing is `rm`. If you want to remove a single file then run this command- 
`aws s3 rm s3://{YourBucketName}/{YourFileName.extension}`. For Example:
```
aws s3 rm s3://test-bucket-using-cli/file2.txt
```
And if you want to remove all files from the bucket then run this command- 
`aws s3 rm s3://{YourBucketName} --recursive`. For Example:
```
aws s3 rm s3://test-bucket-using-cli --recursive
```

#### Delete S3 Bucket
To [delete a bucket](https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html) we need to use the keyword `rb`. Use this command - `aws s3 rb s3://{YourBucketName}`
```
aws s3 rb s3://test-bucket-using-cli
```
Output:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654685986478/FFUiBkp_0.png align="left")

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654685950727/RkXIgq9F0.png align="left")

In this way, following the AWS CLI documentation we can practice and learn the command line interface commands. Hope that this article would help and strengthen you to move forward with Cloud Computing career.


