Let's work with AWS CLI
Tags: beginners, aws, cloud
Published: 2023-01-16 04:42:58

In this blog, we will learn how to create below resources using AWS CLI

1. Create a key pair
2. Create a security group
3. Launch an instance using the above created key pair and security group.
4. Create an EBS volume of 1 GB.
5. The final step is to attach the above created EBS volume to the instance you created in the previous steps.

First we have to create an IAM User.

So Amazon has IAM (Identity and Access Management) service where we can create user and can give limited access to that user.

**Step 1:** To give user name and here in AWS Access type give programmatic access so that it enables access key & secret key for AWS CLI.

![create user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kiwgg93h4fom9xx05gu.png)

**Step 2:** Give permissions to that user.

_What are **Permissions**?_
_Permissions enable you to perform actions on AWS resources. When a new user or group is created, it has no permissions and a policy must be attached to allow actions to be taken on AWS resources._

_You can assign permissions to all AWS identities (users, groups, and roles). Permissions are assigned in the following two ways:_

_**Identity-based**: Policies attached directly to users, groups, or roles_
_**Resource-based**: Policies attached to AWS resources, such as S3 Buckets, ECR Repositories, and more_

**Step 3:** Now AWS, will generate Access Key & Secret Key for that user. We have to use this Access Key & Secret Key (save these) to login into our AWS CLI and then click create and here you can see IAM user is created.

![IAM User created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3a1fygpondbje0pruji.png)

Install AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Now in Windows Command Prompt, there’s a command `aws configure` to login with aws cli user created above.

![Here, provide the access key & secret key to login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3889fm2y47ykg7hssnt.png)

🧩 Command syntax is :

`aws <command> <subcommand> <options>`

Now, firstly we have to create our own key pair for that we take help of AWS so here to take help we write :

`aws ec2 help`: This gives all the commands available in ec2.

![ec2 help command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ncqbglzew0a0vjqc5qb.png)

Now, here to create new key pair you can see subcommand:

![Create new key pair subcommand](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hw5jpo88wntayniodo10.png)

Now, we can create new key pair by command:

```
aws ec2 create-key-pair — key-name TaskKeyPair
```

![create new key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10xvvr634f320vy2e1vs.png)

Now, we can create new security group by command:

```
aws ec2 create-security-group — group-name TaskSG — description “Task Security Group”
```

![create new security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elkzgzr0fv7c89t0cpob.png)

By using this newly created key pair & security group we have to launch new instance:

```
aws run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — subnet-id subnet-37666f5f — count 1 — security-group-ids sg-082e7e59275801fcd — key-name TaskKeyPair
```

![launch new instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12f1wb2u5alcdb72d89t.png)

You can see the created instance using CLI OR GUI:

![CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsvyf8hb54nsb7wfiutg.png)

![GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rac6ctsa737db5gno34u.png)

We have to create an EBS volume of 1 GB. 

```
aws ec2 create-volume — volume-type gp2 — size 1 — availability-zone ap-south-1a
```

![EBS volume CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t73hq3rujtzbprr7c2kn.png)

![GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6agqxr7jy9ar4phssk02.png)

Now, finally we have attach the Volume to the instance that we created now. we can attach the volume by following command:

```
aws ec2 attach-volume — volume-id vol-0522f336c28172eb1 — instance-id i-07345102940d94e27 — device /dev/sdf
```

_Device name can be “sdf or svdh”_

![attach the volume CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9ew897iva02hrr4a5xo.png)

![attach the volume GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcjlq3c7zznljbtb4301.png)

You can see Volume of 1 GB is in use.

You can find me here: {% embed https://vrukshali.bio.link/ %}