- Install AWS CLI and setup your AWS profile.
- Create a key pair
- Create a security group
- Launch an instance using the above created key pair and security group.
- Create an EBS volume of 1 GB.
- The final step is to attach the above created EBS volume to the instance you created in the previous steps and able to mount directory on it.
- Detach volume and terminate instances you created.
What is Key-Pair?
A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and you store the private key. You use the private key, instead of a password, to securely access your instances. Anyone who possesses your private keys can connect to your instances, so it’s important that you store your private keys in a secure place.
What is the Security Group?
A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. If you don’t specify a security group, Amazon EC2 uses the default security group.
What is the EC2 Instance?
An instance is a virtual server in the AWS cloud. With Amazon EC2, you can set up and configure the operating system and applications that run on your instance.
What is the EBS Volume?
Amazon Elastic Block Store provides raw block-level storage that can be attached to Amazon EC2 instances and is used by Amazon Relational Database Service. Amazon EBS provides a range of options for storage performance and cost.
Let’s Get Started
Step 1: Download AWS CLI SDK from their official site. If you are installing for first time i would prefer to download the CLI version 2 and download according to your operating system.
For windows it’s very easy to install. Just download and install through msi installer. You can download the latest version directly from here for windows.
Click on next and follow their guide to install aws cli. It’s a very easy process.
Step 2: After successfully installing you can check your installation by following command
Step 3: Now configure your AWS profile by
aws configure command
Here enter the Access Key and Secret Access key which are provided when you create IAM user, then enter your default region and the last one you can skip.
Now if you have done all the steps successfully then you have successfully setup your profile and you are good to go.
Step 4 : Create key-pair : Now we have to create a key-pair. You can do easily by below command
aws ec2 create-key-pair --key-name <name-of-your-key>
Note: Copy this key into a new file with extension .pem as it will be used later. Format the above key in similar manner as shown below by replacing \n with next line.
Step 5: Create a Security group: Now before creating security group check your public IP by below command
So the CIDR notation will be
<your-IP>/24. In my case the CIDR notation will be 184.108.40.206/24. It will restrict the access to your instances except IP having similar notation.
Below command will create security group.
aws ec2 create-security-group --group-name <name-for-your-security-group> --description <"a short description for your security group">
Step 6: Now copy the security-group id in text editor as they are required later. Now after creating security-group, it is important to add rules to it otherwise like SSH access so that we can access it.
aws ec2 authorize-security-group-ingress --group-id <security-group -id> --protocol tcp --port <port-of-service-you-want-to-expose> --cidr <CIDR-Notation-of-IP>
In my case I allowed ssh access from my public ip.
Step 7: Launch ec2 instance: Now for launching ec2 instance we require following information
- ami-id of image denoted by image-id
- no of instances to launch denoted by count
- type of instance denoted by instance-type
- name of key denoted by key-name
- id number of security group denoted by security-group-ids
- subnet id of the region denoted by subnet-id
So after collecting all the above information you can launch instance by below command
NOTE: We will use the same key-pair and security group we created earlier.
aws ec2 run-instances --image-id <ami-id> --count <no-of-count> --instance-type <type-of-instance> --key-name <name-of-key> --security-group-ids <security-group-id> --subnet-id <subnet-id>
Here also note down the instance-id of your instance.
You can also provide tag to your ec2-instance by below command.
aws ec2 create-tags --resources <instance-id> --tags Key=Name,Value=<Name-you-want-to-give>
Step 8: Create EBS volume: Now we have to create EBS volume to attach to our instance. You can easily create ebs volume by following command
aws ec2 create-volume --volume-type gp2 --size <storage-in-GB> --availability-zone <availability-zone-name>
Here also note down the Volume Id as it will be required later.
Step 9: Attaching EBS volume to EC2 instance:
aws ec2 attach-volume --volume-id <volume-id> --instance-id <instance-id> --device /dev/xvdh
So from above picture you can see i created one EBS volume of 1 GB and mounted to our ec2 instance
Step 10: Creating Partition and formatting : So attaching volume only will not allow us to able to able to use that volume. First we have to create partition and then format it to make it usable.
You can create a partition by command
fdisk <device-name> . In our case device name was
/dev/xvdh . So after then
press n to create new partition and then
hit Enter four times to get everything on default and
press w to save and exit. You can see from below photo.
To confirm and to get more details about your partition you can check by following command
fdisk -l . From there, you can get name of your drive which is required while formatting. From below photo, you can see our partition and the name of drive is /dev/xvdh1.
Now after partition is created successfully, you can format your disk before mounting on any directory. The following command will format your disk
Step 11: Mounting directory on drive : To mount any directory or folder, first of all it should be empty and then you can mount by below command
mount /dev/xvdh1 <path-to-your-directory>
df -h command shows all mounting on your system and you can see that our volume is successfully mounted.
Step 12: Forcefully detach and terminate ec2 instance: In order to detach, it is advisable to first unmount your disk and then detach the volume.
✔ To unmount the disk :
umount -l <path-of-your-mounted-folder>
✔ To forcefully detach your volume :
aws ec2 detach-volume — volume-id <volume-id> — force
✔ To terminate your ec2 instance :
aws ec2 terminate-instances — instance-ids <instance-id>
So this is it. We performed all the points we wanted to achieve. I hope you all were able to do it perfectly.