Using AWS CLI(command line interface)
What will be covered in this article:
β Prerequisites
β Will create a key pair .
β Create a security group.
β Launch an instance using the above created key pair and security group.
β Create an EBS (Elastic Block Storage) volume of 1 GB.
β Lastly, attach the above created EBS volume to the instance created in above steps.
Note: All of the above steps can be achieved via gui as well. But the main reason for doing all this using cli is that we get more flexibility.
Prerequisites
πBefore we start using cli to launch instances and create key pair and all, we need to first install aws cli to our laptop. Below is the link from where you can download aws cli v-2
π link to download : AWSCLIV2.msi
π Once downloaded you can check the version of aws installed :
aws ~~version
πNow you need to log in to aws IAM user account and than we can implement any of the above mentioned steps.
π In order to use AWScli, while creating IAM user we need to check the box that says Programmatic access. This will allow us to use AWS services for this following IAM user through cli.
π In the next step attach following policy .
π Once user is created, you will find this page where you will be given access key and secret access key. This are very important and are not supposed to be given to anyone as with this two key, we can login to IAM user account through cli.
π Now from the command line we can login to the IAM user account we created. For that we use following command
βaws configureβ
πNow you can go on with bellow steps .
β Step : 1
Create key pair
Here in the image, the command highlighted in yellow is used to create a new key pair. The key here is not saved to your local device and if you want to do ssh login, than you need private key pair. So we will copy block of code from βkeyMaterialβ, that is inside red bracket, and save that file as .pem file . Than to use it to do remote login using putty, we will convert it to .ppk file.
Note: After copying KeyMaterial, we need to remove β\nβ wherever written and provide a new line as it is mentioning. Without doing this the key wonβt be converted to .ppk file
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
β Step : 2
create a security group.
We create or specify a security group so that two or more instances can communicate effectively and share information based on the inbound/outbound rules we set .
Here i have created security group named βcli-sgβ and provided some description as in to identify what that security group is doing or for which instance it is attached to . The output here shows the groupId of the security group created.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
β Step : 3
launching new instance
Things required for launching a new instance :
1. Image-id (ami image) : here we have used Red-hat linux image.
2. Count : count defines how many instances you want to launch at the same time
3. Instance type : here βt2.microβ for free tier users.
4. Key-name : here we use key name and not key-id . we have attached βclikey-1β that we previously created. A key can only be used in same region only but zone can be any.
5. Security Group Id : Id for the βcli-sgβ security group we created before .
6. Subnet-id : subnet is important. As you need to specify which zone of which region you like to launch new instance. Here βsubnet-7022318β is subnet-id for mumbai region zone-a.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
βStep : 4
Create 1 GB size of EBS volume.
To create EBS volume we need three information.
- Volume-type : here we have used βgp2β volume type.
2. Size of Volume: Here 1 GB of EBS volume .
3. Availability zone : here we have created volume in βap-south-1aβ i.e. Mumbai region zone-1a.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
β Step : 5
Attaching the volume to instance created before.
- While attaching the volume we have to provide volumeβs device name . In this case β/dev/sdfβ
- Instance Id : Instance to which the volume is attaching. Here the instance we previously created
- Volume-Id : the id of the EBS volume we created before.